From 712b6a285b29e087c98fd90f429f1f1d4c058200 Mon Sep 17 00:00:00 2001 From: Boy132 Date: Sat, 18 May 2024 19:31:02 +0200 Subject: [PATCH] Add artisan command to create queue worker service (#253) * add command to create queue worker service file * remove comments from service file that are no longer needed * only create queue worker service file if queue driver is not "sync" * make "database" the recommended queue driver, again --- .../Environment/AppSettingsCommand.php | 29 +++---- .../Environment/QueueWorkerServiceCommand.php | 76 +++++++++++++++++++ 2 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 app/Console/Commands/Environment/QueueWorkerServiceCommand.php diff --git a/app/Console/Commands/Environment/AppSettingsCommand.php b/app/Console/Commands/Environment/AppSettingsCommand.php index cb5520742..8a01437fa 100644 --- a/app/Console/Commands/Environment/AppSettingsCommand.php +++ b/app/Console/Commands/Environment/AppSettingsCommand.php @@ -24,9 +24,9 @@ class AppSettingsCommand extends Command ]; public const QUEUE_DRIVERS = [ - 'sync' => 'Synchronous (recommended)', - 'database' => 'Database', + 'database' => 'Database (recommended)', 'redis' => 'Redis', + 'sync' => 'Synchronous', ]; protected $description = 'Configure basic environment settings for the Panel.'; @@ -103,7 +103,13 @@ class AppSettingsCommand extends Command $this->variables['SESSION_SECURE_COOKIE'] = 'true'; } - $this->checkForRedis(); + $redisUsed = count(collect($this->variables)->filter(function ($item) { + return $item === 'redis'; + })) !== 0; + + if ($redisUsed) { + $this->requestRedisSettings(); + } $path = base_path('.env'); if (!file_exists($path)) { @@ -116,25 +122,20 @@ class AppSettingsCommand extends Command Artisan::call('key:generate'); } + if ($this->variables['QUEUE_CONNECTION'] !== 'sync') { + Artisan::call('p:environment:queue-service', $redisUsed ? ['--use-redis'] : []); + } + $this->info($this->console->output()); return 0; } /** - * Check if redis is selected, if so, request connection details and verify them. + * Request connection details and verify them. */ - private function checkForRedis() + private function requestRedisSettings(): void { - $items = collect($this->variables)->filter(function ($item) { - return $item === 'redis'; - }); - - // Redis was not selected, no need to continue. - if (count($items) === 0) { - return; - } - $this->output->note(__('commands.appsettings.redis.note')); $this->variables['REDIS_HOST'] = $this->option('redis-host') ?? $this->ask( 'Redis Host', diff --git a/app/Console/Commands/Environment/QueueWorkerServiceCommand.php b/app/Console/Commands/Environment/QueueWorkerServiceCommand.php new file mode 100644 index 000000000..eb704ac5c --- /dev/null +++ b/app/Console/Commands/Environment/QueueWorkerServiceCommand.php @@ -0,0 +1,76 @@ +option('service-name') ?? $this->ask('Service name', 'pelican-queue'); + $path = '/etc/systemd/system/' . $serviceName . '.service'; + + if ($this->input->isInteractive()) { + if (file_exists($path) && !$this->confirm('The service file already exists. Do you want to overwrite it?')) { + return; + } + + $user = $this->option('user') ?? $this->ask('User', 'www-data'); + $group = $this->option('group') ?? $this->ask('Group', 'www-data'); + + $afterRedis = $this->option('use-redis') ? '\nAfter=redis-server.service' : ''; + } else { + $user = 'www-data'; + $group = 'www-data'; + + $afterRedis = ''; + } + + $basePath = base_path(); + + $success = File::put($path, "# Pelican Queue File +# ---------------------------------- + +[Unit] +Description=Pelican Queue Service$afterRedis + +[Service] +User=$user +Group=$group +Restart=always +ExecStart=/usr/bin/php $basePath/artisan queue:work --queue=high,standard,low --tries=3 +StartLimitInterval=180 +StartLimitBurst=30 +RestartSec=5s + +[Install] +WantedBy=multi-user.target + "); + + if (!$success) { + $this->error('Error creating service file'); + + return; + } + + $result = Process::run("systemctl enable --now $serviceName.service"); + if ($result->failed()) { + $this->error('Error enabling service: ' . $result->errorOutput()); + + return; + } + + $this->line('Queue worker service file created successfully.'); + } +}