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.

1225 lines
37KB

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