/
FileFailedJobProvider.php
237 lines (206 loc) · 5.65 KB
/
FileFailedJobProvider.php
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
namespace Illuminate\Queue\Failed;
use Closure;
use DateTimeInterface;
use Illuminate\Support\Facades\Date;
class FileFailedJobProvider implements CountableFailedJobProvider, FailedJobProviderInterface, PrunableFailedJobProvider
{
/**
* The file path where the failed job file should be stored.
*
* @var string
*/
protected $path;
/**
* The maximum number of failed jobs to retain.
*
* @var int
*/
protected $limit;
/**
* The lock provider resolver.
*
* @var \Closure
*/
protected $lockProviderResolver;
/**
* Create a new database failed job provider.
*
* @param string $path
* @param int $limit
* @param \Closure|null $lockProviderResolver
* @return void
*/
public function __construct($path, $limit = 100, ?Closure $lockProviderResolver = null)
{
$this->path = $path;
$this->limit = $limit;
$this->lockProviderResolver = $lockProviderResolver;
}
/**
* Log a failed job into storage.
*
* @param string $connection
* @param string $queue
* @param string $payload
* @param \Throwable $exception
* @return int|null
*/
public function log($connection, $queue, $payload, $exception)
{
return $this->lock(function () use ($connection, $queue, $payload, $exception) {
$id = json_decode($payload, true)['uuid'];
$jobs = $this->read();
$failedAt = Date::now();
array_unshift($jobs, [
'id' => $id,
'connection' => $connection,
'queue' => $queue,
'payload' => $payload,
'exception' => (string) mb_convert_encoding($exception, 'UTF-8'),
'failed_at' => $failedAt->format('Y-m-d H:i:s'),
'failed_at_timestamp' => $failedAt->getTimestamp(),
]);
$this->write(array_slice($jobs, 0, $this->limit));
return $id;
});
}
/**
* Get the IDs of all of the failed jobs.
*
* @param string|null $queue
* @return array
*/
public function ids($queue = null)
{
return collect($this->all())
->when(! is_null($queue), fn ($collect) => $collect->where('queue', $queue))
->pluck('id')
->all();
}
/**
* Get a list of all of the failed jobs.
*
* @return array
*/
public function all()
{
return $this->read();
}
/**
* Get a single failed job.
*
* @param mixed $id
* @return object|null
*/
public function find($id)
{
return collect($this->read())
->first(fn ($job) => $job->id === $id);
}
/**
* Delete a single failed job from storage.
*
* @param mixed $id
* @return bool
*/
public function forget($id)
{
return $this->lock(function () use ($id) {
$this->write($pruned = collect($jobs = $this->read())
->reject(fn ($job) => $job->id === $id)
->values()
->all());
return count($jobs) !== count($pruned);
});
}
/**
* Flush all of the failed jobs from storage.
*
* @param int|null $hours
* @return void
*/
public function flush($hours = null)
{
$this->prune(Date::now()->subHours($hours ?: 0));
}
/**
* Prune all of the entries older than the given date.
*
* @param \DateTimeInterface $before
* @return int
*/
public function prune(DateTimeInterface $before)
{
return $this->lock(function () use ($before) {
$jobs = $this->read();
$this->write($prunedJobs = collect($jobs)->reject(function ($job) use ($before) {
return $job->failed_at_timestamp <= $before->getTimestamp();
})->values()->all());
return count($jobs) - count($prunedJobs);
});
}
/**
* Execute the given callback while holding a lock.
*
* @param \Closure $callback
* @return mixed
*/
protected function lock(Closure $callback)
{
if (! $this->lockProviderResolver) {
return $callback();
}
return ($this->lockProviderResolver)()
->lock('laravel-failed-jobs', 5)
->block(10, function () use ($callback) {
return $callback();
});
}
/**
* Read the failed jobs file.
*
* @return array
*/
protected function read()
{
if (! file_exists($this->path)) {
return [];
}
$content = file_get_contents($this->path);
if (empty(trim($content))) {
return [];
}
$content = json_decode($content);
return is_array($content) ? $content : [];
}
/**
* Write the given array of jobs to the failed jobs file.
*
* @param array $jobs
* @return void
*/
protected function write(array $jobs)
{
file_put_contents(
$this->path,
json_encode($jobs, JSON_PRETTY_PRINT)
);
}
/**
* Count the failed jobs.
*
* @param string|null $connection
* @param string|null $queue
* @return int
*/
public function count($connection = null, $queue = null)
{
if (($connection ?? $queue) === null) {
return count($this->read());
}
return collect($this->read())
->filter(fn ($job) => $job->connection === ($connection ?? $job->connection) && $job->queue === ($queue ?? $job->queue))
->count();
}
}