I have a Plugin\QueueWorker
that creates a queue of emails to send from nodes that are created/updated throughout the day. I've defined a processItem()
method, as well as an arbitrary function (let's call it mymodule_add_to_queue()
) that runs $queue->createItem()
to add the node to the queue. When cron runs, all the items in the queue get run, which is fine and dandy.
The only problem is that cron is running every hour, and it seems the QueueWorker
class will run the process every time, when I only want it to run once, say between 2am and 3am.
I have the code that checks the REQUEST_TIME
and the state of the last time it ran and all that, however I don't know where to put it. Even if it goes into hook_cron()
, it doesn't appear to make any difference to the default behaviour.
Pseudocode for what kind of thing I'd be expecting to happen is:
<?php
// @file src/Plugin/QueueWorker/MyModuleCronQueueWorkerEmailSend.php
class MyModuleCronQueueWorkerEmailSend extends QueueWorkerBase {
public function __construct () {
this.auto_cron = FALSE;
}
public function processItem() {
doSomething();
}
}
// @file mymodule.module
function mymodule_add_to_queue() {
$queue = \Drupal::queue('mymodule_queue_worker_email_send');
$queue->createItem(['foo' => 'bar']);
}
function mymodule_cron() {
if (some_cron_conditionals()) {
$queue = \Drupal::queue('mymodule_queue_worker_email_send');
$queue->process();
}
}
... this may be completely out of scope though, or there may be a really obvious other method I'm missing. Any thoughts? As always, thanks in advance :)