Skip to content

Commit 2967b6c

Browse files
committed
add queue methods to inspect jobs
queue methods to inspect jobs squash it to save viewing pain doc Update InspectedJob.php csx2 cs wip tests and clean up
1 parent 1da2839 commit 2967b6c

12 files changed

Lines changed: 501 additions & 0 deletions

File tree

src/Illuminate/Queue/BeanstalkdQueue.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Contracts\Queue\Queue as QueueContract;
66
use Illuminate\Queue\Jobs\BeanstalkdJob;
7+
use Illuminate\Support\Collection;
78
use Pheanstalk\Contract\JobIdInterface;
89
use Pheanstalk\Pheanstalk;
910
use Pheanstalk\Values\Job;
@@ -111,6 +112,39 @@ public function reservedSize($queue = null)
111112
return $this->pheanstalk->statsTube(new TubeName($this->getQueue($queue)))->currentJobsReserved;
112113
}
113114

115+
/**
116+
* Get the pending jobs for the given queue.
117+
*
118+
* @param string|null $queue
119+
* @return \Illuminate\Support\Collection
120+
*/
121+
public function pendingJobs($queue = null): Collection
122+
{
123+
return new Collection;
124+
}
125+
126+
/**
127+
* Get the delayed jobs for the given queue.
128+
*
129+
* @param string|null $queue
130+
* @return \Illuminate\Support\Collection
131+
*/
132+
public function delayedJobs($queue = null): Collection
133+
{
134+
return new Collection;
135+
}
136+
137+
/**
138+
* Get the reserved jobs for the given queue.
139+
*
140+
* @param string|null $queue
141+
* @return \Illuminate\Support\Collection
142+
*/
143+
public function reservedJobs($queue = null): Collection
144+
{
145+
return new Collection;
146+
}
147+
114148
/**
115149
* Get the creation timestamp of the oldest pending job, excluding delayed jobs.
116150
*

src/Illuminate/Queue/DatabaseQueue.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Connection;
88
use Illuminate\Queue\Jobs\DatabaseJob;
99
use Illuminate\Queue\Jobs\DatabaseJobRecord;
10+
use Illuminate\Queue\Jobs\InspectedJob;
1011
use Illuminate\Support\Carbon;
1112
use Illuminate\Support\Collection;
1213
use Illuminate\Support\Str;
@@ -131,6 +132,53 @@ public function reservedSize($queue = null)
131132
->count();
132133
}
133134

135+
/**
136+
* Get the pending jobs for the given queue.
137+
*
138+
* @param string|null $queue
139+
* @return \Illuminate\Support\Collection<int, \Illuminate\Queue\Jobs\InspectedJob>
140+
*/
141+
public function pendingJobs($queue = null): Collection
142+
{
143+
return $this->database->table($this->table)
144+
->where('queue', $this->getQueue($queue))
145+
->whereNull('reserved_at')
146+
->where('available_at', '<=', $this->currentTime())
147+
->get()
148+
->map(fn ($record) => InspectedJob::fromPayload($record->payload, $record->queue, $record->attempts));
149+
}
150+
151+
/**
152+
* Get the delayed jobs for the given queue.
153+
*
154+
* @param string|null $queue
155+
* @return \Illuminate\Support\Collection<int, \Illuminate\Queue\Jobs\InspectedJob>
156+
*/
157+
public function delayedJobs($queue = null): Collection
158+
{
159+
return $this->database->table($this->table)
160+
->where('queue', $this->getQueue($queue))
161+
->whereNull('reserved_at')
162+
->where('available_at', '>', $this->currentTime())
163+
->get()
164+
->map(fn ($record) => InspectedJob::fromPayload($record->payload, $record->queue, $record->attempts));
165+
}
166+
167+
/**
168+
* Get the reserved jobs for the given queue.
169+
*
170+
* @param string|null $queue
171+
* @return \Illuminate\Support\Collection<int, \Illuminate\Queue\Jobs\InspectedJob>
172+
*/
173+
public function reservedJobs($queue = null): Collection
174+
{
175+
return $this->database->table($this->table)
176+
->where('queue', $this->getQueue($queue))
177+
->whereNotNull('reserved_at')
178+
->get()
179+
->map(fn ($record) => InspectedJob::fromPayload($record->payload, $record->queue, $record->attempts));
180+
}
181+
134182
/**
135183
* Get the creation timestamp of the oldest pending job, excluding delayed jobs.
136184
*

src/Illuminate/Queue/FailoverQueue.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
66
use Illuminate\Contracts\Queue\Queue as QueueContract;
77
use Illuminate\Queue\Events\QueueFailedOver;
8+
use Illuminate\Support\Collection;
89
use RuntimeException;
910
use Throwable;
1011

@@ -71,6 +72,39 @@ public function reservedSize($queue = null)
7172
return $this->manager->connection($this->connections[0])->reservedSize($queue);
7273
}
7374

75+
/**
76+
* Get the pending jobs for the given queue.
77+
*
78+
* @param string|null $queue
79+
* @return \Illuminate\Support\Collection
80+
*/
81+
public function pendingJobs($queue = null): Collection
82+
{
83+
return $this->manager->connection($this->connections[0])->pendingJobs($queue);
84+
}
85+
86+
/**
87+
* Get the delayed jobs for the given queue.
88+
*
89+
* @param string|null $queue
90+
* @return \Illuminate\Support\Collection
91+
*/
92+
public function delayedJobs($queue = null): Collection
93+
{
94+
return $this->manager->connection($this->connections[0])->delayedJobs($queue);
95+
}
96+
97+
/**
98+
* Get the reserved jobs for the given queue.
99+
*
100+
* @param string|null $queue
101+
* @return \Illuminate\Support\Collection
102+
*/
103+
public function reservedJobs($queue = null): Collection
104+
{
105+
return $this->manager->connection($this->connections[0])->reservedJobs($queue);
106+
}
107+
74108
/**
75109
* Get the creation timestamp of the oldest pending job, excluding delayed jobs.
76110
*
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Illuminate\Queue\Jobs;
4+
5+
use Illuminate\Support\Carbon;
6+
7+
class InspectedJob
8+
{
9+
/**
10+
* Create a new inspected job instance.
11+
*
12+
* @param string|null $name The display name of the job.
13+
* @param string|null $queue The name of the queue the job belongs to.
14+
* @param int $attempts The number of times the job has been attempted.
15+
* @param string|null $uuid The unique identifier for the job.
16+
* @param \Illuminate\Support\Carbon|null $createdAt The date and time the job was created.
17+
*/
18+
public function __construct(
19+
public readonly ?string $name,
20+
public readonly ?string $queue,
21+
public readonly int $attempts,
22+
public readonly ?string $uuid,
23+
public readonly ?Carbon $createdAt,
24+
) {
25+
}
26+
27+
/**
28+
* Create a new instance from a raw job payload.
29+
*
30+
* @param string $payload The raw JSON job payload.
31+
* @param string $queue The name of the queue the job belongs to.
32+
* @param int|null $attempts The number of times the job has been attempted.
33+
* @return static
34+
*/
35+
public static function fromPayload(string $payload, string $queue, ?int $attempts = null): static
36+
{
37+
$decoded = json_decode($payload, true);
38+
39+
return new static(
40+
name: $decoded['displayName'] ?? null,
41+
queue: $queue,
42+
attempts: $attempts ?? $decoded['attempts'] ?? 0,
43+
uuid: $decoded['uuid'] ?? null,
44+
createdAt: isset($decoded['createdAt']) ? Carbon::createFromTimestamp($decoded['createdAt']) : null,
45+
);
46+
}
47+
}

