Skip to content

Commit

Permalink
add option to queue in update command
Browse files Browse the repository at this point in the history
  • Loading branch information
david-d-h committed May 18, 2024
1 parent 034d895 commit bbaba45
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
8 changes: 2 additions & 6 deletions src/CachesValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ final public static function update($parameters = [])
{
$instance = app()->make(static::class, $parameters);

dispatch(
$instance
);
dispatch($instance);
}

/**
Expand All @@ -139,9 +137,7 @@ final public static function updateAndGet($parameters = [])
{
$instance = app()->make(static::class, $parameters);

dispatch(
$instance
)->onConnection('sync');
dispatch($instance)->onConnection('sync');

return static::get($parameters);
}
Expand Down
15 changes: 11 additions & 4 deletions src/Commands/UpdatePermanentCachesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Spatie\Emoji\Emoji;
use SplObjectStorage;
use Symfony\Component\Console\Helper\ProgressBar;
use Vormkracht10\PermanentCache\CachesValue;
use Vormkracht10\PermanentCache\Facades\PermanentCache;

class UpdatePermanentCachesCommand extends Command
Expand All @@ -16,7 +17,7 @@ class UpdatePermanentCachesCommand extends Command
*
* @var string
*/
protected $signature = 'permanent-cache:update {--filter=}';
protected $signature = 'permanent-cache:update {--filter=} {--connection=} {--queue=}';

/**
* The console command description.
Expand Down Expand Up @@ -57,17 +58,23 @@ public function handle()
$progressBar->start();

foreach ($caches as $c) {
/** @var CachesValue $cache */
$cache = $caches->current();
$parameters = $caches->getInfo();

$currentTask = $cache->getName();
$emoji = ($progressBar->getProgress() % 2 ? Emoji::hourglassNotDone() : Emoji::hourglassDone());

$progressBar->setMessage('Updating: '.$currentTask.' '.$emoji);

$connection = $this->option('connection') ?? 'sync';

$queue = $connection === 'sync' ? null : $this->option('queue');

try {
$cache->update($parameters);
} catch (Exception $exception) {
dispatch($cache)
->onConnection($connection)
->onQueue($queue);
} catch (Exception) {
$progressBar->setMessage('Error: '.$currentTask.' '.Emoji::warning());

sleep(2);
Expand Down
2 changes: 1 addition & 1 deletion src/PermanentCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function caches($registeredCaches): self
$cacheInstance = $this->app->make($cache, $parameters);

if ([] !== $events = $cacheInstance->getListenerEvents()) {
foreach($events as $event) {
foreach ($events as $event) {
Event::listen($event, fn ($e) => $cacheInstance->handle($e));
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Vormkracht10\PermanentCache\Tests;

use Illuminate\Database\Eloquent\Factories\Factory;
use Orchestra\Testbench\TestCase as Orchestra;
use Vormkracht10\PermanentCache\PermanentCacheServiceProvider;

Expand Down

0 comments on commit bbaba45

Please sign in to comment.