Skip to content

Commit

Permalink
Fix event listener registration.
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartcusackie committed Nov 28, 2022
1 parent e68ec32 commit 72a33fa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Artisan commands that queue up entry and glide urls for retrieval, which engages

This package can do a few things:
1. Queue up all entry urls for retrieval which automatically engages the static cache, making first time loads quicker.
2. Search for all image and picture sources within your entries and queue them up for a separate retrieval to initiate Glide generation. This means that all images and responsive variants will be pre-generated for your first visitors. This is particularly useful when you have lots of responsive image variants or if you are using Spatie's Statamic Responsive Images package. It can help to avoid server crashes for image heavy websites where Glide has a lot of processing to perform.
2. Search for all image and picture sources within your entries and queue them up for a separate retrieval to initiate Glide generation. This means that all images and responsive variants will be pre-generated for your first visitor. This is particularly useful when you have lots of responsive image variants or if you are using Spatie's Statamic Responsive Images package. It can help to avoid server crashes for image heavy websites where Glide has a lot of processing to perform.
3. Listens for EntrySaved events and automatically queues the entry's url for image requests.

**CAUTION:** Glide image manipulation can take a lot of work, especially when using responsive image variants and jpeg fallbacks. For example, a site using Spatie's responsive images addon could have 10 sizes and 2 formats per image. If this site has 1000 images then 20,000 variants will need to be manipulated by Glide. Keep an eye on your CPU usage, especially if using a hosting server that limits CPU (e.g. AWS-EC2).
Expand Down Expand Up @@ -51,4 +51,4 @@ The queue can be ran manually with this command:

`php artisan queue:work redis --queue=cacherequester`

I recommend using a Laravel Forge worker, or something similar, as workers are prone to exit prematurely when using the command line.
I recommend using a Laravel Forge worker, or something similar, as workers are prone to exit prematurely when using the command line.
24 changes: 24 additions & 0 deletions src/Listeners/EntrySavedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace stuartcusackie\StatamicCacheRequester\Listeners;

use Statamic\Events\EntrySaved;
use Illuminate\Support\Facades\Log;
use stuartcusackie\StatamicCacheRequester\Jobs\RequestUrl;

class EntrySavedListener
{
public function handle(EntrySaved $event): void
{
if($event->entry->url) {

try{
RequestUrl::dispatch(url($event->entry->url), true);
}
catch(\Throwable $e){
Log::warning('Could not queue saved entry for cache requesting.');
}

}
}
}
22 changes: 4 additions & 18 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Event;
use Statamic\Events\EntrySaved;
use Illuminate\Support\Facades\Log;
use stuartcusackie\StatamicCacheRequester\Jobs\RequestUrl;
use stuartcusackie\StatamicCacheRequester\Listeners\EntrySavedListener;
use stuartcusackie\StatamicCacheRequester\Console\Commands\RequestEntries;
use stuartcusackie\StatamicCacheRequester\Console\Commands\RequestImages;
use stuartcusackie\StatamicCacheRequester\Console\Commands\ClearRequestQueue;
Expand All @@ -20,7 +19,7 @@ public function bootAddon()
{
$this
->registerCommands()
->listen();
->registerListeners();

}

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

protected function listen() {
protected function registerListeners() {

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

if($event->entry->url) {

try{
RequestUrl::dispatch(url($event->entry->url), true);
}
catch(\Throwable $e){
Log::warning('Could not queue saved entry for cache requesting.');
}

}

});
Event::listen(EntrySaved::class, EntrySavedListener::class);

return $this;

Expand Down

0 comments on commit 72a33fa

Please sign in to comment.