1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SfCod\QueueBundle\Failer; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use MongoDB\Collection; |
7
|
|
|
use MongoDB\DeleteResult; |
8
|
|
|
use SfCod\QueueBundle\Base\MongoDriverInterface; |
9
|
|
|
use SfCod\QueueBundle\Entity\Job; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Mongo provider for failed jobs |
13
|
|
|
* |
14
|
|
|
* @author Virchenko Maksim <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class MongoFailedJobProvider implements FailedJobProviderInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The database connection name. |
20
|
|
|
* |
21
|
|
|
* @var MongoDriverInterface |
22
|
|
|
*/ |
23
|
|
|
protected $mongo; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The database collection. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $collection; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create a new database failed job provider. |
34
|
|
|
* |
35
|
|
|
* @param MongoDriverInterface $mongo |
36
|
|
|
* @param string $collection |
37
|
|
|
*/ |
38
|
|
|
public function __construct(MongoDriverInterface $mongo, string $collection = 'queue_jobs_failed') |
39
|
|
|
{ |
40
|
|
|
$this->mongo = $mongo; |
41
|
|
|
$this->collection = $collection; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Log a failed job into storage. |
46
|
|
|
* |
47
|
|
|
* @param string $connection |
48
|
|
|
* @param string $queue |
49
|
|
|
* @param string $payload |
50
|
|
|
* @param Exception $exception |
51
|
|
|
* |
52
|
|
|
* @return int|null|void |
53
|
|
|
*/ |
54
|
|
|
public function log(string $connection, string $queue, string $payload, Exception $exception) |
55
|
|
|
{ |
56
|
|
|
$this->getCollection()->insertOne([ |
57
|
|
|
'connection' => $connection, |
58
|
|
|
'queue' => $queue, |
59
|
|
|
'payload' => $payload, |
60
|
|
|
'exception' => $exception->getMessage(), |
61
|
|
|
'failed_at' => time(), |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get a list of all of the failed jobs. |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function all(): array |
71
|
|
|
{ |
72
|
|
|
$result = []; |
73
|
|
|
$jobs = $this->getCollection()->find([], [ |
74
|
|
|
'sort' => ['_id' => -1], |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
foreach ($jobs as $job) { |
78
|
|
|
$result[] = $this->buildJob($job); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $result; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get a single failed job. |
86
|
|
|
* |
87
|
|
|
* @param string $id |
88
|
|
|
* |
89
|
|
|
* @return Job |
90
|
|
|
*/ |
91
|
|
|
public function find($id) |
92
|
|
|
{ |
93
|
|
|
$data = $this->getCollection()->findOne(['_id' => new \MongoDB\BSON\ObjectID($id)]); |
94
|
|
|
|
95
|
|
|
return $this->buildJob($data); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Delete a single failed job from storage. |
100
|
|
|
* |
101
|
|
|
* @param string $id |
102
|
|
|
* |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
public function forget($id) |
106
|
|
|
{ |
107
|
|
|
$result = $this->getCollection()->deleteOne(['_id' => new \MongoDB\BSON\ObjectID($id)]); |
108
|
|
|
|
109
|
|
|
if ($result instanceof DeleteResult) { |
110
|
|
|
return (bool)$result->getDeletedCount(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Flush all of the failed jobs from storage. |
118
|
|
|
*/ |
119
|
|
|
public function flush() |
120
|
|
|
{ |
121
|
|
|
$this->getCollection()->drop(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get a new query builder instance for the collection. |
126
|
|
|
* |
127
|
|
|
* @return Collection mongo collection |
128
|
|
|
*/ |
129
|
|
|
protected function getCollection(): Collection |
130
|
|
|
{ |
131
|
|
|
return $this->mongo->getDatabase()->selectCollection($this->collection); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Build job from database data |
136
|
|
|
* |
137
|
|
|
* @param $data |
138
|
|
|
* |
139
|
|
|
* @return Job |
140
|
|
|
*/ |
141
|
|
|
protected function buildJob($data): Job |
142
|
|
|
{ |
143
|
|
|
$job = new Job(); |
144
|
|
|
$job->setId($data->_id); |
145
|
|
|
$job->setQueue($data->queue); |
146
|
|
|
$job->setAttempts(isset($data->attempts) ? $data->attempts : 0); |
147
|
|
|
$job->setReserved(isset($data->reserved) ? $data->reserved : false); |
148
|
|
|
$job->setReservedAt(isset($data->reserved_at) ? $data->reserved_at : null); |
149
|
|
|
$job->setPayload(json_decode($data->payload, true)); |
150
|
|
|
|
151
|
|
|
return $job; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|