Skip to content

Commit

Permalink
feat: add initial logs command and signal handler
Browse files Browse the repository at this point in the history
  • Loading branch information
praswicaksono committed Nov 17, 2024
1 parent f1d7a4a commit c55ace3
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 8 deletions.
1 change: 0 additions & 1 deletion bin/mager
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ $exitCode = 0;
} catch (ExitException $e) {
$exitCode = $e->getStatus();
}

});

exit($exitCode);
4 changes: 2 additions & 2 deletions helpers/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ function runLocally(callable|string|TaskInterface $command, bool $showProgress =
return run($command, 'local', local: true, showProgress: $showProgress, throwError: $throwError, tty: $tty);
}

function runOnManager(callable|string|TaskInterface $command, string $on, bool $showProgress = true, bool $throwError = true): mixed {
return run($command, $on, managerOnly: true, showProgress: $showProgress, throwError: $throwError);
function runOnManager(callable|string|TaskInterface $command, string $on, bool $showProgress = true, bool $throwError = true, bool $tty = false): mixed {
return run($command, $on, managerOnly: true, showProgress: $showProgress, throwError: $throwError, tty: $tty);
}

function runOnWorker(callable|string|TaskInterface $command, string $on, bool $showProgress = true, bool $throwError = true): void {
Expand Down
2 changes: 1 addition & 1 deletion src/Command/DeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ private function deploy(
// just update image if service exists
// TODO: update other configuration like cpu, labels, mount, ram
if (yield CommandHelper::isServiceRunning($namespace, $serviceName)) {
yield "Deploying {$fullServiceName}" => "docker service update --image {$imageName} --force {$namespace}-{$serviceName}";
yield "Deploying {$fullServiceName}" => "docker service update --image {$imageName} --force --task-history-limit 5 {$namespace}-{$serviceName}";

return;
}
Expand Down
64 changes: 64 additions & 0 deletions src/Command/LogsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\Command;

use App\Component\Config\Config;
use App\Helper\CommandHelper;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(
name: 'logs',
description: 'Show logs for given service',
)]
class LogsCommand extends Command
{
public function __construct(private readonly Config $config)
{
parent::__construct();
}

protected function configure(): void
{
$this->addArgument('namespace', InputArgument::REQUIRED);
$this->addArgument('serviceName', InputArgument::REQUIRED);

$this->addOption('follow', 'f', InputOption::VALUE_NONE, 'Stream logs until it cancelled');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$namespace = $input->getArgument('namespace');
$serviceName = $input->getArgument('serviceName');
$follow = $input->getOption('follow') ?? false;

$cmd = ['docker', 'service', 'logs', "{$namespace}-{$serviceName}"];
$tty = false;
if ($follow) {
$cmd[] = '--follow';
$tty = true;
}

$cmd = implode(' ', $cmd);

$isRunning = runOnManager(fn() => yield CommandHelper::isServiceRunning($namespace, $serviceName), $namespace);
if (empty($isRunning)) {
$io->error("Service {$namespace}-{$serviceName} is not running");
return Command::FAILURE;
}

$res = runOnManager(fn() => yield $cmd, $namespace, throwError: false, tty: $tty);

if (false === $tty) {
$io->writeln($res);
}

return Command::SUCCESS;
}
}
12 changes: 10 additions & 2 deletions src/Component/TaskRunner/LocalRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace App\Component\TaskRunner;

use Swoole\Coroutine;
use Swoole\Coroutine\System;
use Swoole\Timer;
use Symfony\Component\Console\Helper\ProgressIndicator;
use Symfony\Component\Console\Style\SymfonyStyle;
Expand Down Expand Up @@ -38,6 +40,13 @@ public function run(\Generator $tasks, bool $showProgress = true, bool $throwErr
}

$timer = Timer::tick(500, fn() => $progress?->advance());
$cid = go(function() use ($process) {
System::waitSignal(SIGINT);
if (! $process->isRunning()) {
return;
}
$process->stop(signal: SIGINT);
});

try {
$process->wait(function () use ($progress, $showProgress) {
Expand All @@ -51,13 +60,12 @@ public function run(\Generator $tasks, bool $showProgress = true, bool $throwErr
throw $e;
}

Coroutine::cancel($cid);
Timer::clear($timer);

$exitCode = $process->getExitCode();

if (0 !== $exitCode && $throwError) {
$this->io->error($process->getErrorOutput());
$this->io->writeln($process->getOutput());
$tasks->throw(new \Exception($process->getErrorOutput()));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Helper/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public static function generateTlsCertificateLocally(string $namespace, string $
->withCommand($domain);
}

public static function isServiceRunning(string $namespace, string $name, string $mode = 'replicated'): string
public static function isServiceRunning(string $namespace, string $name): string
{
$fullServiceName = "{$namespace}-{$name}";

return sprintf('docker service ls --format "{{.ID}}" --filter name=%s --filter mode=%s', $fullServiceName, $mode);
return sprintf('docker service ps --format "{{.ID}}" %s', $fullServiceName);
}

public static function removeService(string $namespace, string $name, string $mode = 'replicated'): \Generator
Expand Down

0 comments on commit c55ace3

Please sign in to comment.