• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 2.0 Re-queuing Job after a while

#1
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();
}
 
#3
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?
 

Mike

XenForo developer
Staff member
#6
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).