XF 2.0 Re-queuing Job after a while

CMTV

Well-known member
I am trying to send emails to all thread watchers. Many hosts have restrictions on amount of emails sent per minute/hour.

@Mike advised me to use jobs system to avoid timeouts.

I am absolutely new to jobs topic and have not found any tutorials but I did understand something.

As I understand, $this->resume(); immediately re-queues job and runs run(...) method.

How to set at least 1 minute delay before rerunning job?
PHP:
public function run($maxRunTime)
{
    $start = microtime(true);

    $db = $this->app->db();
    $query = "SELECT user_id, email_subscribe FROM xf_thread_watch WHERE thread_id = ? ORDER BY user_id";

    /* Getting watchers */
    $watchers = $db->fetchAll(
        $db->limit(
            $query,
            $this->data['amount_to_get'],
            $this->data['limit']
        ),
        $this->data['thread_id']
    );
    if(!$watchers) // All watchers are notified
    {
        return $this->complete();
    }

    /* Iterating through watchers */
    foreach($watchers as $watcher)
    {
        if(microtime(true) - $start >= $maxRunTime)
        {
            break;
        }

        $this->data['limit'] = $watcher['user_id'];

        /* Notifying watcher (sending alert and email) ... */
    }

    /* ============================== */
    /* ? 1 minute delay ? */
    /* ============================== */

    return $this->resume();
}
 
Here is how I managed to create a delay before rerunning:
PHP:
$resume = $this->resume();
$resume->continueDate = intval((new \DateTime('@' . \XF::$time))->modify('+1 minute')->format('U'));
return $resume;

Generally it works. But there is a problem. I noticed that job is not rerunning every minute. Is is rerunning after at least a minute and after performing any action on site...
If there is no people on the forum it just waits for someone to come and reload the page or perform another action. And only ater that it reruns the job.

I don't think that it is the correct behaviour... What am I doing wrong?
 
That is how jobs work. They're triggered by activity. So this is the expected behavior.
 
That is how jobs work. They're triggered by activity. So this is the expected behavior.
Ok. And thread watchers notification system (including sending emails) works the same? I mean is it triggered by activity too?
 
Yes, but we don't do subsequent delays, which means that one user triggering and then reading a thread is likely to run through everything. There are a few other mitigating elements as well (such as running some/potentially all of it when the response is posted and automatically triggering the job for the author to run immediately after their post).
 
Top Bottom