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 55 forks source link

feat: backport fix for #266 to Craft 3 #288

Closed qrazi closed 9 months ago

qrazi commented 9 months ago

The issue described in #266 also affects Craft 3 version of this plugin. So this PR back-ports the fix as is.

What we usually do in our own custom modules when it comes to potentially off-loading to the queue, is to always run the job. But conditionally on the queue.

So, in this case we would have done:

$job = new IndexElement([
    'id' => $element->id,
    'siteId' => $element->site->id,
]);
$queue = Craft::$app->getQueue();
if (Scout::$plugin->getSettings()->queue) {
    $queue->push($job);
} else {
    $job->execute($queue);
}

This way both paths end up doing the same work. Now if Scout::$plugin->getSettings()->queue === false, the extra work that the Job does does not get executed.

I didn't think I should "pollute" this PR with that, but I could put this in a new PR if interested?

janhenckens commented 9 months ago

Hey @qrazi, that's an interesting approach - I haven't seen that done before. Feel free to make a new PR for that if you'd like!

Meanwhile I'll merge this fix for Craft 3 as well.