1717use MongoDB \Driver \ReadPreference ;
1818use MongoDB \Laravel \Collection ;
1919use MongoDB \Laravel \Connection ;
20+ use MongoDB \Operation \FindOneAndUpdate ;
2021use Override ;
2122
2223use function date_default_timezone_get ;
2324use function is_string ;
25+ use function serialize ;
26+ use function unserialize ;
2427
2528// Extending DatabaseBatchRepository is necessary so methods pruneUnfinished and pruneCancelled
2629// are called by PruneBatchesCommand
@@ -68,19 +71,7 @@ public function find(string $batchId): ?Batch
6871 ],
6972 );
7073
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 ;
8475 }
8576
8677 #[Override]
@@ -92,7 +83,8 @@ public function store(PendingBatch $batch): Batch
9283 'pending_jobs ' => 0 ,
9384 'failed_jobs ' => 0 ,
9485 'failed_job_ids ' => [],
95- 'options ' => $ batch ->options ,
86+ // Serialization is required for Closures
87+ 'options ' => serialize ($ batch ->options ),
9688 'created_at ' => new UTCDateTime (Carbon::now ()),
9789 'cancelled_at ' => null ,
9890 'finished_at ' => null ,
@@ -126,11 +118,12 @@ public function decrementPendingJobs(string $batchId, string $jobId): UpdatedBat
126118 $ values = $ this ->collection ->findOneAndUpdate (
127119 ['_id ' => $ batchId ],
128120 [
129- '$dec ' => ['pending_jobs ' => 1 ],
121+ '$inc ' => ['pending_jobs ' => - 1 ],
130122 '$pull ' => ['failed_job_ids ' => $ jobId ],
131123 ],
132124 [
133125 'projection ' => ['pending_jobs ' => 1 , 'failed_jobs ' => 1 ],
126+ 'returnDocument ' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER ,
134127 ],
135128 );
136129
@@ -147,11 +140,12 @@ public function incrementFailedJobs(string $batchId, string $jobId): UpdatedBatc
147140 $ values = $ this ->collection ->findOneAndUpdate (
148141 ['_id ' => $ batchId ],
149142 [
150- '$inc ' => ['pending_jobs ' => 1 ],
143+ '$inc ' => ['failed_jobs ' => 1 ],
151144 '$push ' => ['failed_job_ids ' => $ jobId ],
152145 ],
153146 [
154147 'projection ' => ['pending_jobs ' => 1 , 'failed_jobs ' => 1 ],
148+ 'returnDocument ' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER ,
155149 ],
156150 );
157151
@@ -251,16 +245,16 @@ protected function toBatch($batch): Batch
251245 {
252246 return $ this ->factory ->make (
253247 $ 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 ' ] ),
264258 );
265259 }
266260
0 commit comments