1
|
<?php
|
2
|
|
3
|
namespace Illuminate\Queue;
|
4
|
|
5
|
use Exception;
|
6
|
use Throwable;
|
7
|
use Illuminate\Queue\Jobs\SyncJob;
|
8
|
use Illuminate\Contracts\Queue\Job;
|
9
|
use Illuminate\Contracts\Queue\Queue as QueueContract;
|
10
|
|
11
|
class SyncQueue extends Queue implements QueueContract
|
12
|
{
|
13
|
/**
|
14
|
* Push a new job onto the queue.
|
15
|
*
|
16
|
* @param string $job
|
17
|
* @param mixed $data
|
18
|
* @param string $queue
|
19
|
* @return mixed
|
20
|
*
|
21
|
* @throws \Exception|\Throwable
|
22
|
*/
|
23
|
public function push($job, $data = '', $queue = null)
|
24
|
{
|
25
|
$queueJob = $this->resolveJob($this->createPayload($job, $data, $queue));
|
26
|
|
27
|
try {
|
28
|
$this->raiseBeforeJobEvent($queueJob);
|
29
|
|
30
|
$queueJob->fire();
|
31
|
|
32
|
$this->raiseAfterJobEvent($queueJob);
|
33
|
} catch (Exception $e) {
|
34
|
$this->raiseExceptionOccurredJobEvent($queueJob, $e);
|
35
|
|
36
|
$this->handleFailedJob($queueJob);
|
37
|
|
38
|
throw $e;
|
39
|
} catch (Throwable $e) {
|
40
|
$this->raiseExceptionOccurredJobEvent($queueJob, $e);
|
41
|
|
42
|
$this->handleFailedJob($queueJob);
|
43
|
|
44
|
throw $e;
|
45
|
}
|
46
|
|
47
|
return 0;
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* Push a raw payload onto the queue.
|
52
|
*
|
53
|
* @param string $payload
|
54
|
* @param string $queue
|
55
|
* @param array $options
|
56
|
* @return mixed
|
57
|
*/
|
58
|
public function pushRaw($payload, $queue = null, array $options = [])
|
59
|
{
|
60
|
//
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* Push a new job onto the queue after a delay.
|
65
|
*
|
66
|
* @param \DateTime|int $delay
|
67
|
* @param string $job
|
68
|
* @param mixed $data
|
69
|
* @param string $queue
|
70
|
* @return mixed
|
71
|
*/
|
72
|
public function later($delay, $job, $data = '', $queue = null)
|
73
|
{
|
74
|
return $this->push($job, $data, $queue);
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* Pop the next job off of the queue.
|
79
|
*
|
80
|
* @param string $queue
|
81
|
* @return \Illuminate\Contracts\Queue\Job|null
|
82
|
*/
|
83
|
public function pop($queue = null)
|
84
|
{
|
85
|
//
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* Resolve a Sync job instance.
|
90
|
*
|
91
|
* @param string $payload
|
92
|
* @return \Illuminate\Queue\Jobs\SyncJob
|
93
|
*/
|
94
|
protected function resolveJob($payload)
|
95
|
{
|
96
|
return new SyncJob($this->container, $payload);
|
97
|
}
|
98
|
|
99
|
/**
|
100
|
* Raise the before queue job event.
|
101
|
*
|
102
|
* @param \Illuminate\Contracts\Queue\Job $job
|
103
|
* @return void
|
104
|
*/
|
105
|
protected function raiseBeforeJobEvent(Job $job)
|
106
|
{
|
107
|
$data = json_decode($job->getRawBody(), true);
|
108
|
|
109
|
if ($this->container->bound('events')) {
|
110
|
$this->container['events']->fire(new Events\JobProcessing('sync', $job, $data));
|
111
|
}
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* Raise the after queue job event.
|
116
|
*
|
117
|
* @param \Illuminate\Contracts\Queue\Job $job
|
118
|
* @return void
|
119
|
*/
|
120
|
protected function raiseAfterJobEvent(Job $job)
|
121
|
{
|
122
|
$data = json_decode($job->getRawBody(), true);
|
123
|
|
124
|
if ($this->container->bound('events')) {
|
125
|
$this->container['events']->fire(new Events\JobProcessed('sync', $job, $data));
|
126
|
}
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* Raise the exception occurred queue job event.
|
131
|
*
|
132
|
* @param \Illuminate\Contracts\Queue\Job $job
|
133
|
* @param \Throwable $exception
|
134
|
* @return void
|
135
|
*/
|
136
|
protected function raiseExceptionOccurredJobEvent(Job $job, $exception)
|
137
|
{
|
138
|
$data = json_decode($job->getRawBody(), true);
|
139
|
|
140
|
if ($this->container->bound('events')) {
|
141
|
$this->container['events']->fire(new Events\JobExceptionOccurred('sync', $job, $data, $exception));
|
142
|
}
|
143
|
}
|
144
|
|
145
|
/**
|
146
|
* Handle the failed job.
|
147
|
*
|
148
|
* @param \Illuminate\Contracts\Queue\Job $job
|
149
|
* @return array
|
150
|
*/
|
151
|
protected function handleFailedJob(Job $job)
|
152
|
{
|
153
|
$job->failed();
|
154
|
|
155
|
$this->raiseFailedJobEvent($job);
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* Raise the failed queue job event.
|
160
|
*
|
161
|
* @param \Illuminate\Contracts\Queue\Job $job
|
162
|
* @return void
|
163
|
*/
|
164
|
protected function raiseFailedJobEvent(Job $job)
|
165
|
{
|
166
|
$data = json_decode($job->getRawBody(), true);
|
167
|
|
168
|
if ($this->container->bound('events')) {
|
169
|
$this->container['events']->fire(new Events\JobFailed('sync', $job, $data));
|
170
|
}
|
171
|
}
|
172
|
}
|