@@ -150,8 +150,8 @@ public function run($maxJobs)
150
150
$ model ->{$ method }(new Varien_Object ($ job ['data ' ]));
151
151
152
152
// Delete one by one
153
- $ where = $ this ->db ->quoteInto ( 'job_id IN (?) ' , $ job ['merged_ids ' ]);
154
- $ this -> db -> delete ( $ this -> table , $ where );
153
+ $ this ->db ->delete ( $ this -> table , array ( 'job_id IN (?) ' => $ job ['merged_ids ' ]) );
154
+
155
155
156
156
$ this ->logRecord ['processed_jobs ' ] += count ($ job ['merged_ids ' ]);
157
157
} catch (\Exception $ e ) {
@@ -230,11 +230,6 @@ private function getJobs($maxJobs, $pid)
230
230
break ;
231
231
}
232
232
233
- // If $jobs is empty, it's the first run
234
- if (empty ($ jobs )) {
235
- $ firstJobId = $ rawJobs [0 ]['job_id ' ];
236
- }
237
-
238
233
$ rawJobs = $ this ->prepareJobs ($ rawJobs );
239
234
$ rawJobs = array_merge ($ jobs , $ rawJobs );
240
235
$ rawJobs = $ this ->mergeJobs ($ rawJobs );
@@ -265,14 +260,7 @@ private function getJobs($maxJobs, $pid)
265
260
}
266
261
}
267
262
268
- if (isset ($ firstJobId )) {
269
- $ lastJobId = $ this ->maxValueInArray ($ jobs , 'job_id ' );
270
-
271
- // Reserve all new jobs since last run
272
- $ this ->db ->query ("UPDATE {$ this ->db ->quoteIdentifier ($ this ->table , true )}
273
- SET pid = " . $ pid . ", locked_at = ' " . date ('Y-m-d H:i:s ' ) . "'
274
- WHERE job_id >= " . $ firstJobId . " AND job_id <= " . $ lastJobId );
275
- }
263
+ $ this ->lockJobs ($ jobs );
276
264
277
265
$ this ->db ->commit ();
278
266
} catch (\Exception $ e ) {
@@ -442,19 +430,32 @@ private function arrayMultisort()
442
430
return array_pop ($ args );
443
431
}
444
432
445
- private function maxValueInArray ($ array , $ keyToSearch )
433
+ /**
434
+ * @param array $jobs
435
+ */
436
+ private function lockJobs ($ jobs )
446
437
{
447
- $ currentMax = null ;
438
+ $ jobsIds = $ this -> getJobsIdsFromMergedJobs ( $ jobs ) ;
448
439
449
- foreach ($ array as $ arr ) {
450
- foreach ($ arr as $ key => $ value ) {
451
- if ($ key == $ keyToSearch && ($ value >= $ currentMax )) {
452
- $ currentMax = $ value ;
453
- }
454
- }
440
+ if ($ jobsIds !== array ()) {
441
+ $ pid = getmypid ();
442
+ $ this ->db ->update ($ this ->table , array ('pid ' => $ pid ), array ('job_id IN (?) ' => $ jobsIds ));
443
+ }
444
+ }
445
+
446
+ /**
447
+ * @param array $mergedJobs
448
+ *
449
+ * @return string[]
450
+ */
451
+ private function getJobsIdsFromMergedJobs ($ mergedJobs )
452
+ {
453
+ $ jobsIds = array ();
454
+ foreach ($ mergedJobs as $ job ) {
455
+ $ jobsIds = array_merge ($ jobsIds , $ job ['merged_ids ' ]);
455
456
}
456
457
457
- return $ currentMax ;
458
+ return $ jobsIds ;
458
459
}
459
460
460
461
private function clearOldLogRecords ()
0 commit comments