-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJob.php
More file actions
72 lines (57 loc) · 1.23 KB
/
Job.php
File metadata and controls
72 lines (57 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
declare(strict_types=1);
namespace LogadApp\Queue;
use Exception;
abstract class Job
{
public string $id;
protected string $queue = 'default';
protected int $tries = 0; // no retries unless set
protected int $attempts = 0;
protected int $retryAfter = 60;
public static function dispatch(...$params): void
{
(new static(...$params))->dispatchSelf();
}
final public function dispatchSelf(): void
{
Queue::add($this->queue, $this);
}
final public function process(): void
{
try {
$this->handle();
Queue::delete($this->queue, $this);
} catch (Exception $e) {
$this->attempts++;
if ($this->attempts < $this->tries) {
$this->retry();
} else {
$this->failure($e);
Queue::delete($this->queue, $this);
}
}
}
final public function getId(): string
{
return $this->id ?? '';
}
final public function getAttempts(): int
{
return $this->attempts;
}
final public function setId(string $id): self
{
$this->id = $id;
return $this;
}
private function retry(): void
{
Queue::retry($this->queue, $this);
}
final protected function failure(Exception $exception): void
{
error_log("Job failed: " . $exception->getMessage());
}
abstract protected function handle(): void;
}