4

I've noticed that tasks only run when logged into the admin panel, is there anyway to avoid this?

For some context: I have jobs scheduled on a cron to run every 30 minutes, when I log in it seems that all of the items get queued up from the last time I logged in and proceed to run, this isn't terrible - but this app won't be seeing much login activity down the road.

`public function execute($queue)
{
    $section = Craft::$app->sections->getSectionByHandle('orders');
    $entryTypes = $section->getEntryTypes();
    $entryType = reset($entryTypes);

// Create an entry
    $entry = new Entry([
        'sectionId' => $section->id,
        'typeId' => $entryType->id,
        'fieldLayoutId' => $entryType->fieldLayoutId,
        'authorId' => 1,
        'title' => 'Order Number: ' . $this->orderId,
        'slug' => $this->orderId,
        'postDate' => new DateTime(),
    ]);


    $entry->setFieldValues([
        'orderId' => $this->orderId,
        'lineItemMatrix' => $this->lineItemMatrix,
        'couponMatrix' => $this->couponMatrix,
        'amountPayable' => $this->amountPayable,
        'orderTotal' => $this->orderTotal,
        'tax' => $this->tax,
        'sellerDiscount' => $this->sellerDiscount,
        'shipping' => $this->shipping
    ]);
    //check to see if entry exists.
    $existing = Entry::find()->section('orders')->slug($entry->slug)->one();

    if (is_null($existing)){
        return Craft::$app->elements->saveElement($entry);
    } else {
       // don't do anything;
        return false;
    }
}`
5
  • Tasks should run even when not logged in, would you be able to post the code of the task you have written? – tekstrand Nov 30 '18 at 17:13
  • @TheEks - edited to question to have the code for the task. – Brett Moore Nov 30 '18 at 17:55
  • 2
    A good way to do this is to just disable the runQueueAutomatically and from the CLI start up ./craft queue/listen which starts a process that polls the queue, and runs things as needed; see yiisoft/yii2-queue. – andrew.welch Nov 30 '18 at 21:19
  • It sounds like you're comfortable with cron tasks already—you can also run a similar command to the one @andrew.welch recommended (./craft queue/run) from a cron task. The listen method is appropriate when you're OK daemonizing it (or, as on Heroku, you have a "worker" dyno), but if the setup is daunting, cron would be totally suitable. – August Miller Dec 11 '18 at 22:09
  • Additionally, as @TheEks said: you should be getting a snippet of JS that sends an XHR request to the queue controller, in any page (just before the closing body tag), if there are pending jobs in the queue… – August Miller Dec 11 '18 at 22:12
5

To reiterate what has been said in the comments above and to present an answer, the best way to ensure that queue jobs are completed even without the CMS being logged into or the site being visited is using a scheduled cron job.

The following will run all jobs in the queue every 10 minutes:

*/10 * * * * /usr/bin/php /path/to/craft queue/run
2
  • Is this still a valid solution? I have scoured the "Robust queue job handling in Craft CMS" article at nystudio107.com and there are a number of somewhat complex solutions to this. But if I can just manage this with a simple cron, that would be ideal. – gjhead Mar 3 at 2:25
  • 1
    Yes, it should still be valid. – Ben Croker Mar 3 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.