src/Illuminate/Queue/NullQueue.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Queue;
44

55
use Illuminate\Contracts\Queue\Queue as QueueContract;
6+
use Illuminate\Support\Collection;
67

78
class NullQueue extends Queue implements QueueContract
89
{
@@ -50,6 +51,39 @@ public function reservedSize($queue = null)
5051
return 0;
5152
}
5253

54+
/**
55+
* Get the pending jobs for the given queue.
56+
*
57+
* @param string|null $queue
58+
* @return \Illuminate\Support\Collection
59+
*/
60+
public function pendingJobs($queue = null): Collection
61+
{
62+
return new Collection;
63+
}
64+
65+
/**
66+
* Get the delayed jobs for the given queue.
67+
*
68+
* @param string|null $queue
69+
* @return \Illuminate\Support\Collection
70+
*/
71+
public function delayedJobs($queue = null): Collection
72+
{
73+
return new Collection;
74+
}
75+
76+
/**
77+
* Get the reserved jobs for the given queue.
78+
*
79+
* @param string|null $queue
80+
* @return \Illuminate\Support\Collection
81+
*/
82+
public function reservedJobs($queue = null): Collection
83+
{
84+
return new Collection;
85+
}
86+
5387
/**
5488
* Get the creation timestamp of the oldest pending job, excluding delayed jobs.
5589
*

src/Illuminate/Queue/RedisQueue.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
use Illuminate\Contracts\Queue\ClearableQueue;
66
use Illuminate\Contracts\Queue\Queue as QueueContract;
77
use Illuminate\Contracts\Redis\Factory as Redis;
8+
use Illuminate\Queue\Jobs\InspectedJob;
89
use Illuminate\Queue\Jobs\RedisJob;
910
use Illuminate\Redis\Connections\PhpRedisClusterConnection;
1011
use Illuminate\Redis\Connections\PredisClusterConnection;
12+
use Illuminate\Support\Collection;
1113
use Illuminate\Support\Str;
1214

1315
class RedisQueue extends Queue implements QueueContract, ClearableQueue
@@ -142,6 +144,48 @@ public function reservedSize($queue = null)
142144
return $this->getConnection()->zcard($this->getQueue($queue).':reserved');
143145
}
144146

147+
/**
148+
* Get the pending jobs for the given queue.
149+
*
150+
* @param string|null $queue
151+
* @return \Illuminate\Support\Collection<int, \Illuminate\Queue\Jobs\InspectedJob>
152+
*/
153+
public function pendingJobs($queue = null): Collection
154+
{
155+
$queue = $this->getQueue($queue);
156+
157+
return (new Collection($this->getConnection()->lrange($queue, 0, -1)))
158+
->map(fn ($payload) => InspectedJob::fromPayload($payload, $queue));
159+
}
160+
161+
/**
162+
* Get the delayed jobs for the given queue.
163+
*
164+
* @param string|null $queue
165+
* @return \Illuminate\Support\Collection<int, \Illuminate\Queue\Jobs\InspectedJob>
166+
*/
167+
public function delayedJobs($queue = null): Collection
168+
{
169+
$queue = $this->getQueue($queue);
170+
171+
return (new Collection($this->getConnection()->zrange($queue.':delayed', 0, -1)))
172+
->map(fn ($payload) => InspectedJob::fromPayload($payload, $queue));
173+
}
174+
175+
/**
176+
* Get the reserved jobs for the given queue.
177+
*
178+
* @param string|null $queue
179+
* @return \Illuminate\Support\Collection<int, \Illuminate\Queue\Jobs\InspectedJob>
180+
*/
181+
public function reservedJobs($queue = null): Collection
182+
{
183+
$queue = $this->getQueue($queue);
184+
185+
return (new Collection($this->getConnection()->zrange($queue.':reserved', 0, -1)))
186+
->map(fn ($payload) => InspectedJob::fromPayload($payload, $queue));
187+
}
188+
145189
/**
146190
* Get the creation timestamp of the oldest pending job, excluding delayed jobs.
147191
*

src/Illuminate/Queue/SqsQueue.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Contracts\Queue\ClearableQueue;
77
use Illuminate\Contracts\Queue\Queue as QueueContract;
88
use Illuminate\Queue\Jobs\SqsJob;
9+
use Illuminate\Support\Collection;
910
use Illuminate\Support\Str;
1011

1112
class SqsQueue extends Queue implements QueueContract, ClearableQueue
@@ -133,6 +134,39 @@ public function reservedSize($queue = null)
133134
return (int) $response['Attributes']['ApproximateNumberOfMessagesNotVisible'] ?? 0;
134135
}
135136

137+
/**
138+
* Get the pending jobs for the given queue.
139+
*
140+
* @param string|null $queue
141+
* @return \Illuminate\Support\Collection
142+
*/
143+
public function pendingJobs($queue = null): Collection
144+
{
145+
return new Collection;
146+
}
147+
148+
/**
149+
* Get the delayed jobs for the given queue.
150+
*
151+
* @param string|null $queue
152+
* @return \Illuminate\Support\Collection
153+
*/
154+
public function delayedJobs($queue = null): Collection
155+
{
156+
return new Collection;
157+
}
158+
159+
/**
160+
* Get the reserved jobs for the given queue.
161+
*
162+
* @param string|null $queue
163+
* @return \Illuminate\Support\Collection
164+
*/
165+
public function reservedJobs($queue = null): Collection
166+
{
167+
return new Collection;
168+
}
169+
136170
/**
137171
* Get the creation timestamp of the oldest pending job, excluding delayed jobs.
138172
*

0 commit comments

Comments
 (0)