public static function pickReadyJobsForWorkers(MongoCollection $collection, $worksOn, $workers) { $jobs = Onebip\array_pluck(iterator_to_array($collection->find(Worker::canWorkOnAnyJobs($worksOn) ? ['scheduled_at' => ['$lt' => T\MongoDate::now()], 'locked' => false] : ['scheduled_at' => ['$lt' => T\MongoDate::now()], 'locked' => false, 'group' => $worksOn], ['_id' => 1])->sort(['scheduled_at' => 1])->limit(count($workers))), '_id'); if (count($jobs) > 0) { return [$worksOn, $workers, $jobs]; } }
public static function pickAvailableWorkers(MongoCollection $collection, $workersPerUnit) { $result = []; $workers = iterator_to_array($collection->find(['available' => true], ['_id' => 1, 'work_on' => 1])); if (count($workers) > 0) { $unitsOfWorkers = Onebip\array_group_by($workers, function ($worker) { return $worker['work_on']; }); foreach ($unitsOfWorkers as $workOn => $workersInUnit) { $workersInUnit = Onebip\array_pluck($workersInUnit, '_id'); $workersInUnit = array_slice($workersInUnit, 0, min(count($workersInUnit), $workersPerUnit)); $result[] = [$workOn, $workersInUnit]; } } return $result; }