studioespresso / craft-scout

Craft Scout provides a simple solution for adding full-text search to your entries. Scout will automatically keep your search indexes in sync with your entries.
MIT License
81 stars 54 forks source link

[STU-74] Triggering an event after a successful refresh on queue jobs? #283

Closed sunscreem closed 1 year ago

sunscreem commented 1 year ago

I'm wondering if anyone has worked out how to trigger an event when an update of the indexes has successfully completed.

The complication is I'm using supervisor to run my queue, so on my local testing this works great... But it doesn't seem to fire in production, I'm guessing because of how Craft runs queue jobs using supervisor?

 Event::on(
            Queue::class,
            Queue::EVENT_AFTER_EXEC_AND_RELEASE,
            function (ExecEvent $event) {
                if (get_class($event->job) === 'rias\scout\jobs\ImportIndex') {

                   Craft::info('testing');

                }
            }
        );

Essentially what I'm trying to do is update a timestamp the last time a cron job to update my scout index is successfully completed.

STU-74

janhenckens commented 1 year ago

@sunscreem Are you triggering a full import using the CLI command? That uses a seperate job class so I could trigger a new "AFTER_INDEX_IMPORT" event there if that could help?

sunscreem commented 1 year ago

Hey @janhenckens - coincidentally I managed to sort this just last night.

The issue was that supervisor needed restarted to pick up my changed code, only I hadn't notice it wasn't doing so in my deployments due to a permissions issue.

This is working a treat now. I use it alongside Oh Dear - Health Check to make sure my indexes are updating on a cron job overnight.

Although not needed, an event such as AFTER_INDEX_IMPORT would make it more specific to scout vs firing for every queue job?

janhenckens commented 1 year ago

Good to hear you got it working!

Not having to check it for every queue job is probably a lot more efficient on your end, I'll put it on the list and let you know when it's available :)

sunscreem commented 1 year ago

Awesome!

janhenckens commented 1 year ago

Had to work on some other issues for the plugin so rolled this in already :) It's out in 3.3.1, you can find documentation here

sunscreem commented 1 year ago

@janhenckens Works perfectly. Thank you!