Skip to content
This repository was archived by the owner on Mar 9, 2024. It is now read-only.

Commit 738de1b

Browse files
committed
Move delay to command
1 parent 9905573 commit 738de1b

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

src/AsyncQueue.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ public function __construct(array $config)
2828
*/
2929
public function push($job, $data = '', $queue = null)
3030
{
31-
$id = $this->storeJob($job, $data);
31+
$id = $this->storeJob($job, $data, 0);
3232
$this->startProcess($id, 0);
3333

34-
return 0;
34+
return $id;
3535
}
3636

3737
/**
@@ -62,31 +62,33 @@ public function storeJob($job, $data, $delay = 0)
6262
* Make a Process for the Artisan command for the job id.
6363
*
6464
* @param int $jobId
65+
* @param int $delay
6566
*
6667
* @return void
6768
*/
68-
public function startProcess($jobId)
69+
public function startProcess($jobId, $delay = 0)
6970
{
7071
chdir($this->container['path.base']);
71-
exec($this->getCommand($jobId));
72+
exec($this->getCommand($jobId, $delay));
7273
}
7374

7475
/**
7576
* Get the Artisan command as a string for the job id.
7677
*
7778
* @param int $jobId
79+
* @param int $delay
7880
*
7981
* @return string
8082
*/
81-
protected function getCommand($jobId)
83+
protected function getCommand($jobId, $delay = 0)
8284
{
83-
$cmd = '%s artisan queue:async %d --env=%s';
85+
$cmd = '%s artisan queue:async %d --env=%s --delay=%d';
8486
$cmd = $this->getBackgroundCommand($cmd);
8587

8688
$binary = $this->getPhpBinary();
8789
$environment = $this->container->environment();
8890

89-
return sprintf($cmd, $binary, $jobId, $environment);
91+
return sprintf($cmd, $binary, $jobId, $environment, $delay);
9092
}
9193

9294
/**
@@ -127,9 +129,9 @@ public function later($delay, $job, $data = '', $queue = null)
127129
{
128130
$delay = $this->getSeconds($delay);
129131
$id = $this->storeJob($job, $data, $delay);
130-
$this->startProcess($id);
132+
$this->startProcess($id, $delay);
131133

132-
return 0;
134+
return $id;
133135
}
134136

135137
}

src/Console/AsyncCommand.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Barryvdh\Queue\Models\Job;
77
use Illuminate\Console\Command;
88
use Symfony\Component\Console\Input\InputArgument;
9+
use Symfony\Component\Console\Input\InputOption;
910

1011
class AsyncCommand extends Command
1112
{
@@ -32,6 +33,10 @@ public function fire()
3233
{
3334
$item = Job::findOrFail($this->argument('job_id'));
3435

36+
if ($delay = (int) $this->option('delay')) {
37+
sleep($delay);
38+
}
39+
3540
$job = new AsyncJob($this->laravel, $item);
3641

3742
$job->fire();
@@ -48,4 +53,16 @@ protected function getArguments()
4853
array('job_id', InputArgument::REQUIRED, 'The Job ID'),
4954
);
5055
}
56+
57+
/**
58+
* Get the console command arguments.
59+
*
60+
* @return array
61+
*/
62+
protected function getOptions()
63+
{
64+
return array(
65+
array('delay', 'D', InputOption::VALUE_OPTIONAL, 'The delay in seconds', 0),
66+
);
67+
}
5168
}

src/Jobs/AsyncJob.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@ public function fire()
3737
// Get the payload from the job
3838
$payload = $this->parsePayload($this->getRawBody());
3939

40-
// If we have to wait, sleep until our time has come
41-
if ($this->job->delay && $this->attempts() == 0) {
42-
$this->job->status = Job::STATUS_WAITING;
43-
$this->job->save();
44-
sleep($this->job->delay);
45-
}
46-
4740
// Mark job as started
4841
$this->job->status = Job::STATUS_STARTED;
4942
$this->job->retries++;

0 commit comments

Comments
 (0)