diff --git a/app/Console/Commands/PerformUpdate.php b/app/Console/Commands/PerformUpdate.php new file mode 100644 index 0000000..59b7b95 --- /dev/null +++ b/app/Console/Commands/PerformUpdate.php @@ -0,0 +1,45 @@ +queryTargetVersion(); + + /** @var Site $site */ + $site = Site::findOrFail($this->input->getArgument('siteId')); + + UpdateSite::dispatchSync( + $site, + $targetVersion + ); + + return Command::SUCCESS; + } +} diff --git a/app/Console/Commands/QueueUpdates.php b/app/Console/Commands/QueueUpdates.php new file mode 100644 index 0000000..59385a6 --- /dev/null +++ b/app/Console/Commands/QueueUpdates.php @@ -0,0 +1,66 @@ +queryTargetVersion(); + + $this->confirm("Are you sure you would like to push the updates for " . $targetVersion); + + $this->output->writeln('Pushing update jobs'); + + Site::query() + ->where( + 'cms_version', + 'like', + $targetVersion[0] . '%' + ) + ->chunkById( + 100, + function (Collection $chunk) use ($targetVersion) { + // Show progress + $this->output->write('.'); + + $this->totalPushed += $chunk->count(); + + // Push each site check to queue + $chunk->each(fn ($site) => UpdateSite::dispatch($site, $targetVersion)); + } + ); + + // Result + $this->output->writeln(""); + $this->output->writeln('Pushed ' . $this->totalPushed . ' pending jobs to queue'); + + return Command::SUCCESS; + } +} diff --git a/app/Console/Traits/RequestTargetVersion.php b/app/Console/Traits/RequestTargetVersion.php new file mode 100644 index 0000000..5102f2d --- /dev/null +++ b/app/Console/Traits/RequestTargetVersion.php @@ -0,0 +1,18 @@ +getReleases(); + + return $this->choice( // @phpstan-ignore-line + "What's the target version?", + $releases->map(fn ($release) => $release["version"])->values()->toArray() // @phpstan-ignore-line + ); + } +} diff --git a/app/Http/Controllers/Api/V1/SiteController.php b/app/Http/Controllers/Api/V1/SiteController.php index bae991d..b653542 100644 --- a/app/Http/Controllers/Api/V1/SiteController.php +++ b/app/Http/Controllers/Api/V1/SiteController.php @@ -36,7 +36,7 @@ public function register(SiteRequest $request): JsonResponse $connectionService = App::makeWith( Connection::class, - ["baseUrl" => $url, "key" => $key] + ["baseUrl" => rtrim($url, "/"), "key" => $key] ); // Do a health check @@ -48,11 +48,11 @@ public function register(SiteRequest $request): JsonResponse return $this->error($e->getMessage(), 500); } - // If successful save site - $site = new Site(); + // If successful create or update site + $site = Site::where('url', $url)->where('key', $key)->first() ?? new Site(); $site->key = $key; - $site->url = rtrim($url, "/"); + $site->url = $url; $site->last_seen = Carbon::now(); // Fill with site info @@ -75,15 +75,20 @@ public function check(SiteRequest $request): JsonResponse $url = $request->string('url'); $key = $request->string('key'); - $connectionService = new Connection($url, $key); + try { + /** @var Site $site */ + $site = Site::where('url', $url)->where('key', $key)->firstOrFail(); + } catch (\Exception $e) { + return $this->error("Not found", 404); + } // Do a health check try { - $connectionService->checkHealth(); - } catch (ServerException $e) { + $site->connection->checkHealth(); + } catch (ServerException|ClientException $e) { + return $this->error($e->getMessage(), 400); + } catch (\Exception $e) { return $this->error($e->getMessage(), 500); - } catch (ClientException|\Exception $e) { - return $this->error($e->getMessage()); } return $this->ok(); @@ -100,11 +105,13 @@ public function delete(SiteRequest $request): JsonResponse $key = $request->string('key'); try { - Site::where('url', $url)->where('key', $key)->delete(); + $site = Site::where('url', $url)->where('key', $key)->firstOrFail(); } catch (\Exception $e) { - return $this->error($e->getMessage()); + return $this->error("Not found", 404); } + $site->delete(); + return $this->ok(); } } diff --git a/app/Jobs/UpdateSite.php b/app/Jobs/UpdateSite.php index bd16e83..3cfcff4 100644 --- a/app/Jobs/UpdateSite.php +++ b/app/Jobs/UpdateSite.php @@ -62,7 +62,7 @@ public function handle(): void return; } - $prepareResult = $connection->prepareUpdate($this->targetVersion); + $prepareResult = $connection->prepareUpdate(["targetVersion" => $this->targetVersion]); // Perform the actual extraction $this->performExtraction($prepareResult); diff --git a/app/RemoteSite/Connection.php b/app/RemoteSite/Connection.php index 467c4da..79a779b 100644 --- a/app/RemoteSite/Connection.php +++ b/app/RemoteSite/Connection.php @@ -20,7 +20,7 @@ /** * @method HealthCheckResponse checkHealth() * @method GetUpdateResponse getUpdate() - * @method PrepareUpdateResponse prepareUpdate(string $targetVersion) + * @method PrepareUpdateResponse prepareUpdate(array $data) * @method FinalizeUpdateResponse finalizeUpdate() */ class Connection diff --git a/routes/console.php b/routes/console.php index b05cd0e..aabe8d1 100644 --- a/routes/console.php +++ b/routes/console.php @@ -3,6 +3,8 @@ use Illuminate\Support\Facades\Schedule; use Illuminate\Queue\Console\PruneFailedJobsCommand; use App\Console\Commands\QueueHealthChecks; +use App\Console\Commands\CleanupSitesList; +Schedule::command(CleanupSitesList::class)->everySixHours(); Schedule::command(PruneFailedJobsCommand::class)->daily(); Schedule::command(QueueHealthChecks::class)->everyFifteenMinutes();