Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added commands to dispatch batch updates and individual updates #19

Merged
merged 3 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions app/Console/Commands/PerformUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Console\Commands;

use App\Jobs\UpdateSite;
use App\Models\Site;
use Illuminate\Console\Command;
use App\Console\Traits\RequestTargetVersion;

class PerformUpdate extends Command
{
use RequestTargetVersion;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:perform-update {siteId}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Executes an update job for given site id';

/**
* Execute the console command.
*/
public function handle(): int
{
$targetVersion = $this->queryTargetVersion();

/** @var Site $site */
$site = Site::findOrFail($this->input->getArgument('siteId'));

UpdateSite::dispatchSync(
$site,
$targetVersion
);

return Command::SUCCESS;
}
}
66 changes: 66 additions & 0 deletions app/Console/Commands/QueueUpdates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Console\Commands;

use App\Console\Traits\RequestTargetVersion;
use App\Jobs\UpdateSite;
use App\Models\Site;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;

class QueueUpdates extends Command
{
use RequestTargetVersion;
protected int $totalPushed = 0;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:queue-updates';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Queues updates for all applicable registered sites';

/**
* Execute the console command.
*/
public function handle(): int
{
$targetVersion = $this->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;
}
}
18 changes: 18 additions & 0 deletions app/Console/Traits/RequestTargetVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Console\Traits;

use App\TUF\TufFetcher;

trait RequestTargetVersion
{
protected function queryTargetVersion(): string
{
$releases = (new TufFetcher())->getReleases();

return $this->choice( // @phpstan-ignore-line
"What's the target version?",
$releases->map(fn ($release) => $release["version"])->values()->toArray() // @phpstan-ignore-line
);
}
}
29 changes: 18 additions & 11 deletions app/Http/Controllers/Api/V1/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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();
Expand All @@ -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();
}
}
2 changes: 1 addition & 1 deletion app/Jobs/UpdateSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion app/RemoteSite/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* @method HealthCheckResponse checkHealth()
* @method GetUpdateResponse getUpdate()
* @method PrepareUpdateResponse prepareUpdate(string $targetVersion)
* @method PrepareUpdateResponse prepareUpdate(array<string,string> $data)
* @method FinalizeUpdateResponse finalizeUpdate()
*/
class Connection
Expand Down
2 changes: 2 additions & 0 deletions routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Loading