Skip to content

Commit

Permalink
check for new release once a day
Browse files Browse the repository at this point in the history
this is an experiment to replace the approach that utilized the schedule
  • Loading branch information
NiclasvanEyk committed Sep 2, 2023
1 parent 67b98bc commit 4d08de3
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,58 @@
namespace Domains\CreateProjectForm\Http\Controllers;

use Domains\CreateProjectForm\Http\Request\CreateProjectRequest;
use Domains\ProjectTemplate\ProjectTemplateService;
use Domains\ProjectTemplateCustomization\ProjectTemplateCustomizer;
use Domains\Statistics\StatisticsService;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Cache;
use Symfony\Component\HttpFoundation\Response;

class CreateProjectController
{
public function __invoke(
CreateProjectRequest $request,
ProjectTemplateCustomizer $builder,
Application $app,
StatisticsService $statistics,
): Response {
public function __construct(
private readonly ProjectTemplateService $templateStorage,
private readonly ProjectTemplateCustomizer $builder,
private readonly Application $app,
private readonly StatisticsService $statistics,
) {
}

public function __invoke(CreateProjectRequest $request) : Response
{
$this->maybeUpdateTemplate();

$form = $request->buildForm();
$name = $form->metadata->projectName;
$archive = $builder->build($form);
$archive = $this->builder->build($form);

$app->terminating(fn () => rescue(fn () => $statistics->record($request)));
$this->recordStatistics($request);

$name = $form->metadata->projectName;
return $archive->outputAsSymfonyResponse("$name.zip");
}

private function recordStatistics(CreateProjectRequest $request) : void
{
$this->app->terminating(function () use ($request) {
rescue(fn () => $this->statistics->record($request));
});
}

private function maybeUpdateTemplate() : void
{
$this->app->terminating(function () {
if (Cache::get('template-requires-check') === false) {
return;
}

info("Checking if template needs an update triggered by a CreateProjectRequest...");
if ($this->templateStorage->canBeUpdated()) {
info("Updating template triggered by a CreateProjectRequest...");
$this->templateStorage->update();
}

info("Don't check again for 24 hours...");
Cache::set('template-requires-check', false, ttl: now()->addDays(1));

Check failure on line 57 in domains/CreateProjectForm/Http/Controllers/CreateProjectController.php

View workflow job for this annotation

GitHub Actions / unit-tests

Parameter $ttl of static method Illuminate\Cache\Repository::set() expects DateInterval|int|null, Illuminate\Support\Carbon given.
});
}
}
27 changes: 10 additions & 17 deletions domains/ProjectTemplate/Console/Commands/UpdateTemplateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace Domains\ProjectTemplate\Console\Commands;

use Domains\ProjectTemplate\LaravelDownloader;
use Domains\ProjectTemplate\TemplateStorage;
use Domains\ProjectTemplate\ProjectTemplateService;
use Illuminate\Console\Command;
use Log;

Expand All @@ -12,29 +11,23 @@ class UpdateTemplateCommand extends Command
protected $signature = 'initializer:update-template';
protected $description = 'Downloads the latest release of Laravel if necessary.';

public function handle(
LaravelDownloader $downloader,
TemplateStorage $templateStorage,
): void {
$latestRelease = $downloader->latestRelease();

if ($templateStorage->currentVersion() === $latestRelease->version) {
$this->logAndInfo("$latestRelease->version is still the latest release!");
public function handle(ProjectTemplateService $template) : void
{
if (! $template->canBeUpdated()) {
$this->logAndInfo("Local template storage does not need to be updated");

return;
}

$this->logAndInfo("Downloading $latestRelease->version...");

$downloadedRelease = $downloader->download($latestRelease);
$templateStorage->updateCurrentRelease($downloadedRelease);
$this->logAndInfo("Downloading latest release...");
$template->update();

$this->logAndInfo("Finished downloading $latestRelease->version!");
$this->logAndInfo("Template was updated to the latest release!");
}

private function logAndInfo(string $message): void
private function logAndInfo(string $message) : void
{
$this->info($message);
Log::info($message);
}
}
}
27 changes: 27 additions & 0 deletions domains/ProjectTemplate/ProjectTemplateService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Domains\ProjectTemplate;

class ProjectTemplateService
{
public function __construct(
private readonly TemplateStorage $storage,
private readonly LaravelDownloader $downloader,
) {
}

public function canBeUpdated() : bool
{
$latestRelease = $this->downloader->latestRelease();
$currentVersion = $this->storage->currentVersion();

return $latestRelease->version !== $currentVersion;
}

public function update() : void
{
$latestRelease = $this->downloader->latestRelease();
$downloadedRelease = $this->downloader->download($latestRelease);
$this->storage->updateCurrentRelease($downloadedRelease);
}
}

0 comments on commit 4d08de3

Please sign in to comment.