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

Commit a56b297

Browse files
mgrensonmgrenson
authored andcommitted
Complete for ewbs usage
1 parent a1d7ed9 commit a56b297

File tree

7 files changed

+134
-115
lines changed

7 files changed

+134
-115
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
composer.lock
22
vendor
3+
/.settings/
4+
/.project

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
],
1212
"require": {
1313
"php": ">=5.3.0",
14-
"illuminate/support": "4.x",
15-
"illuminate/console": "4.x",
16-
"symfony/process": "~2.3"
14+
"illuminate/support": "4.x|5.0.x",
15+
"illuminate/console": "4.x|5.0.x"
1716
},
1817
"autoload": {
1918
"classmap": [

src/AsyncQueue.php

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use Barryvdh\Queue\Models\Job;
55
use Illuminate\Queue\SyncQueue;
6-
use Symfony\Component\Process\Process;
6+
use Barryvdh\Queue\Jobs\AsyncJob;
77

88
class AsyncQueue extends SyncQueue
99
{
@@ -27,12 +27,11 @@ public function __construct(array $config)
2727
*
2828
* @return int
2929
*/
30-
public function push($job, $data = '', $queue = null)
30+
public function push($job, $data='', $queue=null)
3131
{
32-
$id = $this->storeJob($job, $data, 0);
33-
$this->startProcess($id, 0);
34-
35-
return $id;
32+
$id = $this->storeJob($job, $data, $queue);
33+
//$this->startProcess($id, 0);
34+
return 0;
3635
}
3736

3837
/**
@@ -42,17 +41,19 @@ public function push($job, $data = '', $queue = null)
4241
*
4342
* @param string $job
4443
* @param mixed $data
44+
* @param string|null $queue
4545
* @param int $delay
4646
*
4747
* @return int
4848
*/
49-
public function storeJob($job, $data, $delay = 0)
49+
private function storeJob($job, $data, $queue=null, $delay=0)
5050
{
5151
$payload = $this->createPayload($job, $data);
5252

5353
$job = new Job();
5454
$job->status = Job::STATUS_OPEN;
5555
$job->delay = $delay;
56+
if($queue) $job->queue = $queue;
5657
$job->payload = $payload;
5758
$job->save();
5859

@@ -63,36 +64,31 @@ public function storeJob($job, $data, $delay = 0)
6364
* Make a Process for the Artisan command for the job id.
6465
*
6566
* @param int $jobId
66-
* @param int $delay
6767
*
6868
* @return void
6969
*/
70-
public function startProcess($jobId, $delay = 0)
70+
public function startProcess($jobId)
7171
{
72-
$command = $this->getCommand($jobId, $delay);
73-
$cwd = $this->container['path.base'];
74-
75-
$process = new Process($command, $cwd);
76-
$process->run();
72+
chdir($this->container['path.base']);
73+
exec($this->getCommand($jobId));
7774
}
7875

7976
/**
8077
* Get the Artisan command as a string for the job id.
8178
*
8279
* @param int $jobId
83-
* @param int $delay
8480
*
8581
* @return string
8682
*/
87-
protected function getCommand($jobId, $delay = 0)
83+
protected function getCommand($jobId)
8884
{
89-
$cmd = '%s artisan queue:async %d --env=%s --delay=%d';
85+
$cmd = '%s artisan queue:async %d --env=%s';
9086
$cmd = $this->getBackgroundCommand($cmd);
9187

9288
$binary = $this->getPhpBinary();
9389
$environment = $this->container->environment();
9490

95-
return sprintf($cmd, $binary, $jobId, $environment, $delay);
91+
return sprintf($cmd, $binary, $jobId, $environment);
9692
}
9793

9894
/**
@@ -102,11 +98,7 @@ protected function getCommand($jobId, $delay = 0)
10298
*/
10399
protected function getPhpBinary()
104100
{
105-
$path = $this->config['binary'];
106-
if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
107-
$path = escapeshellarg($path);
108-
}
109-
101+
$path = escapeshellarg($this->config['binary']);
110102
$args = $this->config['binary_args'];
111103
if(is_array($args)){
112104
$args = implode(' ', $args);
@@ -133,13 +125,38 @@ protected function getBackgroundCommand($cmd)
133125
*
134126
* @return int
135127
*/
136-
public function later($delay, $job, $data = '', $queue = null)
128+
public function later($delay, $job, $data='', $queue=null)
137129
{
138130
$delay = $this->getSeconds($delay);
139-
$id = $this->storeJob($job, $data, $delay);
140-
$this->startProcess($id, $delay);
141-
142-
return $id;
131+
$id = $this->storeJob($job, $data, $queue, $delay);
132+
//$this->startProcess($id);
133+
return 0;
143134
}
144-
135+
136+
/**
137+
* Pop the next job off of the queue.
138+
*
139+
* @param string|null $queue
140+
* @return \Illuminate\Queue\Jobs\Job|null
141+
*/
142+
public function pop($queue=null) {
143+
//TODO Il faudrait peut-être tenir compte de la colonne delay (en tt cas si on compte utiliser cette notion pour déterminer quel est le job suivant...)
144+
if($queue)
145+
$item=Job::where('queue', '=', $queue)->orderBy('id', 'asc')->first();
146+
else
147+
$item=Job::orderBy('id', 'asc')->first();
148+
if($item)
149+
return new AsyncJob($this->container, $item);
150+
return null;
151+
}
152+
153+
/**
154+
*
155+
* {@inheritDoc}
156+
* @see \Illuminate\Queue\SyncQueue::pushRaw()
157+
*/
158+
public function pushRaw($payload, $queue=null, array $options = array()) {
159+
$payload=json_decode($payload,true);
160+
return $this->storeJob($payload['job'], $payload['data'], $queue);
161+
}
145162
}

src/Console/AsyncCommand.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
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;
109

1110
class AsyncCommand extends Command
1211
{
@@ -33,10 +32,6 @@ public function fire()
3332
{
3433
$item = Job::findOrFail($this->argument('job_id'));
3534

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

4237
$job->fire();
@@ -53,16 +48,4 @@ protected function getArguments()
5348
array('job_id', InputArgument::REQUIRED, 'The Job ID'),
5449
);
5550
}
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-
}
6851
}

src/Jobs/AsyncJob.php

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@
88

99
class AsyncJob extends SyncJob
1010
{
11-
/**
12-
* Indicates if the job has been deleted.
13-
*
14-
* @var bool
15-
*/
16-
protected $deleted = false;
17-
1811
/**
1912
* The job model.
2013
*
@@ -27,11 +20,14 @@ class AsyncJob extends SyncJob
2720
*
2821
* @param \Illuminate\Container\Container $container
2922
* @param \Barryvdh\Queue\Models\Job $job
23+
*
24+
* @return void
3025
*/
3126
public function __construct(Container $container, Job $job)
3227
{
33-
$this->container = $container;
3428
$this->job = $job;
29+
$this->container = $container;
30+
$this->job->retries = $this->job->retries + 1;
3531
}
3632

3733
/**
@@ -42,13 +38,20 @@ public function __construct(Container $container, Job $job)
4238
public function fire()
4339
{
4440
// Get the payload from the job
45-
$payload = $this->parsePayload($this->getRawBody());
41+
$payload = $this->parsePayload($this->job->payload);
42+
if(isset($payload['error'])) unset($payload['error']);
43+
44+
// If we have to wait, sleep until our time has come
45+
if ($this->job->delay) {
46+
$this->job->status = Job::STATUS_WAITING;
47+
$this->job->save();
48+
sleep($this->job->delay);
49+
}
4650

4751
// Mark job as started
4852
$this->job->status = Job::STATUS_STARTED;
49-
$this->job->retries++;
5053
$this->job->save();
51-
54+
try {
5255
// Fire the actual job
5356
$this->resolveAndFire($payload);
5457

@@ -57,47 +60,13 @@ public function fire()
5760
$this->job->status = Job::STATUS_FINISHED;
5861
$this->job->save();
5962
}
60-
}
61-
62-
/**
63-
* Get the raw body string for the job.
64-
*
65-
* @return string
66-
*/
67-
public function getRawBody()
68-
{
69-
return $this->job->payload;
70-
}
71-
72-
/**
73-
* Release the job back into the queue.
74-
*
75-
* @param int $delay
76-
* @return void
77-
*/
78-
public function release($delay = 0)
79-
{
80-
// Update the Job status
81-
$this->job->status = Job::STATUS_OPEN;
82-
$this->job->save();
83-
84-
// Wait for the delay
85-
if ($delay) {
86-
sleep($this->getSeconds($delay));
87-
}
88-
89-
// Fire again
90-
$this->fire();
91-
}
92-
93-
/**
94-
* Get the number of times the job has been attempted.
95-
*
96-
* @return int
97-
*/
98-
public function attempts()
99-
{
100-
return (int) $this->job->retries;
63+
}
64+
catch (\Exception $e){
65+
\Log::error('Error when fire a job :'.$e->getMessage()."\nJob details : $this->job->payload");
66+
$payload['error']=utf8_encode($e->__toString());
67+
$this->job->payload=json_encode($payload);
68+
$this->job->save();
69+
}
10170
}
10271

10372
/**
@@ -122,14 +91,35 @@ protected function parsePayload($payload)
12291
{
12392
return json_decode($payload, true);
12493
}
125-
94+
12695
/**
127-
* Get the job identifier.
96+
* Get the number of times the job has been attempted.
12897
*
129-
* @return string
98+
* @return int
13099
*/
131-
public function getJobId()
100+
public function attempts()
101+
{
102+
return (int) $this->job->retries;
103+
}
104+
105+
/**
106+
* Retourner le nom de la queue
107+
*
108+
* {@inheritDoc}
109+
* @see \Illuminate\Queue\Jobs\Job::getQueue()
110+
*/
111+
public function getQueue()
112+
{
113+
return $this->job->queue;
114+
}
115+
116+
/**
117+
* Rendre le payload à l'appel de cette méthode, utile pour le log en cas de failed_job.
118+
* {@inheritDoc}
119+
* @see \Illuminate\Queue\Jobs\SyncJob::getRawBody()
120+
*/
121+
public function getRawBody()
132122
{
133-
return $this->job->id;
123+
return $this->job->payload;
134124
}
135125
}

src/Models/FailedJob.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Barryvdh\Queue\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
/**
8+
* Failed jobs.
9+
*
10+
* @property int $id
11+
* @property string $connection
12+
* @property string $queue
13+
* @property string $payload
14+
* @property \Carbon\Carbon $failed_at
15+
*/
16+
class FailedJob extends Model {
17+
protected $table = 'failed_jobs';
18+
protected $guarded = array('id', 'failed_at');
19+
}

0 commit comments

Comments
 (0)