Skip to content

Commit 0154bfe

Browse files
authored
Added commands to dispatch batch updates and individual updates (#19)
* Added start commands * cs fix * cs fixes
1 parent d5eec1b commit 0154bfe

File tree

7 files changed

+151
-13
lines changed

7 files changed

+151
-13
lines changed
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Jobs\UpdateSite;
6+
use App\Models\Site;
7+
use Illuminate\Console\Command;
8+
use App\Console\Traits\RequestTargetVersion;
9+
10+
class PerformUpdate extends Command
11+
{
12+
use RequestTargetVersion;
13+
14+
/**
15+
* The name and signature of the console command.
16+
*
17+
* @var string
18+
*/
19+
protected $signature = 'app:perform-update {siteId}';
20+
21+
/**
22+
* The console command description.
23+
*
24+
* @var string
25+
*/
26+
protected $description = 'Executes an update job for given site id';
27+
28+
/**
29+
* Execute the console command.
30+
*/
31+
public function handle(): int
32+
{
33+
$targetVersion = $this->queryTargetVersion();
34+
35+
/** @var Site $site */
36+
$site = Site::findOrFail($this->input->getArgument('siteId'));
37+
38+
UpdateSite::dispatchSync(
39+
$site,
40+
$targetVersion
41+
);
42+
43+
return Command::SUCCESS;
44+
}
45+
}

app/Console/Commands/QueueUpdates.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Console\Traits\RequestTargetVersion;
6+
use App\Jobs\UpdateSite;
7+
use App\Models\Site;
8+
use Illuminate\Console\Command;
9+
use Illuminate\Database\Eloquent\Collection;
10+
11+
class QueueUpdates extends Command
12+
{
13+
use RequestTargetVersion;
14+
protected int $totalPushed = 0;
15+
16+
/**
17+
* The name and signature of the console command.
18+
*
19+
* @var string
20+
*/
21+
protected $signature = 'app:queue-updates';
22+
23+
/**
24+
* The console command description.
25+
*
26+
* @var string
27+
*/
28+
protected $description = 'Queues updates for all applicable registered sites';
29+
30+
/**
31+
* Execute the console command.
32+
*/
33+
public function handle(): int
34+
{
35+
$targetVersion = $this->queryTargetVersion();
36+
37+
$this->confirm("Are you sure you would like to push the updates for " . $targetVersion);
38+
39+
$this->output->writeln('Pushing update jobs');
40+
41+
Site::query()
42+
->where(
43+
'cms_version',
44+
'like',
45+
$targetVersion[0] . '%'
46+
)
47+
->chunkById(
48+
100,
49+
function (Collection $chunk) use ($targetVersion) {
50+
// Show progress
51+
$this->output->write('.');
52+
53+
$this->totalPushed += $chunk->count();
54+
55+
// Push each site check to queue
56+
$chunk->each(fn ($site) => UpdateSite::dispatch($site, $targetVersion));
57+
}
58+
);
59+
60+
// Result
61+
$this->output->writeln("");
62+
$this->output->writeln('Pushed ' . $this->totalPushed . ' pending jobs to queue');
63+
64+
return Command::SUCCESS;
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Console\Traits;
4+
5+
use App\TUF\TufFetcher;
6+
7+
trait RequestTargetVersion
8+
{
9+
protected function queryTargetVersion(): string
10+
{
11+
$releases = (new TufFetcher())->getReleases();
12+
13+
return $this->choice( // @phpstan-ignore-line
14+
"What's the target version?",
15+
$releases->map(fn ($release) => $release["version"])->values()->toArray() // @phpstan-ignore-line
16+
);
17+
}
18+
}

app/Http/Controllers/Api/V1/SiteController.php

+18-11
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function register(SiteRequest $request): JsonResponse
3636

3737
$connectionService = App::makeWith(
3838
Connection::class,
39-
["baseUrl" => $url, "key" => $key]
39+
["baseUrl" => rtrim($url, "/"), "key" => $key]
4040
);
4141

4242
// Do a health check
@@ -48,11 +48,11 @@ public function register(SiteRequest $request): JsonResponse
4848
return $this->error($e->getMessage(), 500);
4949
}
5050

51-
// If successful save site
52-
$site = new Site();
51+
// If successful create or update site
52+
$site = Site::where('url', $url)->where('key', $key)->first() ?? new Site();
5353

5454
$site->key = $key;
55-
$site->url = rtrim($url, "/");
55+
$site->url = $url;
5656
$site->last_seen = Carbon::now();
5757

5858
// Fill with site info
@@ -75,15 +75,20 @@ public function check(SiteRequest $request): JsonResponse
7575
$url = $request->string('url');
7676
$key = $request->string('key');
7777

78-
$connectionService = new Connection($url, $key);
78+
try {
79+
/** @var Site $site */
80+
$site = Site::where('url', $url)->where('key', $key)->firstOrFail();
81+
} catch (\Exception $e) {
82+
return $this->error("Not found", 404);
83+
}
7984

8085
// Do a health check
8186
try {
82-
$connectionService->checkHealth();
83-
} catch (ServerException $e) {
87+
$site->connection->checkHealth();
88+
} catch (ServerException|ClientException $e) {
89+
return $this->error($e->getMessage(), 400);
90+
} catch (\Exception $e) {
8491
return $this->error($e->getMessage(), 500);
85-
} catch (ClientException|\Exception $e) {
86-
return $this->error($e->getMessage());
8792
}
8893

8994
return $this->ok();
@@ -100,11 +105,13 @@ public function delete(SiteRequest $request): JsonResponse
100105
$key = $request->string('key');
101106

102107
try {
103-
Site::where('url', $url)->where('key', $key)->delete();
108+
$site = Site::where('url', $url)->where('key', $key)->firstOrFail();
104109
} catch (\Exception $e) {
105-
return $this->error($e->getMessage());
110+
return $this->error("Not found", 404);
106111
}
107112

113+
$site->delete();
114+
108115
return $this->ok();
109116
}
110117
}

app/Jobs/UpdateSite.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function handle(): void
6262
return;
6363
}
6464

65-
$prepareResult = $connection->prepareUpdate($this->targetVersion);
65+
$prepareResult = $connection->prepareUpdate(["targetVersion" => $this->targetVersion]);
6666

6767
// Perform the actual extraction
6868
$this->performExtraction($prepareResult);

app/RemoteSite/Connection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/**
2121
* @method HealthCheckResponse checkHealth()
2222
* @method GetUpdateResponse getUpdate()
23-
* @method PrepareUpdateResponse prepareUpdate(string $targetVersion)
23+
* @method PrepareUpdateResponse prepareUpdate(array<string,string> $data)
2424
* @method FinalizeUpdateResponse finalizeUpdate()
2525
*/
2626
class Connection

routes/console.php

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use Illuminate\Support\Facades\Schedule;
44
use Illuminate\Queue\Console\PruneFailedJobsCommand;
55
use App\Console\Commands\QueueHealthChecks;
6+
use App\Console\Commands\CleanupSitesList;
67

8+
Schedule::command(CleanupSitesList::class)->everySixHours();
79
Schedule::command(PruneFailedJobsCommand::class)->daily();
810
Schedule::command(QueueHealthChecks::class)->everyFifteenMinutes();

0 commit comments

Comments
 (0)