<p>It's not entirely clear what your goal is, but when a job is pushed onto the queue, a job <code>id</code> is returned which you can store and later query.</p>
<pre class="lang-php prettyprint-override"><code>$jobId = Craft::$app-&gt;getQueue()-&gt;push($job);

$jobDetails = Craft::$app-&gt;getQueue()-&gt;getJobDetails($jobId);
</code></pre>
<p>If you want to know whether the queue has pending or running jobs, you can use the <code>getHasWaitingJobs()</code> and <code>getHasReservedJobs()</code> methods respectively.</p>
<pre class="lang-php prettyprint-override"><code>// Returns `true` if there are pending jobs
Craft::$app-&gt;getQueue()-&gt;getHasWaitingJobs();

// Returns `true` if there are running jobs
Craft::$app-&gt;getQueue()-&gt;getHasReservedJobs();
</code></pre>
<p>See the Queue component for all available methods:<br />
<a href="https://github.com/craftcms/cms/blob/v3/src/queue/Queue.php" rel="nofollow noreferrer">https://github.com/craftcms/cms/blob/v3/src/queue/Queue.php</a></p>

CC BY-SA 4.0
2022-06-24T16:27:55.010Z
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.
James White
CC BY-SA 4.0
2022-06-24T16:50:06.557Z
@JamesWhite I've updated my answer tith some new methods that should hopefully help you.
Ben Croker
CC BY-SA 4.0
2022-06-24T17:08:13.327Z
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.
James White
CC BY-SA 4.0
2022-06-24T19:36:15.843Z
Yeah I guess so.
Ben Croker
CC BY-SA 4.0
2022-06-24T16:11:13.103Z
2022-06-24T16:49:40.763Z
2022-06-24T16:49:40.763Z
Ben Croker
<p>In Craft 2, you used to be able to do something like this to find pending tasks:</p>
<pre class="lang-php prettyprint-override"><code>craft()-&gt;tasks-&gt;getPendingTasks('MyPlugin_CustomTask')
</code></pre>
<p>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.</p>
<p>Using the Queue service I know I can get the current queue jobs without knowing any ID using getJobInfo():</p>
<pre class="lang-php prettyprint-override"><code>$queue = Craft::$app-&gt;getQueue();
$jobs = $queue-&gt;getJobInfo();
</code></pre>
<p>$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?</p>
<pre class="lang-php prettyprint-override"><code>foreach ($jobs as $job) {
    if($job['description'] === 'something') {
         // Do something
    }
}
</code></pre>
<p>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.</p>
<pre class="lang-php prettyprint-override"><code>$jobsQuery = (new Query())
            -&gt;select('id')
            -&gt;from(Table::QUEUE)
            -&gt;where(['description', 'My job description'])
            -&gt;all();

$jobsCount = count($jobsQuery) &gt; 0;

if ($jobsCount) {
    // Do something
}
</code></pre>
<p>Although both methods don't really seem that elegant to me.</p>
<p>Any suggestions of a better way or approach to this?</p>

<craft3><queue>
Best way to find current/pending jobs without knowing their ID in the queue
# Best way to find current/pending jobs without knowing their ID in the queue

(Asked by: username_0 on 2022-06-24)

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?

**Tags:** craft3, queue


---

## 1 answers

### Answer by username_1 on 2022-06-24. Score: 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

> **Comments:**
> **username_0**: 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.  
> **username_1**: @JamesWhite I've updated my answer tith some new methods that should hopefully help you.  
> **username_0**: 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.  
> **username_1**: Yeah I guess so.  

---

