3
3
namespace MongoDB \Laravel \Bus ;
4
4
5
5
use BadMethodCallException ;
6
+ use Carbon \CarbonImmutable ;
6
7
use Closure ;
7
8
use DateTimeInterface ;
9
+ use Illuminate \Bus \Batch ;
10
+ use Illuminate \Bus \BatchFactory ;
8
11
use Illuminate \Bus \DatabaseBatchRepository ;
9
12
use Illuminate \Bus \PendingBatch ;
10
13
use Illuminate \Bus \PrunableBatchRepository ;
11
14
use Illuminate \Bus \UpdatedBatchJobCounts ;
15
+ use Illuminate \Database \Connection ;
12
16
use Illuminate \Support \Carbon ;
13
17
use MongoDB \BSON \ObjectId ;
14
18
use MongoDB \BSON \UTCDateTime ;
15
19
use MongoDB \Driver \ReadPreference ;
16
20
use MongoDB \Laravel \Collection ;
17
21
use Override ;
18
22
23
+ use function assert ;
24
+ use function date_default_timezone_get ;
19
25
use function is_string ;
20
26
21
27
// Extending DatabaseBatchRepository is necessary so methods pruneUnfinished and pruneCancelled
22
28
// are called by PruneBatchesCommand
23
29
class MongoBatchRepository extends DatabaseBatchRepository implements PrunableBatchRepository
24
30
{
31
+ private Collection $ collection ;
32
+
25
33
public function __construct (
26
- private Collection $ collection ,
34
+ BatchFactory $ factory ,
35
+ Connection $ connection ,
36
+ string $ collection ,
27
37
) {
38
+ assert ($ connection instanceof \MongoDB \Laravel \Connection);
39
+ $ this ->collection = $ connection ->getCollection ($ collection );
40
+
41
+ parent ::__construct ($ factory , $ connection , $ collection );
28
42
}
29
43
30
44
#[Override]
31
- public function get ($ limit = 50 , $ before = null )
45
+ public function get ($ limit = 50 , $ before = null ): array
32
46
{
33
47
if (is_string ($ before )) {
34
48
$ before = new ObjectId ($ before );
@@ -41,22 +55,36 @@ public function get($limit = 50, $before = null)
41
55
'sort ' => ['_id ' => -1 ],
42
56
'typeMap ' => ['root ' => 'array ' , 'document ' => 'array ' , 'array ' => 'array ' ],
43
57
],
44
- );
58
+ )-> toArray () ;
45
59
}
46
60
47
61
#[Override]
48
- public function find (string $ batchId )
62
+ public function find (string $ batchId ): ? Batch
49
63
{
50
64
$ batchId = new ObjectId ($ batchId );
51
65
52
- return $ this ->collection ->findOne (
66
+ $ batch = $ this ->collection ->findOne (
53
67
['_id ' => $ batchId ],
54
68
['readPreference ' => ReadPreference::PRIMARY ],
55
69
);
70
+
71
+ return $ this ->factory ->make (
72
+ $ this ,
73
+ $ batch ['id ' ],
74
+ $ batch ['name ' ],
75
+ $ batch ['total_jobs ' ],
76
+ $ batch ['pending_jobs ' ],
77
+ $ batch ['failed_jobs ' ],
78
+ $ batch ['failed_job_ids ' ],
79
+ $ batch ['options ' ],
80
+ CarbonImmutable::createFromTimestamp ($ batch ['created_at ' ]->getTimestamp (), date_default_timezone_get ()),
81
+ $ batch ['cancelled_at ' ] ? CarbonImmutable::createFromTimestamp ($ batch ['cancelled_at ' ]->getTimestamp (), date_default_timezone_get ()) : null ,
82
+ $ batch ['finished_at ' ] ? CarbonImmutable::createFromTimestamp ($ batch ['finished_at ' ]->getTimestamp (), date_default_timezone_get ()) : null ,
83
+ );
56
84
}
57
85
58
86
#[Override]
59
- public function store (PendingBatch $ batch )
87
+ public function store (PendingBatch $ batch ): Batch
60
88
{
61
89
$ this ->collection ->insertOne ([
62
90
'name ' => $ batch ->name ,
@@ -72,7 +100,7 @@ public function store(PendingBatch $batch)
72
100
}
73
101
74
102
#[Override]
75
- public function incrementTotalJobs (string $ batchId , int $ amount )
103
+ public function incrementTotalJobs (string $ batchId , int $ amount ): void
76
104
{
77
105
$ batchId = new ObjectId ($ batchId );
78
106
$ this ->collection ->updateOne (
@@ -90,13 +118,14 @@ public function incrementTotalJobs(string $batchId, int $amount)
90
118
}
91
119
92
120
#[Override]
93
- public function decrementPendingJobs (string $ batchId , string $ jobId )
121
+ public function decrementPendingJobs (string $ batchId , string $ jobId ): UpdatedBatchJobCounts
94
122
{
95
123
$ batchId = new ObjectId ($ batchId );
96
124
$ values = $ this ->collection ->findOneAndUpdate (
97
125
['_id ' => $ batchId ],
98
126
[
99
127
'$dec ' => ['pending_jobs ' => 1 ],
128
+ '$pull ' => ['failed_job_ids ' => $ jobId ],
100
129
],
101
130
[
102
131
'projection ' => ['pending_jobs ' => 1 , 'failed_jobs ' => 1 ],
@@ -110,13 +139,28 @@ public function decrementPendingJobs(string $batchId, string $jobId)
110
139
}
111
140
112
141
#[Override]
113
- public function incrementFailedJobs (string $ batchId , string $ jobId )
142
+ public function incrementFailedJobs (string $ batchId , string $ jobId ): UpdatedBatchJobCounts
114
143
{
115
- // TODO: Implement incrementFailedJobs() method.
144
+ $ batchId = new ObjectId ($ batchId );
145
+ $ values = $ this ->collection ->findOneAndUpdate (
146
+ ['_id ' => $ batchId ],
147
+ [
148
+ '$inc ' => ['pending_jobs ' => 1 ],
149
+ '$push ' => ['failed_job_ids ' => $ jobId ],
150
+ ],
151
+ [
152
+ 'projection ' => ['pending_jobs ' => 1 , 'failed_jobs ' => 1 ],
153
+ ],
154
+ );
155
+
156
+ return new UpdatedBatchJobCounts (
157
+ $ values ['pending_jobs ' ],
158
+ $ values ['failed_jobs ' ],
159
+ );
116
160
}
117
161
118
162
#[Override]
119
- public function markAsFinished (string $ batchId )
163
+ public function markAsFinished (string $ batchId ): void
120
164
{
121
165
$ batchId = new ObjectId ($ batchId );
122
166
$ this ->collection ->updateOne (
@@ -126,7 +170,7 @@ public function markAsFinished(string $batchId)
126
170
}
127
171
128
172
#[Override]
129
- public function cancel (string $ batchId )
173
+ public function cancel (string $ batchId ): void
130
174
{
131
175
$ batchId = new ObjectId ($ batchId );
132
176
$ this ->collection ->updateOne (
@@ -141,22 +185,27 @@ public function cancel(string $batchId)
141
185
}
142
186
143
187
#[Override]
144
- public function delete (string $ batchId )
188
+ public function delete (string $ batchId ): void
145
189
{
146
190
$ batchId = new ObjectId ($ batchId );
147
191
$ this ->collection ->deleteOne (['_id ' => $ batchId ]);
148
192
}
149
193
194
+ /** Execute the given Closure within a storage specific transaction. */
150
195
#[Override]
151
- public function transaction (Closure $ callback )
196
+ public function transaction (Closure $ callback ): mixed
152
197
{
153
198
// Transactions are not necessary
154
199
return $ callback ();
155
200
}
156
201
157
- /** Update an atomic value within the batch. */
202
+ /**
203
+ * Rollback the last database transaction for the connection.
204
+ *
205
+ * Not implemented.
206
+ */
158
207
#[Override]
159
- public function rollBack ()
208
+ public function rollBack (): void
160
209
{
161
210
throw new BadMethodCallException ('Not implemented ' );
162
211
}
@@ -197,4 +246,22 @@ public function pruneCancelled(DateTimeInterface $before): int
197
246
198
247
return $ result ->getDeletedCount ();
199
248
}
249
+
250
+ #[Override]
251
+ protected function toBatch ($ batch ): Batch
252
+ {
253
+ return $ this ->factory ->make (
254
+ $ this ,
255
+ $ batch ->id ,
256
+ $ batch ->name ,
257
+ $ batch ->total_jobs ,
258
+ $ batch ->pending_jobs ,
259
+ $ batch ->failed_jobs ,
260
+ $ batch ->failed_job_ids ,
261
+ $ batch ->options ,
262
+ CarbonImmutable::createFromTimestamp ($ batch ->created_at ->getTimestamp (), date_default_timezone_get ()),
263
+ $ batch ->cancelled_at ? CarbonImmutable::createFromTimestamp ($ batch ->cancelled_at ->getTimestamp (), date_default_timezone_get ()) : null ,
264
+ $ batch ->finished_at ? CarbonImmutable::createFromTimestamp ($ batch ->finished_at ->getTimestamp (), date_default_timezone_get ()) : null ,
265
+ );
266
+ }
200
267
}
0 commit comments