Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Queue\Jobs;
4
 
5
use Illuminate\Contracts\Events\Dispatcher;
6
use Illuminate\Queue\Events\JobFailed;
7
use Illuminate\Queue\ManuallyFailedException;
8
use Illuminate\Support\InteractsWithTime;
9
 
10
abstract class Job
11
{
12
    use InteractsWithTime;
13
 
14
    /**
15
     * The job handler instance.
16
     *
17
     * @var mixed
18
     */
19
    protected $instance;
20
 
21
    /**
22
     * The IoC container instance.
23
     *
24
     * @var \Illuminate\Container\Container
25
     */
26
    protected $container;
27
 
28
    /**
29
     * Indicates if the job has been deleted.
30
     *
31
     * @var bool
32
     */
33
    protected $deleted = false;
34
 
35
    /**
36
     * Indicates if the job has been released.
37
     *
38
     * @var bool
39
     */
40
    protected $released = false;
41
 
42
    /**
43
     * Indicates if the job has failed.
44
     *
45
     * @var bool
46
     */
47
    protected $failed = false;
48
 
49
    /**
50
     * The name of the connection the job belongs to.
51
     *
52
     * @var string
53
     */
54
    protected $connectionName;
55
 
56
    /**
57
     * The name of the queue the job belongs to.
58
     *
59
     * @var string
60
     */
61
    protected $queue;
62
 
63
    /**
64
     * Get the job identifier.
65
     *
66
     * @return string
67
     */
68
    abstract public function getJobId();
69
 
70
    /**
71
     * Get the raw body of the job.
72
     *
73
     * @return string
74
     */
75
    abstract public function getRawBody();
76
 
77
    /**
78
     * Get the UUID of the job.
79
     *
80
     * @return string|null
81
     */
82
    public function uuid()
83
    {
84
        return $this->payload()['uuid'] ?? null;
85
    }
86
 
87
    /**
88
     * Fire the job.
89
     *
90
     * @return void
91
     */
92
    public function fire()
93
    {
94
        $payload = $this->payload();
95
 
96
        [$class, $method] = JobName::parse($payload['job']);
97
 
98
        ($this->instance = $this->resolve($class))->{$method}($this, $payload['data']);
99
    }
100
 
101
    /**
102
     * Delete the job from the queue.
103
     *
104
     * @return void
105
     */
106
    public function delete()
107
    {
108
        $this->deleted = true;
109
    }
110
 
111
    /**
112
     * Determine if the job has been deleted.
113
     *
114
     * @return bool
115
     */
116
    public function isDeleted()
117
    {
118
        return $this->deleted;
119
    }
120
 
121
    /**
122
     * Release the job back into the queue after (n) seconds.
123
     *
124
     * @param  int  $delay
125
     * @return void
126
     */
127
    public function release($delay = 0)
128
    {
129
        $this->released = true;
130
    }
131
 
132
    /**
133
     * Determine if the job was released back into the queue.
134
     *
135
     * @return bool
136
     */
137
    public function isReleased()
138
    {
139
        return $this->released;
140
    }
141
 
142
    /**
143
     * Determine if the job has been deleted or released.
144
     *
145
     * @return bool
146
     */
147
    public function isDeletedOrReleased()
148
    {
149
        return $this->isDeleted() || $this->isReleased();
150
    }
151
 
152
    /**
153
     * Determine if the job has been marked as a failure.
154
     *
155
     * @return bool
156
     */
157
    public function hasFailed()
158
    {
159
        return $this->failed;
160
    }
161
 
162
    /**
163
     * Mark the job as "failed".
164
     *
165
     * @return void
166
     */
167
    public function markAsFailed()
168
    {
169
        $this->failed = true;
170
    }
171
 
172
    /**
173
     * Delete the job, call the "failed" method, and raise the failed job event.
174
     *
175
     * @param  \Throwable|null  $e
176
     * @return void
177
     */
178
    public function fail($e = null)
179
    {
180
        $this->markAsFailed();
181
 
182
        if ($this->isDeleted()) {
183
            return;
184
        }
185
 
186
        try {
187
            // If the job has failed, we will delete it, call the "failed" method and then call
188
            // an event indicating the job has failed so it can be logged if needed. This is
189
            // to allow every developer to better keep monitor of their failed queue jobs.
190
            $this->delete();
191
 
192
            $this->failed($e);
193
        } finally {
194
            $this->resolve(Dispatcher::class)->dispatch(new JobFailed(
195
                $this->connectionName, $this, $e ?: new ManuallyFailedException
196
            ));
197
        }
198
    }
199
 
200
    /**
201
     * Process an exception that caused the job to fail.
202
     *
203
     * @param  \Throwable|null  $e
204
     * @return void
205
     */
206
    protected function failed($e)
207
    {
208
        $payload = $this->payload();
209
 
210
        [$class, $method] = JobName::parse($payload['job']);
211
 
212
        if (method_exists($this->instance = $this->resolve($class), 'failed')) {
213
            $this->instance->failed($payload['data'], $e, $payload['uuid'] ?? '');
214
        }
215
    }
216
 
217
    /**
218
     * Resolve the given class.
219
     *
220
     * @param  string  $class
221
     * @return mixed
222
     */
223
    protected function resolve($class)
224
    {
225
        return $this->container->make($class);
226
    }
227
 
228
    /**
229
     * Get the resolved job handler instance.
230
     *
231
     * @return mixed
232
     */
233
    public function getResolvedJob()
234
    {
235
        return $this->instance;
236
    }
237
 
238
    /**
239
     * Get the decoded body of the job.
240
     *
241
     * @return array
242
     */
243
    public function payload()
244
    {
245
        return json_decode($this->getRawBody(), true);
246
    }
247
 
248
    /**
249
     * Get the number of times to attempt a job.
250
     *
251
     * @return int|null
252
     */
253
    public function maxTries()
254
    {
255
        return $this->payload()['maxTries'] ?? null;
256
    }
257
 
258
    /**
259
     * Get the number of times to attempt a job after an exception.
260
     *
261
     * @return int|null
262
     */
263
    public function maxExceptions()
264
    {
265
        return $this->payload()['maxExceptions'] ?? null;
266
    }
267
 
268
    /**
269
     * Determine if the job should fail when it timeouts.
270
     *
271
     * @return bool
272
     */
273
    public function shouldFailOnTimeout()
274
    {
275
        return $this->payload()['failOnTimeout'] ?? false;
276
    }
277
 
278
    /**
279
     * The number of seconds to wait before retrying a job that encountered an uncaught exception.
280
     *
281
     * @return int|int[]|null
282
     */
283
    public function backoff()
284
    {
285
        return $this->payload()['backoff'] ?? $this->payload()['delay'] ?? null;
286
    }
287
 
288
    /**
289
     * Get the number of seconds the job can run.
290
     *
291
     * @return int|null
292
     */
293
    public function timeout()
294
    {
295
        return $this->payload()['timeout'] ?? null;
296
    }
297
 
298
    /**
299
     * Get the timestamp indicating when the job should timeout.
300
     *
301
     * @return int|null
302
     */
303
    public function retryUntil()
304
    {
305
        return $this->payload()['retryUntil'] ?? null;
306
    }
307
 
308
    /**
309
     * Get the name of the queued job class.
310
     *
311
     * @return string
312
     */
313
    public function getName()
314
    {
315
        return $this->payload()['job'];
316
    }
317
 
318
    /**
319
     * Get the resolved name of the queued job class.
320
     *
321
     * Resolves the name of "wrapped" jobs such as class-based handlers.
322
     *
323
     * @return string
324
     */
325
    public function resolveName()
326
    {
327
        return JobName::resolve($this->getName(), $this->payload());
328
    }
329
 
330
    /**
331
     * Get the name of the connection the job belongs to.
332
     *
333
     * @return string
334
     */
335
    public function getConnectionName()
336
    {
337
        return $this->connectionName;
338
    }
339
 
340
    /**
341
     * Get the name of the queue the job belongs to.
342
     *
343
     * @return string
344
     */
345
    public function getQueue()
346
    {
347
        return $this->queue;
348
    }
349
 
350
    /**
351
     * Get the service container instance.
352
     *
353
     * @return \Illuminate\Container\Container
354
     */
355
    public function getContainer()
356
    {
357
        return $this->container;
358
    }
359
}