Skip to content

Commit 480bebe

Browse files
authored
Merge pull request #13 from techmahedy/techmahedy-queue
[Feature] Add #[Queueable] Attribute & New Job Dispatching Methods
2 parents 18cc4d5 + 42962f2 commit 480bebe

File tree

6 files changed

+168
-112
lines changed

6 files changed

+168
-112
lines changed

src/Attributes/Queueable.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Doppar\Queue\Attributes;
4+
5+
use Attribute;
6+
7+
#[Attribute(Attribute::TARGET_CLASS)]
8+
class Queueable
9+
{
10+
/**
11+
* @param int|null $tries
12+
* @param int|null $retryAfter
13+
* @param int|null $delayFor
14+
* @param string|null $onQueue
15+
*/
16+
public function __construct(
17+
public ?int $tries = null,
18+
public ?int $retryAfter = null,
19+
public ?int $delayFor = null,
20+
public ?string $onQueue = null
21+
) {}
22+
}

src/Commands/MakeJobCommand.php

Lines changed: 0 additions & 107 deletions
This file was deleted.

src/Dispatchable.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Doppar\Queue;
4+
5+
trait Dispatchable
6+
{
7+
/**
8+
* Dispatch the job with the given arguments.
9+
*
10+
* @param mixed ...$args
11+
* @return string|null Job ID if queued, null if executed immediately
12+
*/
13+
public static function dispatchWith(...$args): ?string
14+
{
15+
return (new static(...$args))->dispatch();
16+
}
17+
18+
/**
19+
* Dispatch the job synchronously with the given arguments.
20+
*
21+
* @param mixed ...$args
22+
* @return void
23+
*/
24+
public static function queueAsSync(...$args): void
25+
{
26+
(new static(...$args))->handle();
27+
}
28+
29+
/**
30+
* Dispatch the job after a delay with the given arguments.
31+
*
32+
* @param int $delay
33+
* @param mixed ...$args
34+
* @return string Job ID
35+
*/
36+
public static function queueAfter(int $delay, ...$args): string
37+
{
38+
return (new static(...$args))->delayFor($delay)->forceQueue();
39+
}
40+
41+
/**
42+
* Dispatch the job to a specific queue with the given arguments.
43+
*
44+
* @param string $queue
45+
* @param mixed ...$args
46+
* @return string Job ID
47+
*/
48+
public static function queueOn(string $queue, ...$args): string
49+
{
50+
return (new static(...$args))->onQueue($queue)->forceQueue();
51+
}
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Doppar\Queue;
4+
5+
use Doppar\Queue\Attributes\Queueable;
6+
7+
trait InteractsWithQueueableAttributes
8+
{
9+
/**
10+
* Check if the job should be queued based on the Queueable attribute.
11+
*
12+
* @return bool
13+
*/
14+
public function shouldQueue(): bool
15+
{
16+
$reflection = new \ReflectionClass($this);
17+
$attributes = $reflection->getAttributes(Queueable::class);
18+
19+
return !empty($attributes);
20+
}
21+
22+
/**
23+
* Apply the Queueable attribute settings to the job.
24+
*
25+
* @return void
26+
*/
27+
protected function applyQueueableAttributes(): void
28+
{
29+
$reflection = new \ReflectionClass($this);
30+
$attributes = $reflection->getAttributes(Queueable::class);
31+
32+
if (!empty($attributes)) {
33+
$attribute = $attributes[0]->newInstance();
34+
35+
if ($attribute->tries !== null) {
36+
$this->tries = $attribute->tries;
37+
}
38+
39+
if ($attribute->retryAfter !== null) {
40+
$this->retryAfter = $attribute->retryAfter;
41+
}
42+
43+
if ($attribute->delayFor !== null) {
44+
$this->jobDelay = $attribute->delayFor;
45+
}
46+
47+
if ($attribute->onQueue !== null) {
48+
$this->queueName = $attribute->onQueue;
49+
}
50+
}
51+
}
52+
}

src/Job.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
abstract class Job implements JobInterface
99
{
10+
use InteractsWithQueueableAttributes;
11+
1012
/**
1113
* The number of times the job may be attempted.
1214
*
@@ -150,11 +152,19 @@ public function delayFor(int $delay): self
150152
/**
151153
* Dispatch the job to the queue.
152154
*
153-
* @return string Job ID
155+
* @return string|null
154156
*/
155-
public function dispatch(): string
157+
public function dispatch(): ?string
156158
{
157-
return Queue::push($this);
159+
$this->applyQueueableAttributes();
160+
161+
if ($this->shouldQueue()) {
162+
return Queue::push($this);
163+
}
164+
165+
$this->handle();
166+
167+
return null;
158168
}
159169

160170
/**
@@ -167,6 +177,8 @@ public function dispatchAfter(int $delay): string
167177
{
168178
$this->delayFor($delay);
169179

180+
$this->applyQueueableAttributes();
181+
170182
return $this->dispatch();
171183
}
172184

@@ -180,6 +192,8 @@ public function dispatchOn(string $queue): string
180192
{
181193
$this->onQueue($queue);
182194

195+
$this->applyQueueableAttributes();
196+
183197
return $this->dispatch();
184198
}
185199

@@ -195,4 +209,29 @@ public static function dispatchNow(...$args): string
195209

196210
return $job->dispatch();
197211
}
212+
213+
/**
214+
* Dispatch the job synchronously
215+
*
216+
* @param mixed ...$args
217+
* @return void
218+
*/
219+
public static function dispatchSync(...$args): void
220+
{
221+
$job = new static(...$args);
222+
223+
$job->handle();
224+
}
225+
226+
/**
227+
* Force the job to be queued even without Queueable attribute.
228+
*
229+
* @return string Job ID
230+
*/
231+
public function forceQueue(): string
232+
{
233+
$this->applyQueueableAttributes();
234+
235+
return Queue::push($this);
236+
}
198237
}

src/QueueServiceProvider.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Doppar\Queue\Commands\QueueRetryCommand;
99
use Doppar\Queue\Commands\QueueFlushCommand;
1010
use Doppar\Queue\Commands\QueueFailedCommand;
11-
use Doppar\Queue\Commands\MakeJobCommand;
1211
use Doppar\Queue\Commands\QueueMonitorCommand;
1312

1413
class QueueServiceProvider extends ServiceProvider
@@ -39,7 +38,6 @@ public function boot(): void
3938
$this->commands([
4039
QueueRunCommand::class,
4140
QueueRetryCommand::class,
42-
MakeJobCommand::class,
4341
QueueFlushCommand::class,
4442
QueueFailedCommand::class,
4543
QueueMonitorCommand::class

0 commit comments

Comments
 (0)