|
38 | 38 | use Tina4\Queue\KafkaBackend; |
39 | 39 | use Tina4\Queue\MongoBackend; |
40 | 40 |
|
41 | | -/** |
42 | | - * Job — Wraps a queue job with lifecycle methods. |
43 | | - */ |
44 | | -class Job |
45 | | -{ |
46 | | - public string $id; |
47 | | - public mixed $payload; |
48 | | - public string $status; |
49 | | - public int $attempts; |
50 | | - public ?string $error; |
51 | | - private Queue $queue; |
52 | | - private string $topic; |
53 | | - |
54 | | - public function __construct(array $data, Queue $queue, string $topic) |
55 | | - { |
56 | | - $this->id = $data['id']; |
57 | | - $this->payload = $data['payload']; |
58 | | - $this->status = $data['status'] ?? 'reserved'; |
59 | | - $this->attempts = $data['attempts'] ?? 0; |
60 | | - $this->error = $data['error'] ?? null; |
61 | | - $this->queue = $queue; |
62 | | - $this->topic = $topic; |
63 | | - } |
64 | | - |
65 | | - /** |
66 | | - * Mark this job as completed (removes it from the queue). |
67 | | - */ |
68 | | - public function complete(): void |
69 | | - { |
70 | | - $this->status = 'completed'; |
71 | | - } |
72 | | - |
73 | | - /** |
74 | | - * Mark this job as failed with a reason. |
75 | | - */ |
76 | | - public function fail(string $reason = ''): void |
77 | | - { |
78 | | - $this->status = 'failed'; |
79 | | - $this->error = $reason; |
80 | | - $this->attempts++; |
81 | | - $this->queue->writeFailed($this->topic, [ |
82 | | - 'id' => $this->id, |
83 | | - 'payload' => $this->payload, |
84 | | - 'status' => 'failed', |
85 | | - 'attempts' => $this->attempts, |
86 | | - 'error' => $reason, |
87 | | - ]); |
88 | | - } |
89 | | - |
90 | | - /** |
91 | | - * Reject this job with a reason. Alias for fail(). |
92 | | - */ |
93 | | - public function reject(string $reason = ''): void |
94 | | - { |
95 | | - $this->fail($reason); |
96 | | - } |
97 | | - |
98 | | - /** |
99 | | - * Get the raw job data as array. |
100 | | - */ |
101 | | - public function toArray(): array |
102 | | - { |
103 | | - return [ |
104 | | - 'id' => $this->id, |
105 | | - 'payload' => $this->payload, |
106 | | - 'status' => $this->status, |
107 | | - 'attempts' => $this->attempts, |
108 | | - 'error' => $this->error, |
109 | | - ]; |
110 | | - } |
111 | | -} |
112 | | - |
113 | 41 | class Queue |
114 | 42 | { |
115 | 43 | private string $backend; |
|
0 commit comments