}
/**
- * Execute a function from the queue
+ * Slow the execution down if the system load is too high
*
- * @param array $queue Workerqueue entry
- * @param string $funcname name of the function
- * @param array $argv Array of values to be passed to the function
- * @param boolean $method_call boolean
* @return void
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
- private static function execFunction(array $queue, string $funcname, array $argv, bool $method_call)
+ public static function coolDown()
{
- $a = DI::app();
+ $load_cooldown = DI::config()->get('system', 'worker_load_cooldown');
+ $processes_cooldown = DI::config()->get('system', 'worker_processes_cooldown');
- $cooldown = DI::config()->get('system', 'worker_cooldown', 0);
- if ($cooldown > 0) {
- Logger::debug('Pre execution cooldown.', ['cooldown' => $cooldown, 'id' => $queue['id'], 'priority' => $queue['priority'], 'command' => $queue['command']]);
- sleep($cooldown);
+ if (($load_cooldown == 0) && ($processes_cooldown == 0)) {
+ return;
}
- $load_cooldown = DI::config()->get('system', 'worker_load_cooldown');
- $processes_cooldown = DI::config()->get('system', 'worker_processes_cooldown');
+ $sleeping = false;
- while ((($load_cooldown > 0) || ($processes_cooldown > 0)) && ($load = System::getLoadAvg())) {
+ while ($load = System::getLoadAvg()) {
if (($load_cooldown > 0) && ($load['average1'] > $load_cooldown)) {
- Logger::debug('Load induced pre execution cooldown.', ['max' => $load_cooldown, 'load' => $load, 'id' => $queue['id'], 'priority' => $queue['priority'], 'command' => $queue['command']]);
+ if (!$sleeping) {
+ Logger::notice('Load induced pre execution cooldown.', ['max' => $load_cooldown, 'load' => $load, 'called-by' => System::callstack(1)]);
+ $sleeping = true;
+ }
sleep(1);
continue;
}
if (($processes_cooldown > 0) && ($load['scheduled'] > $processes_cooldown)) {
- Logger::debug('Process induced pre execution cooldown.', ['max' => $processes_cooldown, 'load' => $load, 'id' => $queue['id'], 'priority' => $queue['priority'], 'command' => $queue['command']]);
+ if (!$sleeping) {
+ Logger::notice('Process induced pre execution cooldown.', ['max' => $processes_cooldown, 'load' => $load, 'called-by' => System::callstack(1)]);
+ $sleeping = true;
+ }
sleep(1);
continue;
}
break;
}
+ if ($sleeping) {
+ Logger::notice('Cooldown ended.', ['max-load' => $load_cooldown, 'max-processes' => $processes_cooldown, 'load' => $load, 'called-by' => System::callstack(1)]);
+ }
+ }
+
+ /**
+ * Execute a function from the queue
+ *
+ * @param array $queue Workerqueue entry
+ * @param string $funcname name of the function
+ * @param array $argv Array of values to be passed to the function
+ * @param boolean $method_call boolean
+ * @return void
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ */
+ private static function execFunction(array $queue, string $funcname, array $argv, bool $method_call)
+ {
+ $a = DI::app();
+
+ $cooldown = DI::config()->get('system', 'worker_cooldown', 0);
+ if ($cooldown > 0) {
+ Logger::notice('Pre execution cooldown.', ['cooldown' => $cooldown, 'id' => $queue['id'], 'priority' => $queue['priority'], 'command' => $queue['command']]);
+ sleep($cooldown);
+ }
+
+ self::coolDown();
+
Logger::enableWorker($funcname);
Logger::info('Process start.', ['priority' => $queue['priority'], 'id' => $queue['id']]);
Logger::info('Performance:', ['state' => self::$state, 'count' => $dbcount, 'stat' => $dbstat, 'write' => $dbwrite, 'lock' => $dblock, 'total' => $dbtotal, 'rest' => $rest, 'exec' => $exec]);
+ self::coolDown();
+
self::$up_start = microtime(true);
self::$db_duration = 0;
self::$db_duration_count = 0;
if (Worker::add($deliver_options, 'Delivery', $cmd, $post_uriid, (int)$contact['id'], $sender_uid)) {
$delivery_queue_count++;
}
+ Worker::coolDown();
}
return $delivery_queue_count;
}
Logger::info('Account removal via ActivityPub', ['uid' => $self_user_id, 'inbox' => $inbox]);
Worker::add(['priority' => PRIORITY_NEGLIGIBLE, 'created' => $created, 'dont_fork' => true],
'APDelivery', Delivery::REMOVAL, 0, $inbox, $self_user_id, $receivers);
+ Worker::coolDown();
}
return true;
$delivery_queue_count++;
}
}
+ Worker::coolDown();
}
// We deliver posts to relay servers slightly delayed to priorize the direct delivery
$delivery_queue_count++;
}
}
+ Worker::coolDown();
}
return ['count' => $delivery_queue_count, 'contacts' => $contacts];