1

In Craft 2, you used to be able to do something like this to find pending tasks:

craft()->tasks->getPendingTasks('MyPlugin_CustomTask')

In Craft 3 we of course now have the Queue, however there doesn't appear to be any specific methods like this for queue jobs given it's a bit different.

Using the Queue service I know I can get the current queue jobs without knowing any ID using getJobInfo():

$queue = Craft::$app->getQueue();
$jobs = $queue->getJobInfo();

$jobs is an array I can loop through to find any jobs I might be interested in. The description could be used but is there a better way?

foreach ($jobs as $job) {
    if($job['description'] === 'something') {
         // Do something
    }
}

Alternatively, I could do a DB query on the queue table without looping, if I just wanted to get the count being greater than 0 as an e example.

$jobsQuery = (new Query())
            ->select('id')
            ->from(Table::QUEUE)
            ->where(['description', 'My job description'])
            ->all();

$jobsCount = count($jobsQuery) > 0;

if ($jobsCount) {
    // Do something
}

Although both methods don't really seem that elegant to me.

Any suggestions of a better way or approach to this?

1 Answer 1

1

It's not entirely clear what your goal is, but when a job is pushed onto the queue, a job id is returned which you can store and later query.

$jobId = Craft::$app->getQueue()->push($job);

$jobDetails = Craft::$app->getQueue()->getJobDetails($jobId);

If you want to know whether the queue has pending or running jobs, you can use the getHasWaitingJobs() and getHasReservedJobs() methods respectively.

// Returns `true` if there are pending jobs
Craft::$app->getQueue()->getHasWaitingJobs();

// Returns `true` if there are running jobs
Craft::$app->getQueue()->getHasReservedJobs();

See the Queue component for all available methods:
https://github.com/craftcms/cms/blob/v3/src/queue/Queue.php

4
  • It is more just to make sure excessive amounts of queue jobs don't get created. Let's say I have a console command which runs every hour or so to queue several feeds, sometimes one of the feeds might run longer than others, so to prevent a backlog, I want to be able to prevent a new queue job from being created, so this is why I want to use the queue job info/DB rows to know if there's either a current job or one pending, as this will be done as a pre-check before allowing another to be queued. As it's several feeds, it needs to check the specific feed name related, not all feeds. Jun 24 at 16:27
  • @JamesWhite I've updated my answer tith some new methods that should hopefully help you.
    – Ben Croker
    Jun 24 at 16:50
  • Thank you for the additional edits, in this case I think I only have the option of using getJobInfo() or DB query, given the queue will be checked at random times and I'd need to compare against something like the description to match the queue items I'd be interested in. Jun 24 at 17:08
  • Yeah I guess so.
    – Ben Croker
    Jun 24 at 19:36

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.