Skip to content

Commit

Permalink
Make the assets:generate-presets command queue friendly (#3490)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga authored Apr 7, 2021
1 parent a0c1a6f commit d630244
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/Console/Commands/AssetsGeneratePresets.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Statamic\Facades\Asset;
use Statamic\Facades\Config;
use Statamic\Facades\Image;
use Statamic\Imaging\PresetGenerator;
use Statamic\Jobs\GeneratePresetImageManipulation;

class AssetsGeneratePresets extends Command
{
Expand All @@ -18,7 +18,7 @@ class AssetsGeneratePresets extends Command
*
* @var string
*/
protected $signature = 'statamic:assets:generate-presets';
protected $signature = 'statamic:assets:generate-presets {--queue : Queue the image generation.}';

/**
* The console command description.
Expand All @@ -27,27 +27,28 @@ class AssetsGeneratePresets extends Command
*/
protected $description = 'Generate asset preset manipulations.';

/**
* @var PresetGenerator
*/
protected $generator;

/**
* @var \Statamic\Assets\AssetCollection
*/
protected $imageAssets;

public function __construct(PresetGenerator $generator)
{
$this->generator = $generator;
parent::__construct();
}
/**
* @var bool
*/
protected $shouldQueue = false;

/**
* Execute the console command.
*/
public function handle()
{
$this->shouldQueue = $this->option('queue');

if ($this->shouldQueue && config('queue.default') === 'sync') {
$this->error('The queue connection is set to "sync". Queueing will be disabled.');
$this->shouldQueue = false;
}

$this->imageAssets = Asset::all()->filter(function ($asset) {
return $asset->isImage();
});
Expand Down Expand Up @@ -97,15 +98,22 @@ private function generatePresets($presets)
{
foreach ($presets as $preset => $params) {
$bar = $this->output->createProgressBar($this->imageAssets->count());
$bar->setFormat("[%current%/%max%] Generating <comment>$preset</comment>... %filename%");

$verb = $this->shouldQueue ? 'Queueing' : 'Generating';
$bar->setFormat("[%current%/%max%] $verb <comment>$preset</comment>... %filename%");

foreach ($this->imageAssets as $asset) {
$bar->setMessage($asset->basename(), 'filename');
$this->generator->generate($asset, $preset);

$dispatchMethod = $this->shouldQueue ? 'dispatch' : 'dispatchSync';
GeneratePresetImageManipulation::$dispatchMethod($asset, $preset);

$bar->advance();
}

$bar->setFormat("<info>[✓] Images generated for <comment>$preset</comment>.</info>");
$verb = $this->shouldQueue ? 'queued' : 'generated';
$bar->setFormat("<info>[✓] Images $verb for <comment>$preset</comment>.</info>");

$bar->finish();

$this->output->newLine();
Expand Down
29 changes: 29 additions & 0 deletions src/Jobs/GeneratePresetImageManipulation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Statamic\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Statamic\Contracts\Assets\Asset;
use Statamic\Imaging\PresetGenerator;

class GeneratePresetImageManipulation implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;

public $asset;
public $preset;

public function __construct(Asset $asset, $preset)
{
$this->asset = $asset;
$this->preset = $preset;
}

public function handle(PresetGenerator $generator)
{
$generator->generate($this->asset, $this->preset);
}
}

0 comments on commit d630244

Please sign in to comment.