Skip to content

Commit

Permalink
Merge pull request #33 from deliciousbrains/29-fix-purge-output
Browse files Browse the repository at this point in the history
Purge site command output is now consistent with other commands
  • Loading branch information
A5hleyRich authored Feb 28, 2022
2 parents 1e829fa + b0287cb commit bfb9d51
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
15 changes: 11 additions & 4 deletions app/Commands/Concerns/InteractsWithIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ public function askToSelectSite(string $question, callable $filter = null): int
$choices = $choices->filter(fn ($site) => $filter($site));
}

if ($choices->isEmpty()) {
return 0;
}

return $this->askToSelect(
$question,
$choices->keyBy('id')->map(fn ($site) => $site->domain)->toArray()
Expand Down Expand Up @@ -201,20 +205,23 @@ protected function forceOrConfirm(string $confirmation, bool $default = true): b
return (bool) $this->option('force') || $this->confirm($confirmation, $default);
}

public function queueResources(Collection $resources, string $endpoint, string $verb): void
public function queueResources(Collection $resources, string $endpoint, string $verb, string $resourcesId = 'name', bool $shouldWait = false): void
{
if ($resources->isEmpty()) {
return;
}

$resourceName = strtolower(class_basename($resources[0]));
$resourceName = strtolower(class_basename($resources->first()));

$events = [];

$resources->each(function ($resource) use ($resources, $endpoint, &$events, $verb) {
$resources->each(function ($resource) use ($resources, $endpoint, &$events, $verb, $resourcesId, $shouldWait) {
try {
if ($shouldWait) {
sleep(1);
}
$eventId = call_user_func(fn () => $resource->$endpoint());
$events[] = ["{$eventId}", $resource->name];
$events[] = ["{$eventId}", $resource->{$resourcesId}];
} catch (\Exception $e) {
if ($resources->count() === 1) {
$this->error("{$verb} failed on {$resource->name}.");
Expand Down
54 changes: 34 additions & 20 deletions app/Commands/Sites/PurgeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Commands\Sites;

use Illuminate\Support\Collection;

class PurgeCommand extends \App\Commands\BaseCommand
{
protected $signature = 'sites:purge {site_id? : The site to purge}
Expand Down Expand Up @@ -32,42 +34,54 @@ protected function action(): int
$siteId = $this->argument('site_id');

if (empty($siteId)) {
$siteId = $this->askToSelectSite('Which site do you want to purge the page cache for');
$siteId = $this->askToSelectSite('Which site do you want to purge the page cache for', function ($site) use ($cacheToPurge) {
if ($cacheToPurge === 'page') {
return $site->page_cache['enabled'];
}
return $site->is_wordpress;
});
}

if ($siteId === 0) {
$this->warn("There are no sites with {$cacheToPurge} cache enabled.");
return self::SUCCESS;
}

$site = $this->spinupwp->sites->get(intval($siteId));

$this->purgeCache([$site], $cacheToPurge);
$this->purgeCache(collect([$site]), $cacheToPurge);

return self::SUCCESS;
}

protected function purgeCacheOnAllSites(string $cacheToPurge): void
{
$sites = $this->spinupwp->sites->list()->toArray();
$shouldWait = count($sites) > 59;
$this->purgeCache($sites, $cacheToPurge, $shouldWait);
}
$sites = collect($this->spinupwp->sites->list());
if ($cacheToPurge === 'page') {
$sites = $sites->filter(fn ($site) => $site->page_cache['enabled']);
}
if ($cacheToPurge === 'object') {
$sites = $sites->filter(fn ($site) => $site->is_wordpress);
}

protected function purgeCache(array $sites, string $cacheToPurge, bool $shouldWait = false): void
{
if (empty($sites)) {
if ($sites->isEmpty()) {
$this->warn("There are no sites with {$cacheToPurge} cache enabled.");
return;
}

foreach ($sites as $site) {
if ($cacheToPurge === 'page' && !$site->page_cache['enabled']) {
continue;
}

$cache = $cacheToPurge === 'page' ? 'page' : 'object';
$response = $cacheToPurge === 'page' ? $site->purgePageCache() : $site->purgeObjectCache();
$shouldWait = $sites->count() > 55;

$this->info("Purging {$cache} cache for site {$site->domain}. Event ID: {$response}");
$this->purgeCache($sites, $cacheToPurge, $shouldWait);
}

if ($shouldWait) {
sleep(1);
}
protected function purgeCache(Collection $sites, string $cacheToPurge, bool $shouldWait = false): void
{
if ($sites->isEmpty()) {
return;
}

$endpoint = $cacheToPurge === 'page' ? 'purgePageCache' : 'purgeObjectCache';
$verb = "{$cacheToPurge} cache purge";
$this->queueResources($sites, $endpoint, $verb, 'domain', $shouldWait);
}
}
19 changes: 9 additions & 10 deletions tests/Feature/Commands/SitesPurgeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'page_cache' => [
'enabled' => true,
],
'is_wordpress' => true,
]]))
);

Expand All @@ -29,18 +30,18 @@
});

test('purge site page cache', function () {
$this->artisan('sites:purge 1 --cache=page')->expectsOutput('Purging page cache for site hellfish.media. Event ID: 100');
$this->artisan('sites:purge 1 --cache=page')->expectsOutput('==> Site queued for page cache purge.');
});

test('purge site object cache', function () {
$this->artisan('sites:purge 1 --cache=object')->expectsOutput('Purging object cache for site hellfish.media. Event ID: 100');
$this->artisan('sites:purge 1 --cache=object')->expectsOutput('==> Site queued for object cache purge.');
});

test('purge all sites page cache', function () {
$this->clientMock->shouldReceive('request')->once()->with('GET', 'sites?page=1', [])->andReturn(
new Response(200, [], listResponseJson([
['id' => 1, 'domain' => 'hellfish.media', 'page_cache' => ['enabled' => true]],
['id' => 2, 'domain' => 'staging.hellfish.media', 'page_cache' => ['enabled' => true]],
['id' => 1, 'domain' => 'hellfish.media', 'page_cache' => ['enabled' => true], 'is_wordpress' => true],
['id' => 2, 'domain' => 'staging.hellfish.media', 'page_cache' => ['enabled' => true], 'is_wordpress' => true],
]))
);

Expand All @@ -49,15 +50,14 @@
->andReturn(new Response(200, [], json_encode(['event_id' => 101])));

$this->artisan('sites:purge --all --cache=page')
->expectsOutput('Purging page cache for site hellfish.media. Event ID: 100')
->expectsOutput('Purging page cache for site staging.hellfish.media. Event ID: 101');
->expectsOutput('==> Sites queued for page cache purge.');
});

test('purge all sites object cache', function () {
$this->clientMock->shouldReceive('request')->once()->with('GET', 'sites?page=1', [])->andReturn(
new Response(200, [], listResponseJson([
['id' => 1, 'domain' => 'hellfish.media', 'page_cache' => ['enabled' => true]],
['id' => 2, 'domain' => 'staging.hellfish.media', 'page_cache' => ['enabled' => true]],
['id' => 1, 'domain' => 'hellfish.media', 'page_cache' => ['enabled' => true], 'is_wordpress' => true],
['id' => 2, 'domain' => 'staging.hellfish.media', 'page_cache' => ['enabled' => true], 'is_wordpress' => true],
]))
);

Expand All @@ -66,6 +66,5 @@
->andReturn(new Response(200, [], json_encode(['event_id' => 101])));

$this->artisan('sites:purge --all --cache=object')
->expectsOutput('Purging object cache for site hellfish.media. Event ID: 100')
->expectsOutput('Purging object cache for site staging.hellfish.media. Event ID: 101');
->expectsOutput('==> Sites queued for object cache purge.');
});

0 comments on commit bfb9d51

Please sign in to comment.