self::runCron();
}
- $starttime = time();
+ $last_check = $starttime = time();
self::$state = self::STATE_STARTUP;
// We fetch the next queue entry that is about to be executed
while ($r = self::workerProcess()) {
- $refetched = false;
+ // Don't refetch when a worker fetches tasks for multiple workers
+ $refetched = DI::config()->get('system', 'worker_multiple_fetch');
foreach ($r as $entry) {
// Assure that the priority is an integer value
$entry['priority'] = (int)$entry['priority'];
}
// To avoid the quitting of multiple workers only one worker at a time will execute the check
- if (!self::getWaitingJobForPID()) {
+ if ((time() > $last_check + 5) && !self::getWaitingJobForPID()) {
self::$state = self::STATE_LONG_LOOP;
if (DI::lock()->acquire(self::LOCK_WORKER, 0)) {
}
DI::lock()->release(self::LOCK_WORKER);
}
+ $last_check = time();
}
// Quit the worker once every cron interval
if (time() > ($starttime + (DI::config()->get('system', 'cron_interval') * 60))) {
Logger::info('Process lifetime reached, respawning.');
+ self::unclaimProcess();
self::spawnWorker();
return;
}
$stamp = (float)microtime(true);
$count = DBA::count('process', ['command' => 'Worker.php']);
self::$db_duration += (microtime(true) - $stamp);
+ self::$db_duration_count += (microtime(true) - $stamp);
return $count;
}
DBA::close($queues);
self::$db_duration += (microtime(true) - $stamp);
+ self::$db_duration_count += (microtime(true) - $stamp);
return $ids;
}
if (DI::config()->get('system', 'worker_multiple_fetch')) {
$pids = [];
- $worker_pids = self::getWorkerPIDList();
- foreach ($worker_pids as $pid => $count) {
+ foreach (self::getWorkerPIDList() as $pid => $count) {
if ($count <= $fetch_limit) {
$pids[] = $pid;
}
DBA::close($tasks);
}
- if (!empty($ids)) {
- $worker = [];
- foreach (array_unique($ids) as $id) {
- $pid = next($pids);
- if (!$pid) {
- $pid = reset($pids);
- }
- $worker[$pid][] = $id;
- }
+ if (empty($ids)) {
+ return;
+ }
- $stamp = (float)microtime(true);
- foreach ($worker as $worker_pid => $worker_ids) {
- Logger::info('Set queue entry', ['pid' => $worker_pid, 'ids' => $worker_ids]);
- DBA::update('workerqueue', ['executed' => DateTimeFormat::utcNow(), 'pid' => $worker_pid],
- ['id' => $worker_ids, 'done' => false, 'pid' => 0]);
+ // Assign the task ids to the workers
+ $worker = [];
+ foreach (array_unique($ids) as $id) {
+ $pid = next($pids);
+ if (!$pid) {
+ $pid = reset($pids);
}
- self::$db_duration += (microtime(true) - $stamp);
- self::$db_duration_write += (microtime(true) - $stamp);
+ $worker[$pid][] = $id;
}
- return !empty($ids);
+ $stamp = (float)microtime(true);
+ foreach ($worker as $worker_pid => $worker_ids) {
+ Logger::info('Set queue entry', ['pid' => $worker_pid, 'ids' => $worker_ids]);
+ DBA::update('workerqueue', ['executed' => DateTimeFormat::utcNow(), 'pid' => $worker_pid],
+ ['id' => $worker_ids, 'done' => false, 'pid' => 0]);
+ }
+ self::$db_duration += (microtime(true) - $stamp);
+ self::$db_duration_write += (microtime(true) - $stamp);
}
/**
}
self::$lock_duration += (microtime(true) - $stamp);
- $found = self::findWorkerProcesses();
+ self::findWorkerProcesses();
DI::lock()->release(self::LOCK_PROCESS);
- if ($found) {
- $stamp = (float)microtime(true);
- $r = DBA::select('workerqueue', [], ['pid' => getmypid(), 'done' => false]);
- self::$db_duration += (microtime(true) - $stamp);
- return DBA::toArray($r);
- }
- return false;
+ return self::getWaitingJobForPID();
}
/**