Skip to content

Commit

Permalink
Merge pull request #1 from cash-track/feature/logs-timestamps
Browse files Browse the repository at this point in the history
Feature / Logs Timestamps
  • Loading branch information
vokomarov authored Sep 4, 2021
2 parents 71a3cc3 + b8df295 commit e89880a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
16 changes: 16 additions & 0 deletions src/Commands/AbstractCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Commands;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;

abstract class AbstractCommand extends Command
{
protected function log(OutputInterface $output, string $message = '')
{
$time = (new \DateTimeImmutable())->format('Y-m-d H:i:s');

$output->writeln("[{$time}] {$message}");
}
}
7 changes: 2 additions & 5 deletions src/Commands/BackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
use App\Services\MySQLExecutor;
use App\Services\Pipeline;
use App\Services\S3Executor;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class BackupCommand extends Command
class BackupCommand extends AbstractCommand
{
use FileFormat;

Expand All @@ -35,12 +34,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
S3Executor::make()->upload($file),
]);

$output->writeln("Making backup {$file}...");
$this->log($output, "Making backup {$file}");

$output->write(exec($command, $out, $result));

$output->writeln('Done.');

return $result;
}
}
6 changes: 3 additions & 3 deletions src/Commands/ClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ClearCommand extends Command
class ClearCommand extends AbstractCommand
{
use FileFormat;

Expand All @@ -29,7 +29,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$days = (int) $input->getOption('days');

if ($days === 0) {
$output->writeln('Nothing to clear as days is set to zero.');
$this->log($output, 'Nothing to clear as days is set to zero.');
return Command::SUCCESS;
}

Expand All @@ -48,7 +48,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
continue;
}

$output->writeln("Removing {$backup['file']}");
$this->log($output, "Removing old backup {$backup['file']}");

exec($executor->remove($backup['file']));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ListCommand extends Command
class ListCommand extends AbstractCommand
{
use FileFormat;

Expand Down
13 changes: 6 additions & 7 deletions src/Commands/RestoreCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class RestoreCommand extends Command
class RestoreCommand extends AbstractCommand
{
use FileFormat;

Expand All @@ -31,28 +31,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$file = $this->renderFormat($id);

if (! $this->isExists($id)) {
$output->writeln("Backup file {$file} does not exists on S3 bucket.");
$this->log($output, "Backup file {$file} does not exists on S3 bucket.");
return Command::SUCCESS;
}


$output->writeln('Preparing database..');
$this->log($output, 'Preparing database..');

$output->write(exec(MySQLExecutor::make()->init(), $out, $result));

$output->writeln('Done.');
$this->log($output, 'Done.');

$command = Pipeline::make([
S3Executor::make()->download($this->renderFormat($id)),
GZipExecutor::make()->decompress(),
MySQLExecutor::make()->restore(),
]);

$output->writeln("Restoring backup {$file}...");
$this->log($output, "Restoring backup {$file}...");

$output->write(exec($command, $out, $result));

$output->writeln('Done.');
$this->log($output, 'Done.');

return $result;
}
Expand Down

0 comments on commit e89880a

Please sign in to comment.