Skip to content

Commit d14ee72

Browse files
authored
Merge pull request #7 from doppar/queue
dispatch method for chainable way to dispatch a queue job:
2 parents 041f104 + 0eab690 commit d14ee72

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/Job.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Doppar\Queue;
44

5+
use Doppar\Queue\Facades\Queue;
56
use Doppar\Queue\Contracts\JobInterface;
67

78
abstract class Job implements JobInterface
@@ -145,4 +146,53 @@ public function delayFor(int $delay): self
145146

146147
return $this;
147148
}
149+
150+
/**
151+
* Dispatch the job to the queue.
152+
*
153+
* @return string Job ID
154+
*/
155+
public function dispatch(): string
156+
{
157+
return Queue::push($this);
158+
}
159+
160+
/**
161+
* Dispatch the job to the queue after a delay.
162+
*
163+
* @param int $delay Delay in seconds
164+
* @return string Job ID
165+
*/
166+
public function dispatchAfter(int $delay): string
167+
{
168+
$this->delayFor($delay);
169+
170+
return $this->dispatch();
171+
}
172+
173+
/**
174+
* Dispatch the job to a specific queue.
175+
*
176+
* @param string $queue
177+
* @return string Job ID
178+
*/
179+
public function dispatchOn(string $queue): string
180+
{
181+
$this->onQueue($queue);
182+
183+
return $this->dispatch();
184+
}
185+
186+
/**
187+
* Dispatch the job.
188+
*
189+
* @param mixed ...$args
190+
* @return string Job ID
191+
*/
192+
public static function dispatchNow(...$args): string
193+
{
194+
$job = new static(...$args);
195+
196+
return $job->dispatch();
197+
}
148198
}

0 commit comments

Comments
 (0)