Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1084 lines
33 KiB

4 years ago
  1. <?php
  2. /**
  3. * @file src/Core/Worker.php
  4. */
  5. namespace Friendica\Core;
  6. use Friendica\App;
  7. use Friendica\Core\System;
  8. use Friendica\Core\Config;
  9. use Friendica\Core\Worker;
  10. use Friendica\Database\DBM;
  11. use Friendica\Util\Lock;
  12. use dba;
  13. /**
  14. * @file src/Core/Worker.php
  15. *
  16. * @brief Contains the class for the worker background job processing
  17. */
  18. /**
  19. * @brief Worker methods
  20. */
  21. class Worker
  22. {
  23. private static $up_start;
  24. private static $db_duration;
  25. private static $last_update;
  26. private static $lock_duration;
  27. /**
  28. * @brief Processes the tasks that are in the workerqueue table
  29. *
  30. * @param boolean $run_cron Should the cron processes be executed?
  31. * @return void
  32. */
  33. public static function processQueue($run_cron = true)
  34. {
  35. $a = get_app();
  36. self::$up_start = microtime(true);
  37. // At first check the maximum load. We shouldn't continue with a high load
  38. if ($a->maxload_reached()) {
  39. logger('Pre check: maximum load reached, quitting.', LOGGER_DEBUG);
  40. return;
  41. }
  42. // We now start the process. This is done after the load check since this could increase the load.
  43. $a->start_process();
  44. // Kill stale processes every 5 minutes
  45. $last_cleanup = Config::get('system', 'poller_last_cleaned', 0);
  46. if (time() > ($last_cleanup + 300)) {
  47. Config::set('system', 'poller_last_cleaned', time());
  48. self::killStaleWorkers();
  49. }
  50. // Count active workers and compare them with a maximum value that depends on the load
  51. if (self::tooMuchWorkers()) {
  52. logger('Pre check: Active worker limit reached, quitting.', LOGGER_DEBUG);
  53. return;
  54. }
  55. // Do we have too few memory?
  56. if ($a->min_memory_reached()) {
  57. logger('Pre check: Memory limit reached, quitting.', LOGGER_DEBUG);
  58. return;
  59. }
  60. // Possibly there are too much database connections
  61. if (self::maxConnectionsReached()) {
  62. logger('Pre check: maximum connections reached, quitting.', LOGGER_DEBUG);
  63. return;
  64. }
  65. // Possibly there are too much database processes that block the system
  66. if ($a->max_processes_reached()) {
  67. logger('Pre check: maximum processes reached, quitting.', LOGGER_DEBUG);
  68. return;
  69. }
  70. // Now we start additional cron processes if we should do so
  71. if ($run_cron) {
  72. self::runCron();
  73. }
  74. $starttime = time();
  75. // We fetch the next queue entry that is about to be executed
  76. while ($r = self::workerProcess($passing_slow)) {
  77. // When we are processing jobs with a lower priority, we don't refetch new jobs
  78. // Otherwise fast jobs could wait behind slow ones and could be blocked.
  79. $refetched = $passing_slow;
  80. foreach ($r as $entry) {
  81. // Assure that the priority is an integer value
  82. $entry['priority'] = (int)$entry['priority'];
  83. // The work will be done
  84. if (!self::execute($entry)) {
  85. logger('Process execution failed, quitting.', LOGGER_DEBUG);
  86. return;
  87. }
  88. // If possible we will fetch new jobs for this worker
  89. if (!$refetched && Lock::set('poller_worker_process', 0)) {
  90. $stamp = (float)microtime(true);
  91. $refetched = self::findWorkerProcesses($passing_slow);
  92. self::$db_duration += (microtime(true) - $stamp);
  93. Lock::remove('poller_worker_process');
  94. }
  95. }
  96. // To avoid the quitting of multiple workers only one worker at a time will execute the check
  97. if (Lock::set('poller_worker', 0)) {
  98. $stamp = (float)microtime(true);
  99. // Count active workers and compare them with a maximum value that depends on the load
  100. if (self::tooMuchWorkers()) {
  101. logger('Active worker limit reached, quitting.', LOGGER_DEBUG);
  102. return;
  103. }
  104. // Check free memory
  105. if ($a->min_memory_reached()) {
  106. logger('Memory limit reached, quitting.', LOGGER_DEBUG);
  107. return;
  108. }
  109. Lock::remove('poller_worker');
  110. self::$db_duration += (microtime(true) - $stamp);
  111. }
  112. // Quit the worker once every 5 minutes
  113. if (time() > ($starttime + 300)) {
  114. logger('Process lifetime reached, quitting.', LOGGER_DEBUG);
  115. return;
  116. }
  117. }
  118. logger("Couldn't select a workerqueue entry, quitting.", LOGGER_DEBUG);
  119. }
  120. /**
  121. * @brief Returns the number of non executed entries in the worker queue
  122. *
  123. * @return integer Number of non executed entries in the worker queue
  124. */
  125. private static function totalEntries()
  126. {
  127. $s = dba::fetch_first("SELECT COUNT(*) AS `total` FROM `workerqueue` WHERE `executed` <= ? AND NOT `done`", NULL_DATE);
  128. if (DBM::is_result($s)) {
  129. return $s["total"];
  130. } else {
  131. return 0;
  132. }
  133. }
  134. /**
  135. * @brief Returns the highest priority in the worker queue that isn't executed
  136. *
  137. * @return integer Number of active worker processes
  138. */
  139. private static function highestPriority()
  140. {
  141. $condition = array("`executed` <= ? AND NOT `done`", NULL_DATE);
  142. $s = dba::select('workerqueue', array('priority'), $condition, array('limit' => 1, 'order' => array('priority')));
  143. if (DBM::is_result($s)) {
  144. return $s["priority"];
  145. } else {
  146. return 0;
  147. }
  148. }
  149. /**
  150. * @brief Returns if a process with the given priority is running
  151. *
  152. * @param integer $priority The priority that should be checked
  153. *
  154. * @return integer Is there a process running with that priority?
  155. */
  156. private static function processWithPriorityActive($priority)
  157. {
  158. $condition = array("`priority` <= ? AND `executed` > ? AND NOT `done`", $priority, NULL_DATE);
  159. return dba::exists('workerqueue', $condition);
  160. }
  161. /**
  162. * @brief Execute a worker entry
  163. *
  164. * @param array $queue Workerqueue entry
  165. *
  166. * @return boolean "true" if further processing should be stopped
  167. */
  168. public static function execute($queue)
  169. {
  170. $a = get_app();
  171. $mypid = getmypid();
  172. // Quit when in maintenance
  173. if (Config::get('system', 'maintenance', true)) {
  174. logger("Maintenance mode - quit process ".$mypid, LOGGER_DEBUG);
  175. return false;
  176. }
  177. // Constantly check the number of parallel database processes
  178. if ($a->max_processes_reached()) {
  179. logger("Max processes reached for process ".$mypid, LOGGER_DEBUG);
  180. return false;
  181. }
  182. // Constantly check the number of available database connections to let the frontend be accessible at any time
  183. if (self::maxConnectionsReached()) {
  184. logger("Max connection reached for process ".$mypid, LOGGER_DEBUG);
  185. return false;
  186. }
  187. $argv = json_decode($queue["parameter"]);
  188. // Check for existance and validity of the include file
  189. $include = $argv[0];
  190. if (method_exists(sprintf('Friendica\Worker\%s', $include), 'execute')) {
  191. // We constantly update the "executed" date every minute to avoid being killed too soon
  192. if (!isset(self::$last_update)) {
  193. self::$last_update = strtotime($queue["executed"]);
  194. }
  195. $age = (time() - self::$last_update) / 60;
  196. self::$last_update = time();
  197. if ($age > 1) {
  198. $stamp = (float)microtime(true);
  199. dba::update('workerqueue', array('executed' => datetime_convert()), array('pid' => $mypid, 'done' => false));
  200. self::$db_duration += (microtime(true) - $stamp);
  201. }
  202. array_shift($argv);
  203. self::execFunction($queue, $include, $argv, true);
  204. $stamp = (float)microtime(true);
  205. if (dba::update('workerqueue', array('done' => true), array('id' => $queue["id"]))) {
  206. Config::set('system', 'last_poller_execution', datetime_convert());
  207. }
  208. self::$db_duration = (microtime(true) - $stamp);
  209. return true;
  210. }
  211. // The script could be provided as full path or only with the function name
  212. if ($include == basename($include)) {
  213. $include = "include/".$include.".php";
  214. }
  215. if (!validate_include($include)) {
  216. logger("Include file ".$argv[0]." is not valid!");
  217. dba::delete('workerqueue', array('id' => $queue["id"]));
  218. return true;
  219. }
  220. require_once $include;
  221. $funcname = str_replace(".php", "", basename($argv[0]))."_run";
  222. if (function_exists($funcname)) {
  223. // We constantly update the "executed" date every minute to avoid being killed too soon
  224. if (!isset(self::$last_update)) {
  225. self::$last_update = strtotime($queue["executed"]);
  226. }
  227. $age = (time() - self::$last_update) / 60;
  228. self::$last_update = time();
  229. if ($age > 1) {
  230. $stamp = (float)microtime(true);
  231. dba::update('workerqueue', array('executed' => datetime_convert()), array('pid' => $mypid, 'done' => false));
  232. self::$db_duration += (microtime(true) - $stamp);
  233. }
  234. self::execFunction($queue, $funcname, $argv, false);
  235. $stamp = (float)microtime(true);
  236. if (dba::update('workerqueue', array('done' => true), array('id' => $queue["id"]))) {
  237. Config::set('system', 'last_poller_execution', datetime_convert());
  238. }
  239. self::$db_duration = (microtime(true) - $stamp);
  240. } else {
  241. logger("Function ".$funcname." does not exist");
  242. dba::delete('workerqueue', array('id' => $queue["id"]));
  243. }
  244. return true;
  245. }
  246. /**
  247. * @brief Execute a function from the queue
  248. *
  249. * @param array $queue Workerqueue entry
  250. * @param string $funcname name of the function
  251. * @param array $argv Array of values to be passed to the function
  252. * @param boolean $method_call boolean
  253. * @return void
  254. */
  255. private static function execFunction($queue, $funcname, $argv, $method_call)
  256. {
  257. $a = get_app();
  258. $mypid = getmypid();
  259. $argc = count($argv);
  260. $new_process_id = uniqid("wrk", true);
  261. logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." ".$queue["parameter"]." - Process PID: ".$new_process_id);
  262. $stamp = (float)microtime(true);
  263. // We use the callstack here to analyze the performance of executed worker entries.
  264. // For this reason the variables have to be initialized.
  265. if (Config::get("system", "profiler")) {
  266. $a->performance["start"] = microtime(true);
  267. $a->performance["database"] = 0;
  268. $a->performance["database_write"] = 0;
  269. $a->performance["network"] = 0;
  270. $a->performance["file"] = 0;
  271. $a->performance["rendering"] = 0;
  272. $a->performance["parser"] = 0;
  273. $a->performance["marktime"] = 0;
  274. $a->performance["markstart"] = microtime(true);
  275. $a->callstack = array();
  276. }
  277. // For better logging create a new process id for every worker call
  278. // But preserve the old one for the worker
  279. $old_process_id = $a->process_id;
  280. $a->process_id = $new_process_id;
  281. $a->queue = $queue;
  282. $up_duration = number_format(microtime(true) - self::$up_start, 3);
  283. // Reset global data to avoid interferences
  284. unset($_SESSION);
  285. if ($method_call) {
  286. call_user_func_array(sprintf('Friendica\Worker\%s::execute', $funcname), $argv);
  287. } else {
  288. $funcname($argv, $argc);
  289. }
  290. $a->process_id = $old_process_id;
  291. unset($a->queue);
  292. $duration = number_format(microtime(true) - $stamp, 3);
  293. self::$up_start = microtime(true);
  294. /* With these values we can analyze how effective the worker is.
  295. * The database and rest time should be low since this is the unproductive time.
  296. * The execution time is the productive time.
  297. * By changing parameters like the maximum number of workers we can check the effectivness.
  298. */
  299. logger(
  300. 'DB: '.number_format(self::$db_duration, 2).
  301. ' - Lock: '.number_format(self::$lock_duration, 2).
  302. ' - Rest: '.number_format($up_duration - self::$db_duration - self::$lock_duration, 2).
  303. ' - Execution: '.number_format($duration, 2),
  304. LOGGER_DEBUG
  305. );
  306. self::$lock_duration = 0;
  307. if ($duration > 3600) {
  308. logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 1 hour (".round($duration/60, 3).")", LOGGER_DEBUG);
  309. } elseif ($duration > 600) {
  310. logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 10 minutes (".round($duration/60, 3).")", LOGGER_DEBUG);
  311. } elseif ($duration > 300) {
  312. logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 5 minutes (".round($duration/60, 3).")", LOGGER_DEBUG);
  313. } elseif ($duration > 120) {
  314. logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 2 minutes (".round($duration/60, 3).")", LOGGER_DEBUG);
  315. }
  316. logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." - done in ".$duration." seconds. Process PID: ".$new_process_id);
  317. // Write down the performance values into the log
  318. if (Config::get("system", "profiler")) {
  319. $duration = microtime(true)-$a->performance["start"];
  320. if (Config::get("rendertime", "callstack")) {
  321. if (isset($a->callstack["database"])) {
  322. $o = "\nDatabase Read:\n";
  323. foreach ($a->callstack["database"] as $func => $time) {
  324. $time = round($time, 3);
  325. if ($time > 0) {
  326. $o .= $func.": ".$time."\n";
  327. }
  328. }
  329. }
  330. if (isset($a->callstack["database_write"])) {
  331. $o .= "\nDatabase Write:\n";
  332. foreach ($a->callstack["database_write"] as $func => $time) {
  333. $time = round($time, 3);
  334. if ($time > 0) {
  335. $o .= $func.": ".$time."\n";
  336. }
  337. }
  338. }
  339. if (isset($a->callstack["network"])) {
  340. $o .= "\nNetwork:\n";
  341. foreach ($a->callstack["network"] as $func => $time) {
  342. $time = round($time, 3);
  343. if ($time > 0) {
  344. $o .= $func.": ".$time."\n";
  345. }
  346. }
  347. }
  348. } else {
  349. $o = '';
  350. }
  351. logger(
  352. "ID ".$queue["id"].": ".$funcname.": ".sprintf(
  353. "DB: %s/%s, Net: %s, I/O: %s, Other: %s, Total: %s".$o,
  354. number_format($a->performance["database"] - $a->performance["database_write"], 2),
  355. number_format($a->performance["database_write"], 2),
  356. number_format($a->performance["network"], 2),
  357. number_format($a->performance["file"], 2),
  358. number_format($duration - ($a->performance["database"] + $a->performance["network"] + $a->performance["file"]), 2),
  359. number_format($duration, 2)
  360. ),
  361. LOGGER_DEBUG
  362. );
  363. }
  364. $cooldown = Config::get("system", "worker_cooldown", 0);
  365. if ($cooldown > 0) {
  366. logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." - in cooldown for ".$cooldown." seconds");
  367. sleep($cooldown);
  368. }
  369. }
  370. /**
  371. * @brief Checks if the number of database connections has reached a critical limit.
  372. *
  373. * @return bool Are more than 3/4 of the maximum connections used?
  374. */
  375. private static function maxConnectionsReached()
  376. {
  377. // Fetch the max value from the config. This is needed when the system cannot detect the correct value by itself.
  378. $max = Config::get("system", "max_connections");
  379. // Fetch the percentage level where the worker will get active
  380. $maxlevel = Config::get("system", "max_connections_level", 75);
  381. if ($max == 0) {
  382. // the maximum number of possible user connections can be a system variable
  383. $r = dba::fetch_first("SHOW VARIABLES WHERE `variable_name` = 'max_user_connections'");
  384. if (DBM::is_result($r)) {
  385. $max = $r["Value"];
  386. }
  387. // Or it can be granted. This overrides the system variable
  388. $r = dba::p('SHOW GRANTS');
  389. while ($grants = dba::fetch($r)) {
  390. $grant = array_pop($grants);
  391. if (stristr($grant, "GRANT USAGE ON")) {
  392. if (preg_match("/WITH MAX_USER_CONNECTIONS (\d*)/", $grant, $match)) {
  393. $max = $match[1];
  394. }
  395. }
  396. }
  397. dba::close($r);
  398. }
  399. // If $max is set we will use the processlist to determine the current number of connections
  400. // The processlist only shows entries of the current user
  401. if ($max != 0) {
  402. $r = dba::p('SHOW PROCESSLIST');
  403. $used = dba::num_rows($r);
  404. dba::close($r);
  405. logger("Connection usage (user values): ".$used."/".$max, LOGGER_DEBUG);
  406. $level = ($used / $max) * 100;
  407. if ($level >= $maxlevel) {
  408. logger("Maximum level (".$maxlevel."%) of user connections reached: ".$used."/".$max);
  409. return true;
  410. }
  411. }
  412. // We will now check for the system values.
  413. // This limit could be reached although the user limits are fine.
  414. $r = dba::fetch_first("SHOW VARIABLES WHERE `variable_name` = 'max_connections'");
  415. if (!DBM::is_result($r)) {
  416. return false;
  417. }
  418. $max = intval($r["Value"]);
  419. if ($max == 0) {
  420. return false;
  421. }
  422. $r = dba::fetch_first("SHOW STATUS WHERE `variable_name` = 'Threads_connected'");
  423. if (!DBM::is_result($r)) {
  424. return false;
  425. }
  426. $used = intval($r["Value"]);
  427. if ($used == 0) {
  428. return false;
  429. }
  430. logger("Connection usage (system values): ".$used."/".$max, LOGGER_DEBUG);
  431. $level = $used / $max * 100;
  432. if ($level < $maxlevel) {
  433. return false;
  434. }
  435. logger("Maximum level (".$level."%) of system connections reached: ".$used."/".$max);
  436. return true;
  437. }
  438. /**
  439. * @brief fix the queue entry if the worker process died
  440. * @return void
  441. */
  442. private static function killStaleWorkers()
  443. {
  444. $entries = dba::select(
  445. 'workerqueue',
  446. array('id', 'pid', 'executed', 'priority', 'parameter'),
  447. array('`executed` > ? AND NOT `done` AND `pid` != 0', NULL_DATE),
  448. array('order' => array('priority', 'created'))
  449. );
  450. while ($entry = dba::fetch($entries)) {
  451. if (!posix_kill($entry["pid"], 0)) {
  452. dba::update('workerqueue', array('executed' => NULL_DATE, 'pid' => 0), array('id' => $entry["id"]));
  453. } else {
  454. // Kill long running processes
  455. // Check if the priority is in a valid range
  456. if (!in_array($entry["priority"], array(PRIORITY_CRITICAL, PRIORITY_HIGH, PRIORITY_MEDIUM, PRIORITY_LOW, PRIORITY_NEGLIGIBLE))) {
  457. $entry["priority"] = PRIORITY_MEDIUM;
  458. }
  459. // Define the maximum durations
  460. $max_duration_defaults = array(PRIORITY_CRITICAL => 720, PRIORITY_HIGH => 10, PRIORITY_MEDIUM => 60, PRIORITY_LOW => 180, PRIORITY_NEGLIGIBLE => 720);
  461. $max_duration = $max_duration_defaults[$entry["priority"]];
  462. $argv = json_decode($entry["parameter"]);
  463. $argv[0] = basename($argv[0]);
  464. // How long is the process already running?
  465. $duration = (time() - strtotime($entry["executed"])) / 60;
  466. if ($duration > $max_duration) {
  467. logger("Worker process ".$entry["pid"]." (".implode(" ", $argv).") took more than ".$max_duration." minutes. It will be killed now.");
  468. posix_kill($entry["pid"], SIGTERM);
  469. // We killed the stale process.
  470. // To avoid a blocking situation we reschedule the process at the beginning of the queue.
  471. // Additionally we are lowering the priority. (But not PRIORITY_CRITICAL)
  472. if ($entry["priority"] == PRIORITY_HIGH) {
  473. $new_priority = PRIORITY_MEDIUM;
  474. } elseif ($entry["priority"] == PRIORITY_MEDIUM) {
  475. $new_priority = PRIORITY_LOW;
  476. } elseif ($entry["priority"] != PRIORITY_CRITICAL) {
  477. $new_priority = PRIORITY_NEGLIGIBLE;
  478. }
  479. dba::update(
  480. 'workerqueue',
  481. array('executed' => NULL_DATE, 'created' => datetime_convert(), 'priority' => $new_priority, 'pid' => 0),
  482. array('id' => $entry["id"])
  483. );
  484. } else {
  485. logger("Worker process ".$entry["pid"]." (".implode(" ", $argv).") now runs for ".round($duration)." of ".$max_duration." allowed minutes. That's okay.", LOGGER_DEBUG);
  486. }
  487. }
  488. }
  489. }
  490. /**
  491. * @brief Checks if the number of active workers exceeds the given limits
  492. *
  493. * @return bool Are there too much workers running?
  494. */
  495. public static function tooMuchWorkers()
  496. {
  497. $queues = Config::get("system", "worker_queues", 4);
  498. $maxqueues = $queues;
  499. $active = self::activeWorkers();
  500. // Decrease the number of workers at higher load
  501. $load = current_load();
  502. if ($load) {
  503. $maxsysload = intval(Config::get("system", "maxloadavg", 50));
  504. $maxworkers = $queues;
  505. // Some magical mathemathics to reduce the workers
  506. $exponent = 3;
  507. $slope = $maxworkers / pow($maxsysload, $exponent);
  508. $queues = ceil($slope * pow(max(0, $maxsysload - $load), $exponent));
  509. if (Config::get('system', 'worker_debug')) {
  510. // Create a list of queue entries grouped by their priority
  511. $listitem = array();
  512. // Adding all processes with no workerqueue entry
  513. $processes = dba::p(
  514. "SELECT COUNT(*) AS `running` FROM `process` WHERE NOT EXISTS
  515. (SELECT id FROM `workerqueue`
  516. WHERE `workerqueue`.`pid` = `process`.`pid` AND NOT `done` AND `pid` != ?)",
  517. getmypid()
  518. );
  519. if ($process = dba::fetch($processes)) {
  520. $listitem[0] = "0:".$process["running"];
  521. }
  522. dba::close($processes);
  523. // Now adding all processes with workerqueue entries
  524. $entries = dba::p("SELECT COUNT(*) AS `entries`, `priority` FROM `workerqueue` WHERE NOT `done` GROUP BY `priority`");
  525. while ($entry = dba::fetch($entries)) {
  526. $processes = dba::p("SELECT COUNT(*) AS `running` FROM `process` INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid` AND NOT `done` WHERE `priority` = ?", $entry["priority"]);
  527. if ($process = dba::fetch($processes)) {
  528. $listitem[$entry["priority"]] = $entry["priority"].":".$process["running"]."/".$entry["entries"];
  529. }
  530. dba::close($processes);
  531. }
  532. dba::close($entries);
  533. $intervals = array(1, 10, 60);
  534. $jobs_per_minute = array();
  535. foreach ($intervals as $interval) {
  536. $jobs = dba::p("SELECT COUNT(*) AS `jobs` FROM `workerqueue` WHERE `done` AND `executed` > UTC_TIMESTAMP() - INTERVAL ".intval($interval)." MINUTE");
  537. if ($job = dba::fetch($jobs)) {
  538. $jobs_per_minute[$interval] = number_format($job['jobs'] / $interval, 0);
  539. }
  540. dba::close($jobs);
  541. }
  542. $processlist = ' - jpm: '.implode('/', $jobs_per_minute).' ('.implode(', ', $listitem).')';
  543. }
  544. $entries = self::totalEntries();
  545. if (Config::get("system", "worker_fastlane", false) && ($queues > 0) && ($entries > 0) && ($active >= $queues)) {
  546. $top_priority = self::highestPriority();
  547. $high_running = self::processWithPriorityActive($top_priority);
  548. if (!$high_running && ($top_priority > PRIORITY_UNDEFINED) && ($top_priority < PRIORITY_NEGLIGIBLE)) {
  549. logger("There are jobs with priority ".$top_priority." waiting but none is executed. Open a fastlane.", LOGGER_DEBUG);
  550. $queues = $active + 1;
  551. }
  552. }
  553. logger("Load: ".$load."/".$maxsysload." - processes: ".$active."/".$entries.$processlist." - maximum: ".$queues."/".$maxqueues, LOGGER_DEBUG);
  554. // Are there fewer workers running as possible? Then fork a new one.
  555. if (!Config::get("system", "worker_dont_fork") && ($queues > ($active + 1)) && ($entries > 1)) {
  556. logger("Active workers: ".$active."/".$queues." Fork a new worker.", LOGGER_DEBUG);
  557. self::spawnWorker();
  558. }
  559. }
  560. return $active >= $queues;
  561. }
  562. /**
  563. * @brief Returns the number of active worker processes
  564. *
  565. * @return integer Number of active worker processes
  566. */
  567. private static function activeWorkers()
  568. {
  569. $workers = dba::fetch_first("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'Worker.php'");
  570. return $workers["processes"];
  571. }
  572. /**
  573. * @brief Check if we should pass some slow processes
  574. *
  575. * When the active processes of the highest priority are using more than 2/3
  576. * of all processes, we let pass slower processes.
  577. *
  578. * @param string $highest_priority Returns the currently highest priority
  579. * @return bool We let pass a slower process than $highest_priority
  580. */
  581. private static function passingSlow(&$highest_priority)
  582. {
  583. $highest_priority = 0;
  584. $r = dba::p(
  585. "SELECT `priority`
  586. FROM `process`
  587. INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid` AND NOT `done`"
  588. );
  589. // No active processes at all? Fine
  590. if (!DBM::is_result($r)) {
  591. return false;
  592. }
  593. $priorities = array();
  594. while ($line = dba::fetch($r)) {
  595. $priorities[] = $line["priority"];
  596. }
  597. dba::close($r);
  598. // Should not happen
  599. if (count($priorities) == 0) {
  600. return false;
  601. }
  602. $highest_priority = min($priorities);
  603. // The highest process is already the slowest one?
  604. // Then we quit
  605. if ($highest_priority == PRIORITY_NEGLIGIBLE) {
  606. return false;
  607. }
  608. $high = 0;
  609. foreach ($priorities as $priority) {
  610. if ($priority == $highest_priority) {
  611. ++$high;
  612. }
  613. }
  614. logger("Highest priority: ".$highest_priority." Total processes: ".count($priorities)." Count high priority processes: ".$high, LOGGER_DEBUG);
  615. $passing_slow = (($high/count($priorities)) > (2/3));
  616. if ($passing_slow) {
  617. logger("Passing slower processes than priority ".$highest_priority, LOGGER_DEBUG);
  618. }
  619. return $passing_slow;
  620. }
  621. /**
  622. * @brief Find and claim the next worker process for us
  623. *
  624. * @param boolean $passing_slow Returns if we had passed low priority processes
  625. * @return boolean Have we found something?
  626. */
  627. private static function findWorkerProcesses(&$passing_slow)
  628. {
  629. $mypid = getmypid();
  630. // Check if we should pass some low priority process
  631. $highest_priority = 0;
  632. $found = false;
  633. $passing_slow = false;
  634. // The higher the number of parallel workers, the more we prefetch to prevent concurring access
  635. // We decrease the limit with the number of entries left in the queue
  636. $worker_queues = Config::get("system", "worker_queues", 4);
  637. $queue_length = Config::get('system', 'worker_fetch_limit', 1);
  638. $lower_job_limit = $worker_queues * $queue_length * 2;
  639. $jobs = self::totalEntries();
  640. // Now do some magic
  641. $exponent = 2;
  642. $slope = $queue_length / pow($lower_job_limit, $exponent);
  643. $limit = min($queue_length, ceil($slope * pow($jobs, $exponent)));
  644. logger('Total: '.$jobs.' - Maximum: '.$queue_length.' - jobs per queue: '.$limit, LOGGER_DEBUG);
  645. if (self::passingSlow($highest_priority)) {
  646. // Are there waiting processes with a higher priority than the currently highest?
  647. $result = dba::select(
  648. 'workerqueue',
  649. array('id'),
  650. array("`executed` <= ? AND `priority` < ? AND NOT `done`", NULL_DATE, $highest_priority),
  651. array('limit' => $limit, 'order' => array('priority', 'created'), 'only_query' => true)
  652. );
  653. while ($id = dba::fetch($result)) {
  654. $ids[] = $id["id"];
  655. }
  656. dba::close($result);
  657. $found = (count($ids) > 0);
  658. if (!$found) {
  659. // Give slower processes some processing time
  660. $result = dba::select(
  661. 'workerqueue',
  662. array('id'),
  663. array("`executed` <= ? AND `priority` > ? AND NOT `done`", NULL_DATE, $highest_priority),
  664. array('limit' => $limit, 'order' => array('priority', 'created'), 'only_query' => true)
  665. );
  666. while ($id = dba::fetch($result)) {
  667. $ids[] = $id["id"];
  668. }
  669. dba::close($result);
  670. $found = (count($ids) > 0);
  671. $passing_slow = $found;
  672. }
  673. }
  674. // If there is no result (or we shouldn't pass lower processes) we check without priority limit
  675. if (!$found) {
  676. $result = dba::select(
  677. 'workerqueue',
  678. array('id'),
  679. array("`executed` <= ? AND NOT `done`", NULL_DATE),
  680. array('limit' => $limit, 'order' => array('priority', 'created'), 'only_query' => true)
  681. );
  682. while ($id = dba::fetch($result)) {
  683. $ids[] = $id["id"];
  684. }
  685. dba::close($result);
  686. $found = (count($ids) > 0);
  687. }
  688. if ($found) {
  689. $condition = "`id` IN (".substr(str_repeat("?, ", count($ids)), 0, -2).") AND `pid` = 0 AND NOT `done`";
  690. array_unshift($ids, $condition);
  691. dba::update('workerqueue', array('executed' => datetime_convert(), 'pid' => $mypid), $ids);
  692. }
  693. return $found;
  694. }
  695. /**
  696. * @brief Returns the next worker process
  697. *
  698. * @param boolean $passing_slow Returns if we had passed low priority processes
  699. * @return string SQL statement
  700. */
  701. public static function workerProcess(&$passing_slow)
  702. {
  703. $stamp = (float)microtime(true);
  704. // There can already be jobs for us in the queue.
  705. $r = dba::select('workerqueue', array(), array('pid' => getmypid(), 'done' => false));
  706. if (DBM::is_result($r)) {
  707. self::$db_duration += (microtime(true) - $stamp);
  708. return dba::inArray($r);
  709. }
  710. dba::close($r);
  711. $stamp = (float)microtime(true);
  712. if (!Lock::set('poller_worker_process')) {
  713. return false;
  714. }
  715. self::$lock_duration = (microtime(true) - $stamp);
  716. $stamp = (float)microtime(true);
  717. $found = self::findWorkerProcesses($passing_slow);
  718. self::$db_duration += (microtime(true) - $stamp);
  719. Lock::remove('poller_worker_process');
  720. if ($found) {
  721. $r = dba::select('workerqueue', array(), array('pid' => getmypid(), 'done' => false));
  722. return dba::inArray($r);
  723. }
  724. return false;
  725. }
  726. /**
  727. * @brief Removes a workerqueue entry from the current process
  728. * @return void
  729. */
  730. public static function unclaimProcess()
  731. {
  732. $mypid = getmypid();
  733. dba::update('workerqueue', array('executed' => NULL_DATE, 'pid' => 0), array('pid' => $mypid, 'done' => false));
  734. }
  735. /**
  736. * @brief Call the front end worker
  737. * @return void
  738. */
  739. public static function callWorker()
  740. {
  741. if (!Config::get("system", "frontend_worker")) {
  742. return;
  743. }
  744. $url = System::baseUrl()."/worker";
  745. fetch_url($url, false, $redirects, 1);
  746. }
  747. /**
  748. * @brief Call the front end worker if there aren't any active
  749. * @return void
  750. */
  751. public static function executeIfIdle()
  752. {
  753. if (!Config::get("system", "frontend_worker")) {
  754. return;
  755. }
  756. // Do we have "proc_open"? Then we can fork the worker
  757. if (function_exists("proc_open")) {
  758. // When was the last time that we called the worker?
  759. // Less than one minute? Then we quit
  760. if ((time() - Config::get("system", "worker_started")) < 60) {
  761. return;
  762. }
  763. Config::set("system", "worker_started", time());
  764. // Do we have enough running workers? Then we quit here.
  765. if (self::tooMuchWorkers()) {
  766. // Cleaning dead processes
  767. self::killStaleWorkers();
  768. get_app()->remove_inactive_processes();
  769. return;
  770. }
  771. self::runCron();
  772. logger('Call worker', LOGGER_DEBUG);
  773. self::spawnWorker();
  774. return;
  775. }
  776. // We cannot execute background processes.
  777. // We now run the processes from the frontend.
  778. // This won't work with long running processes.
  779. self::runCron();
  780. self::clearProcesses();
  781. $workers = dba::fetch_first("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'worker.php'");
  782. if ($workers["processes"] == 0) {
  783. self::callWorker();
  784. }
  785. }
  786. /**
  787. * @brief Removes long running worker processes
  788. * @return void
  789. */
  790. public static function clearProcesses()
  791. {
  792. $timeout = Config::get("system", "frontend_worker_timeout", 10);
  793. /// @todo We should clean up the corresponding workerqueue entries as well
  794. $condition = array("`created` < ? AND `command` = 'worker.php'",
  795. datetime_convert('UTC', 'UTC', "now - ".$timeout." minutes"));
  796. dba::delete('process', $condition);
  797. }
  798. /**
  799. * @brief Runs the cron processes
  800. * @return void
  801. */
  802. private static function runCron()
  803. {
  804. logger('Add cron entries', LOGGER_DEBUG);
  805. // Check for spooled items
  806. self::add(PRIORITY_HIGH, "SpoolPost");
  807. // Run the cron job that calls all other jobs
  808. self::add(PRIORITY_MEDIUM, "Cron");
  809. // Run the cronhooks job separately from cron for being able to use a different timing
  810. self::add(PRIORITY_MEDIUM, "CronHooks");
  811. // Cleaning dead processes
  812. self::killStaleWorkers();
  813. }
  814. public static function spawnWorker() {
  815. $args = array("scripts/worker.php", "no_cron");
  816. get_app()->proc_run($args);
  817. }
  818. /**
  819. * @brief Adds tasks to the worker queue
  820. *
  821. * @param (integer|array) priority or parameter array, strings are deprecated and are ignored
  822. *
  823. * next args are passed as $cmd command line
  824. * or: Worker::add(PRIORITY_HIGH, "Notifier", "drop", $drop_id);
  825. * or: Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "CreateShadowEntry", $post_id);
  826. *
  827. * @note $cmd and string args are surrounded with ""
  828. *
  829. * @hooks 'proc_run'
  830. * array $arr
  831. *
  832. * @return boolean "false" if proc_run couldn't be executed
  833. */
  834. public static function add($cmd)
  835. {
  836. $proc_args = func_get_args();
  837. $args = array();
  838. if (!count($proc_args)) {
  839. return false;
  840. }
  841. // Preserve the first parameter
  842. // It could contain a command, the priority or an parameter array
  843. // If we use the parameter array we have to protect it from the following function
  844. $run_parameter = array_shift($proc_args);
  845. // expand any arrays
  846. foreach ($proc_args as $arg) {
  847. if (is_array($arg)) {
  848. foreach ($arg as $n) {
  849. $args[] = $n;
  850. }
  851. } else {
  852. $args[] = $arg;
  853. }
  854. }
  855. // Now we add the run parameters back to the array
  856. array_unshift($args, $run_parameter);
  857. $arr = array('args' => $args, 'run_cmd' => true);
  858. call_hooks("proc_run", $arr);
  859. if (!$arr['run_cmd'] || !count($args)) {
  860. return true;
  861. }
  862. $priority = PRIORITY_MEDIUM;
  863. $dont_fork = Config::get("system", "worker_dont_fork");
  864. $created = datetime_convert();
  865. if (is_int($run_parameter)) {
  866. $priority = $run_parameter;
  867. } elseif (is_array($run_parameter)) {
  868. if (isset($run_parameter['priority'])) {
  869. $priority = $run_parameter['priority'];
  870. }
  871. if (isset($run_parameter['created'])) {
  872. $created = $run_parameter['created'];
  873. }
  874. if (isset($run_parameter['dont_fork'])) {
  875. $dont_fork = $run_parameter['dont_fork'];
  876. }
  877. }
  878. $argv = $args;
  879. array_shift($argv);
  880. $parameters = json_encode($argv);
  881. $found = dba::exists('workerqueue', array('parameter' => $parameters, 'done' => false));
  882. // Quit if there was a database error - a precaution for the update process to 3.5.3
  883. if (dba::errorNo() != 0) {
  884. return false;
  885. }
  886. if (!$found) {
  887. dba::insert('workerqueue', array('parameter' => $parameters, 'created' => $created, 'priority' => $priority));
  888. }
  889. // Should we quit and wait for the worker to be called as a cronjob?
  890. if ($dont_fork) {
  891. return true;
  892. }
  893. // If there is a lock then we don't have to check for too much worker
  894. if (!Lock::set('poller_worker', 0)) {
  895. return true;
  896. }
  897. // If there are already enough workers running, don't fork another one
  898. $quit = self::tooMuchWorkers();
  899. Lock::remove('poller_worker');
  900. if ($quit) {
  901. return true;
  902. }
  903. // Now call the worker to execute the jobs that we just added to the queue
  904. self::spawnWorker();
  905. return true;
  906. }
  907. }