17
17
use MongoDB \Driver \ReadPreference ;
18
18
use MongoDB \Laravel \Collection ;
19
19
use MongoDB \Laravel \Connection ;
20
+ use MongoDB \Operation \FindOneAndUpdate ;
20
21
use Override ;
21
22
22
23
use function date_default_timezone_get ;
23
24
use function is_string ;
25
+ use function serialize ;
26
+ use function unserialize ;
24
27
25
28
// Extending DatabaseBatchRepository is necessary so methods pruneUnfinished and pruneCancelled
26
29
// are called by PruneBatchesCommand
@@ -68,19 +71,7 @@ public function find(string $batchId): ?Batch
68
71
],
69
72
);
70
73
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
- $ this ->toCarbon ($ batch ['created_at ' ]),
81
- $ this ->toCarbon ($ batch ['cancelled_at ' ]),
82
- $ this ->toCarbon ($ batch ['finished_at ' ]),
83
- );
74
+ return $ batch ? $ this ->toBatch ($ batch ) : null ;
84
75
}
85
76
86
77
#[Override]
@@ -92,7 +83,8 @@ public function store(PendingBatch $batch): Batch
92
83
'pending_jobs ' => 0 ,
93
84
'failed_jobs ' => 0 ,
94
85
'failed_job_ids ' => [],
95
- 'options ' => $ batch ->options ,
86
+ // Serialization is required for Closures
87
+ 'options ' => serialize ($ batch ->options ),
96
88
'created_at ' => new UTCDateTime (Carbon::now ()),
97
89
'cancelled_at ' => null ,
98
90
'finished_at ' => null ,
@@ -126,11 +118,12 @@ public function decrementPendingJobs(string $batchId, string $jobId): UpdatedBat
126
118
$ values = $ this ->collection ->findOneAndUpdate (
127
119
['_id ' => $ batchId ],
128
120
[
129
- '$dec ' => ['pending_jobs ' => 1 ],
121
+ '$inc ' => ['pending_jobs ' => - 1 ],
130
122
'$pull ' => ['failed_job_ids ' => $ jobId ],
131
123
],
132
124
[
133
125
'projection ' => ['pending_jobs ' => 1 , 'failed_jobs ' => 1 ],
126
+ 'returnDocument ' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER ,
134
127
],
135
128
);
136
129
@@ -147,11 +140,12 @@ public function incrementFailedJobs(string $batchId, string $jobId): UpdatedBatc
147
140
$ values = $ this ->collection ->findOneAndUpdate (
148
141
['_id ' => $ batchId ],
149
142
[
150
- '$inc ' => ['pending_jobs ' => 1 ],
143
+ '$inc ' => ['failed_jobs ' => 1 ],
151
144
'$push ' => ['failed_job_ids ' => $ jobId ],
152
145
],
153
146
[
154
147
'projection ' => ['pending_jobs ' => 1 , 'failed_jobs ' => 1 ],
148
+ 'returnDocument ' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER ,
155
149
],
156
150
);
157
151
@@ -251,16 +245,16 @@ protected function toBatch($batch): Batch
251
245
{
252
246
return $ this ->factory ->make (
253
247
$ this ,
254
- $ batch-> id ,
255
- $ batch-> name ,
256
- $ batch-> total_jobs ,
257
- $ batch-> pending_jobs ,
258
- $ batch-> failed_jobs ,
259
- $ batch-> failed_job_ids ,
260
- $ batch-> options ,
261
- $ this ->toCarbon ($ batch-> created_at ),
262
- $ this ->toCarbon ($ batch-> cancelled_at ),
263
- $ this ->toCarbon ($ batch-> finished_at ),
248
+ $ batch[ ' _id ' ] ,
249
+ $ batch[ ' name ' ] ,
250
+ $ batch[ ' total_jobs ' ] ,
251
+ $ batch[ ' pending_jobs ' ] ,
252
+ $ batch[ ' failed_jobs ' ] ,
253
+ $ batch[ ' failed_job_ids ' ] ,
254
+ unserialize ( $ batch[ ' options ' ]) ,
255
+ $ this ->toCarbon ($ batch[ ' created_at ' ] ),
256
+ $ this ->toCarbon ($ batch[ ' cancelled_at ' ] ),
257
+ $ this ->toCarbon ($ batch[ ' finished_at ' ] ),
264
258
);
265
259
}
266
260
0 commit comments