Skip to content

Commit bd129fb

Browse files
committed
Added start commands
1 parent d5eec1b commit bd129fb

File tree

6 files changed

+151
-11
lines changed

6 files changed

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

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

Lines changed: 16 additions & 9 deletions
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
@@ -52,7 +52,7 @@ public function register(SiteRequest $request): JsonResponse
5252
$site = 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)->findOrFail();
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->getConnection()->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)->findOrFail();
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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ public function handle(): void
6262
return;
6363
}
6464

65-
$prepareResult = $connection->prepareUpdate($this->targetVersion);
66-
65+
$prepareResult = $connection->prepareUpdate(["targetVersion" => $this->targetVersion]);
6766
// Perform the actual extraction
6867
$this->performExtraction($prepareResult);
6968

routes/console.php

Lines changed: 2 additions & 0 deletions
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)