Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the assets:generate-presets command queue friendly #3490

Merged
merged 3 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}