Skip to content

Commit

Permalink
Add entry saved event processing.
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartcusackie committed Aug 29, 2022
1 parent d026518 commit a123ee2
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 28 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

This Statamic utility searches images and picture sources in your Statamic Entries and adds each url to a Redis queue. The queue can then be processed to retrieve each url and initiate Glide generation.

The purpose of this package is to pre-generate all website images, and alleviate some of the pressure that image-heavy websites put on the server. You will usually only run these commands once on initial deployment of the site, or after any major restructuring of asset filenames and folders.
The purpose of this package is to pre-generate all website images, and alleviate some of the pressure that image-heavy websites put on the server. This is particularly useful when you have lots of responsive image variants or if you are using Spatie's Statamic Responsive Images package.

You will usually only run these commands once on initial deployment of the site, or after any major restructuring of asset filenames and folders.

The package also listens for EntrySaved events and automatically queues the entry url for processing.


## Installation
Expand All @@ -24,7 +28,7 @@ This package utilises a Redis queue called **gliderequester**. You must have Red

## Usage

This package provides an artisan command that be used like so
A control panel utility is provided for easy usage. An artisan command is also provided and can be used like so:

`php artisan glide:request`

Expand Down
2 changes: 1 addition & 1 deletion config/statamic-glide-requester.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
| size for a responsive image, in which case the values below might be
|
| 'post_data_attributes' => [
| 'data-img-id',
| 'data-asset-id',
| ],
|
| 'asset_view_path' => '/lightbox-image-asset'
Expand Down
6 changes: 3 additions & 3 deletions resources/views/index.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@extends('statamic::layout')
@section('title', __('Rebuild Search'))
@section('title', __('Glide Requester'))

@section('content')

Expand All @@ -15,8 +15,8 @@

<div>
<div class="mb-4">
<p class="mb-2">Click the button to run the artisan command and it will search for all pictures and image elements in your entries and assets and queue them up for retrieval. This should greatly reduce initial page load times when lots of new images have been added.</p>
<p>Don't forget to set up your config file and make sure your redis worker is running on the <strong>gliderequester</strong> queue.</p>
<p class="mb-2">Click the button to queue all entries for image processing. You should only do this when lots of new images are added to multiple entries.</p>
<p>This action will clear the current queue. Don't forget to set up your config file and make sure your redis worker is running on the <strong>gliderequester</strong> queue.</p>
</div>

<form method="POST" action="{{ cp_route('utilities.glide-requester.run') }}">
Expand Down
16 changes: 2 additions & 14 deletions src/Console/Commands/RequestGlideImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
namespace stuartcusackie\StatamicGlideRequester\Console\Commands;

use Illuminate\Console\Command;
use Statamic\Facades\Entry;
use Statamic\Facades\Asset;
use stuartcusackie\StatamicGlideRequester\Jobs\FindElementsAtUrl;
use stuartcusackie\StatamicGlideRequester\StatamicGlideRequester;

class RequestGlideImages extends Command
{
Expand Down Expand Up @@ -35,17 +33,7 @@ class RequestGlideImages extends Command
*/
public function handle()
{
$this->call('queue:clear', ['connection' => 'redis', '--queue' => 'gliderequester']);

foreach(Entry::all() as $entry) {

if($entry->url) {
FindElementsAtUrl::dispatch(url($entry->url));
$this->urls++;
}
}

$this->info($this->urls . ' urls queued for processing. You can now run the gliderequester queue on redis.');
StatamicGlideRequester::queueAllEntries();

return 0;
}
Expand Down
7 changes: 3 additions & 4 deletions src/Http/Controllers/GlideRequesterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Http\Request;
use Statamic\Http\Controllers\Controller;
use Artisan;
use stuartcusackie\StatamicGlideRequester\StatamicGlideRequester;

class GlideRequesterController extends Controller
{
Expand All @@ -28,9 +28,8 @@ public function show(Request $request)
*/
public function run(Request $request)
{
Artisan::call('queue:clear', ['connection' => 'redis', '--queue' => 'gliderequester', '--force' => true]);
Artisan::queue('glide:request')->onConnection('redis')->onQueue('gliderequester');
StatamicGlideRequester::queueAllEntries();

return back();
return back()->withSuccess(__('All entry urls have been queued for processing.'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
use Illuminate\Support\Facades\Http;
use simplehtmldom\HtmlDocument;
use Illuminate\Support\Str;
use stuartcusackie\StatamicGlideRequester\Jobs\VisitGlideUrl;
use stuartcusackie\StatamicGlideRequester\Jobs\VisitGlideSource;
use Illuminate\Support\Facades\Log;

class FindElementsAtUrl implements ShouldQueue
class QueueGlideSources implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

Expand Down Expand Up @@ -132,7 +132,7 @@ protected function processElement($el) {

if(Str::startsWith($path, '/img/')) {
Log::info('Adding glide visit for url: ' . url($path));
VisitGlideUrl::dispatch(url($path));
VisitGlideSource::dispatch(url($path));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class VisitGlideUrl implements ShouldQueue
class VisitGlideSource implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

Expand Down
21 changes: 21 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
use Statamic\Providers\AddonServiceProvider;
use Statamic\Facades\Utility;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Event;
use Statamic\Events\EntrySaved;
use stuartcusackie\StatamicGlideRequester\StatamicGlideRequester;
use stuartcusackie\StatamicGlideRequester\Http\Controllers\GlideRequesterController;
use stuartcusackie\StatamicGlideRequester\Console\Commands\RequestGlideImages;
use Illuminate\Support\Facades\Log;

class ServiceProvider extends AddonServiceProvider
{
Expand All @@ -17,6 +21,7 @@ public function bootAddon()

$this
->registerCommands()
->listen()
->makeUtility();

}
Expand All @@ -30,6 +35,22 @@ protected function registerCommands()
return $this;
}

protected function listen() {

Event::listen(function (EntrySaved $event) {

Log::info('Saved entry queued for glide requests');

if($event->entry->url) {
StatamicGlideRequester::queueUrl(url($event->entry->url));
}

});

return $this;

}

protected function makeUtility() {

Utility::make('glide-requester')
Expand Down
38 changes: 38 additions & 0 deletions src/StatamicGlideRequester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace stuartcusackie\StatamicGlideRequester;

use Statamic\Facades\Entry;
use stuartcusackie\StatamicGlideRequester\Jobs\QueueGlideSources;
use Artisan;

class StatamicGlideRequester {

/**
* Dispatch a job
*
* @param string $url
* @return void
*/
public static function queueUrl($url) {
QueueGlideSources::dispatch($url);
}

/**
* Request
*
* @return void
*/
public static function queueAllEntries() {

Artisan::call('queue:clear', ['connection' => 'redis', '--queue' => 'gliderequester', '--force' => true]);

foreach(Entry::all() as $entry) {

if($entry->url) {
QueueGlideSources::dispatch(url($entry->url));
}
}
}

}

0 comments on commit a123ee2

Please sign in to comment.