Merge remote-tracking branch 'upstream/develop' into 1706-diaspora2

This commit is contained in:
Michael 2017-06-16 06:10:35 +00:00
commit 5a11ef5b90
9 changed files with 228 additions and 150 deletions

View file

@ -22,6 +22,7 @@ require_once(__DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'a
use Friendica\App; use Friendica\App;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Util\Lock;
require_once 'include/config.php'; require_once 'include/config.php';
require_once 'include/network.php'; require_once 'include/network.php';
@ -1069,6 +1070,7 @@ function proc_run($cmd) {
$priority = PRIORITY_MEDIUM; $priority = PRIORITY_MEDIUM;
$dont_fork = get_config("system", "worker_dont_fork"); $dont_fork = get_config("system", "worker_dont_fork");
$created = datetime_convert();
if (is_int($run_parameter)) { if (is_int($run_parameter)) {
$priority = $run_parameter; $priority = $run_parameter;
@ -1076,6 +1078,9 @@ function proc_run($cmd) {
if (isset($run_parameter['priority'])) { if (isset($run_parameter['priority'])) {
$priority = $run_parameter['priority']; $priority = $run_parameter['priority'];
} }
if (isset($run_parameter['created'])) {
$created = $run_parameter['created'];
}
if (isset($run_parameter['dont_fork'])) { if (isset($run_parameter['dont_fork'])) {
$dont_fork = $run_parameter['dont_fork']; $dont_fork = $run_parameter['dont_fork'];
} }
@ -1088,7 +1093,7 @@ function proc_run($cmd) {
$found = dba::select('workerqueue', array('id'), array('parameter' => $parameters), array('limit' => 1)); $found = dba::select('workerqueue', array('id'), array('parameter' => $parameters), array('limit' => 1));
if (!dbm::is_result($found)) { if (!dbm::is_result($found)) {
dba::insert('workerqueue', array('parameter' => $parameters, 'created' => datetime_convert(), 'priority' => $priority)); dba::insert('workerqueue', array('parameter' => $parameters, 'created' => $created, 'priority' => $priority));
} }
// Should we quit and wait for the poller to be called as a cronjob? // Should we quit and wait for the poller to be called as a cronjob?
@ -1096,8 +1101,16 @@ function proc_run($cmd) {
return; return;
} }
// If there is a lock then we don't have to check for too much worker
if (!Lock::set('poller_worker', 0)) {
return;
}
// If there are already enough workers running, don't fork another one // If there are already enough workers running, don't fork another one
if (poller_too_much_workers()) { $quit = poller_too_much_workers();
Lock::remove('poller_worker');
if ($quit) {
return; return;
} }

View file

@ -68,6 +68,7 @@ Example: To set the directory value please add this line to your .htconfig.php:
* **ostatus_poll_timeframe** - Defines how old an item can be to try to complete the conversation with it. * **ostatus_poll_timeframe** - Defines how old an item can be to try to complete the conversation with it.
* **paranoia** (Boolean) - Log out users if their IP address changed. * **paranoia** (Boolean) - Log out users if their IP address changed.
* **permit_crawling** (Boolean) - Restricts the search for not logged in users to one search per minute. * **permit_crawling** (Boolean) - Restricts the search for not logged in users to one search per minute.
* **worker_debug** (Boolean) - If enabled, it prints out the number of running processes split by priority.
* **profiler** (Boolean) - Enable internal timings to help optimize code. Needed for "rendertime" addon. Default is false. * **profiler** (Boolean) - Enable internal timings to help optimize code. Needed for "rendertime" addon. Default is false.
* **free_crawls** - Number of "free" searches when "permit_crawling" is activated (Default value is 10) * **free_crawls** - Number of "free" searches when "permit_crawling" is activated (Default value is 10)
* **crawl_permit_period** - Period in seconds between allowed searches when the number of free searches is reached and "permit_crawling" is activated (Default value is 60) * **crawl_permit_period** - Period in seconds between allowed searches when the number of free searches is reached and "permit_crawling" is activated (Default value is 60)

View file

@ -21,6 +21,8 @@ class dba {
private $driver; private $driver;
public $connected = false; public $connected = false;
public $error = false; public $error = false;
public $errorno = 0;
public $affected_rows = 0;
private $_server_info = ''; private $_server_info = '';
private static $in_transaction = false; private static $in_transaction = false;
private static $dbo; private static $dbo;
@ -551,6 +553,7 @@ class dba {
self::$dbo->error = ''; self::$dbo->error = '';
self::$dbo->errorno = 0; self::$dbo->errorno = 0;
self::$dbo->affected_rows = 0;
switch (self::$dbo->driver) { switch (self::$dbo->driver) {
case 'pdo': case 'pdo':
@ -573,6 +576,7 @@ class dba {
$retval = false; $retval = false;
} else { } else {
$retval = $stmt; $retval = $stmt;
self::$dbo->affected_rows = $retval->rowCount();
} }
break; break;
case 'mysqli': case 'mysqli':
@ -612,6 +616,7 @@ class dba {
} else { } else {
$stmt->store_result(); $stmt->store_result();
$retval = $stmt; $retval = $stmt;
self::$dbo->affected_rows = $retval->affected_rows;
} }
break; break;
case 'mysql': case 'mysql':
@ -620,13 +625,28 @@ class dba {
if (mysql_errno(self::$dbo->db)) { if (mysql_errno(self::$dbo->db)) {
self::$dbo->error = mysql_error(self::$dbo->db); self::$dbo->error = mysql_error(self::$dbo->db);
self::$dbo->errorno = mysql_errno(self::$dbo->db); self::$dbo->errorno = mysql_errno(self::$dbo->db);
} else {
self::$dbo->affected_rows = mysql_affected_rows($retval);
} }
break; break;
} }
if (self::$dbo->errorno != 0) { if (self::$dbo->errorno != 0) {
logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n". $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
$a->callstack(8))."\n".self::replace_parameters($sql, $args); $called_from = array_shift($trace);
// We are having an own error logging in the function "p"
if ($called_from['function'] != 'p') {
// We have to preserve the error code, somewhere in the logging it get lost
$error = self::$dbo->error;
$errorno = self::$dbo->errorno;
logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n".
$a->callstack(8))."\n".self::replace_parameters($sql, $args);
self::$dbo->error = $error;
self::$dbo->errorno = $errorno;
}
} }
$a->save_timestamp($stamp1, 'database'); $a->save_timestamp($stamp1, 'database');
@ -662,18 +682,36 @@ class dba {
$args = func_get_args(); $args = func_get_args();
$stmt = call_user_func_array('self::p', $args); // In a case of a deadlock we are repeating the query 20 times
$timeout = 20;
if (is_bool($stmt)) { do {
$retval = $stmt; $stmt = call_user_func_array('self::p', $args);
} elseif (is_object($stmt)) {
$retval = true; if (is_bool($stmt)) {
} else { $retval = $stmt;
$retval = false; } elseif (is_object($stmt)) {
$retval = true;
} else {
$retval = false;
}
self::close($stmt);
} while ((self::$dbo->errorno == 1213) && (--$timeout > 0));
if (self::$dbo->errorno != 0) {
// We have to preserve the error code, somewhere in the logging it get lost
$error = self::$dbo->error;
$errorno = self::$dbo->errorno;
logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n".
$a->callstack(8))."\n".self::replace_parameters($sql, $args);
self::$dbo->error = $error;
self::$dbo->errorno = $errorno;
} }
self::close($stmt);
$a->save_timestamp($stamp, "database_write"); $a->save_timestamp($stamp, "database_write");
return $retval; return $retval;
@ -723,6 +761,15 @@ class dba {
return $retval; return $retval;
} }
/**
* @brief Returns the number of affected rows of the last statement
*
* @return int Number of rows
*/
static public function affected_rows() {
return self::$dbo->affected_rows;
}
/** /**
* @brief Returns the number of rows of a statement * @brief Returns the number of rows of a statement
* *

View file

@ -1139,7 +1139,7 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
check_item_notification($current_post, $uid); check_item_notification($current_post, $uid);
if ($notify) { if ($notify) {
proc_run(PRIORITY_HIGH, "include/notifier.php", $notify_type, $current_post); proc_run(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "include/notifier.php", $notify_type, $current_post);
} }
return $current_post; return $current_post;
@ -1430,7 +1430,7 @@ function tag_deliver($uid, $item_id) {
); );
update_thread($item_id); update_thread($item_id);
proc_run(PRIORITY_HIGH,'include/notifier.php', 'tgroup', $item_id); proc_run(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), 'include/notifier.php', 'tgroup', $item_id);
} }
@ -2076,7 +2076,7 @@ function item_expire($uid, $days, $network = "", $force = false) {
drop_item($item['id'], false); drop_item($item['id'], false);
} }
proc_run(PRIORITY_LOW, "include/notifier.php", "expire", $uid); proc_run(array('priority' => PRIORITY_LOW, 'dont_fork' => true), "include/notifier.php", "expire", $uid);
} }
@ -2099,7 +2099,7 @@ function drop_items($items) {
// multiple threads may have been deleted, send an expire notification // multiple threads may have been deleted, send an expire notification
if ($uid) { if ($uid) {
proc_run(PRIORITY_LOW, "include/notifier.php", "expire", $uid); proc_run(array('priority' => PRIORITY_LOW, 'dont_fork' => true), "include/notifier.php", "expire", $uid);
} }
} }
@ -2295,7 +2295,7 @@ function drop_item($id, $interactive = true) {
$drop_id = intval($item['id']); $drop_id = intval($item['id']);
$priority = ($interactive ? PRIORITY_HIGH : PRIORITY_LOW); $priority = ($interactive ? PRIORITY_HIGH : PRIORITY_LOW);
proc_run($priority, "include/notifier.php", "drop", $drop_id); proc_run(array('priority' => $priority, 'dont_fork' => true), "include/notifier.php", "drop", $drop_id);
if (! $interactive) { if (! $interactive) {
return $owner; return $owner;

View file

@ -55,17 +55,6 @@ function notifier_run(&$argv, &$argc){
return; return;
} }
// Inherit the priority
$queue = dba::select('workerqueue', array('priority'), array('pid' => getmypid()), array('limit' => 1));
if (dbm::is_result($queue)) {
$priority = (int)$queue['priority'];
logger('inherited priority: '.$priority);
} else {
// Normally this shouldn't happen.
$priority = PRIORITY_HIGH;
logger('no inherited priority! Something is wrong.');
}
logger('notifier: invoked: ' . print_r($argv,true), LOGGER_DEBUG); logger('notifier: invoked: ' . print_r($argv,true), LOGGER_DEBUG);
$cmd = $argv[1]; $cmd = $argv[1];
@ -359,7 +348,7 @@ function notifier_run(&$argv, &$argc){
// a delivery fork. private groups (forum_mode == 2) do not uplink // a delivery fork. private groups (forum_mode == 2) do not uplink
if ((intval($parent['forum_mode']) == 1) && (! $top_level) && ($cmd !== 'uplink')) { if ((intval($parent['forum_mode']) == 1) && (! $top_level) && ($cmd !== 'uplink')) {
proc_run($priority, 'include/notifier.php', 'uplink', $item_id); proc_run($a->queue['priority'], 'include/notifier.php', 'uplink', $item_id);
} }
$conversants = array(); $conversants = array();
@ -498,7 +487,8 @@ function notifier_run(&$argv, &$argc){
} }
logger("Deliver ".$target_item["guid"]." to ".$contact['url']." via network ".$contact['network'], LOGGER_DEBUG); logger("Deliver ".$target_item["guid"]." to ".$contact['url']." via network ".$contact['network'], LOGGER_DEBUG);
proc_run(array('priority' => $priority, 'dont_fork' => true), 'include/delivery.php', $cmd, $item_id, $contact['id']); proc_run(array('priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true),
'include/delivery.php', $cmd, $item_id, (int)$contact['id']);
} }
} }
@ -563,7 +553,8 @@ function notifier_run(&$argv, &$argc){
if ((! $mail) && (! $fsuggest) && (! $followup)) { if ((! $mail) && (! $fsuggest) && (! $followup)) {
logger('notifier: delivery agent: '.$rr['name'].' '.$rr['id'].' '.$rr['network'].' '.$target_item["guid"]); logger('notifier: delivery agent: '.$rr['name'].' '.$rr['id'].' '.$rr['network'].' '.$target_item["guid"]);
proc_run(array('priority' => $priority, 'dont_fork' => true), 'include/delivery.php', $cmd, $item_id, $rr['id']); proc_run(array('priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true),
'include/delivery.php', $cmd, $item_id, (int)$rr['id']);
} }
} }
} }
@ -603,7 +594,8 @@ function notifier_run(&$argv, &$argc){
} }
// Handling the pubsubhubbub requests // Handling the pubsubhubbub requests
proc_run(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), 'include/pubsubpublish.php'); proc_run(array('priority' => PRIORITY_HIGH, 'created' => $a->queue['created'], 'dont_fork' => true),
'include/pubsubpublish.php');
} }
logger('notifier: calling hooks', LOGGER_DEBUG); logger('notifier: calling hooks', LOGGER_DEBUG);

View file

@ -47,11 +47,15 @@ function poller_run($argv, $argc){
// We now start the process. This is done after the load check since this could increase the load. // We now start the process. This is done after the load check since this could increase the load.
$a->start_process(); $a->start_process();
// At first we check the number of workers and quit if there are too much of them // Kill stale processes every 5 minutes
// This is done at the top to avoid that too much code is executed without a need to do so, $last_cleanup = Config::get('system', 'poller_last_cleaned', 0);
// since the poller mostly quits here. if (time() > ($last_cleanup + 300)) {
if (poller_too_much_workers()) { Config::set('system', 'poller_last_cleaned', time());
poller_kill_stale_workers(); poller_kill_stale_workers();
}
// Count active workers and compare them with a maximum value that depends on the load
if (poller_too_much_workers()) {
logger('Pre check: Active worker limit reached, quitting.', LOGGER_DEBUG); logger('Pre check: Active worker limit reached, quitting.', LOGGER_DEBUG);
return; return;
} }
@ -83,10 +87,15 @@ function poller_run($argv, $argc){
// We fetch the next queue entry that is about to be executed // We fetch the next queue entry that is about to be executed
while ($r = poller_worker_process()) { while ($r = poller_worker_process()) {
foreach ($r AS $entry) {
// Assure that the priority is an integer value
$entry['priority'] = (int)$entry['priority'];
// If we got that queue entry we claim it for us // The work will be done
if (!poller_claim_process($r[0])) { if (!poller_execute($entry)) {
continue; logger('Process execution failed, quitting.', LOGGER_DEBUG);
return;
}
} }
// To avoid the quitting of multiple pollers only one poller at a time will execute the check // To avoid the quitting of multiple pollers only one poller at a time will execute the check
@ -105,14 +114,8 @@ function poller_run($argv, $argc){
Lock::remove('poller_worker'); Lock::remove('poller_worker');
} }
// finally the work will be done // Quit the poller once every 5 minutes
if (!poller_execute($r[0])) { if (time() > ($starttime + 300)) {
logger('Process execution failed, quitting.', LOGGER_DEBUG);
return;
}
// Quit the poller once every hour
if (time() > ($starttime + 3600)) {
logger('Process lifetime reached, quitting.', LOGGER_DEBUG); logger('Process lifetime reached, quitting.', LOGGER_DEBUG);
return; return;
} }
@ -120,6 +123,47 @@ function poller_run($argv, $argc){
logger("Couldn't select a workerqueue entry, quitting.", LOGGER_DEBUG); logger("Couldn't select a workerqueue entry, quitting.", LOGGER_DEBUG);
} }
/**
* @brief Returns the number of non executed entries in the worker queue
*
* @return integer Number of non executed entries in the worker queue
*/
function poller_total_entries() {
$s = q("SELECT COUNT(*) AS `total` FROM `workerqueue` WHERE `executed` <= '%s'", dbesc(NULL_DATE));
if (dbm::is_result($s)) {
return $s[0]["total"];
} else {
return 0;
}
}
/**
* @brief Returns the highest priority in the worker queue that isn't executed
*
* @return integer Number of active poller processes
*/
function poller_highest_priority() {
$s = q("SELECT `priority` FROM `workerqueue` WHERE `executed` <= '%s' ORDER BY `priority` LIMIT 1", dbesc(NULL_DATE));
if (dbm::is_result($s)) {
return $s[0]["priority"];
} else {
return 0;
}
}
/**
* @brief Returns if a process with the given priority is running
*
* @param integer $priority The priority that should be checked
*
* @return integer Is there a process running with that priority?
*/
function poller_process_with_priority_active($priority) {
$s = q("SELECT `id` FROM `workerqueue` WHERE `priority` <= %d AND `executed` > '%s' LIMIT 1",
intval($priority), dbesc(NULL_DATE));
return dbm::is_result($s);
}
/** /**
* @brief Execute a worker entry * @brief Execute a worker entry
* *
@ -214,10 +258,12 @@ function poller_exec_function($queue, $funcname, $argv) {
// But preserve the old one for the worker // But preserve the old one for the worker
$old_process_id = $a->process_id; $old_process_id = $a->process_id;
$a->process_id = uniqid("wrk", true); $a->process_id = uniqid("wrk", true);
$a->queue = $queue;
$funcname($argv, $argc); $funcname($argv, $argc);
$a->process_id = $old_process_id; $a->process_id = $old_process_id;
unset($a->queue);
$duration = number_format(microtime(true) - $stamp, 3); $duration = number_format(microtime(true) - $stamp, 3);
@ -442,38 +488,35 @@ function poller_too_much_workers() {
$slope = $maxworkers / pow($maxsysload, $exponent); $slope = $maxworkers / pow($maxsysload, $exponent);
$queues = ceil($slope * pow(max(0, $maxsysload - $load), $exponent)); $queues = ceil($slope * pow(max(0, $maxsysload - $load), $exponent));
// Create a list of queue entries grouped by their priority if (Config::get('system', 'worker_debug')) {
$listitem = array(); // Create a list of queue entries grouped by their priority
$listitem = array();
// Adding all processes with no workerqueue entry // Adding all processes with no workerqueue entry
$processes = dba::p("SELECT COUNT(*) AS `running` FROM `process` WHERE NOT EXISTS (SELECT id FROM `workerqueue` WHERE `workerqueue`.`pid` = `process`.`pid`)"); $processes = dba::p("SELECT COUNT(*) AS `running` FROM `process` WHERE NOT EXISTS (SELECT id FROM `workerqueue` WHERE `workerqueue`.`pid` = `process`.`pid`)");
if ($process = dba::fetch($processes)) {
$listitem[0] = "0:".$process["running"];
}
dba::close($processes);
// Now adding all processes with workerqueue entries
$entries = dba::p("SELECT COUNT(*) AS `entries`, `priority` FROM `workerqueue` GROUP BY `priority`");
while ($entry = dba::fetch($entries)) {
$processes = dba::p("SELECT COUNT(*) AS `running` FROM `process` INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid` WHERE `priority` = ?", $entry["priority"]);
if ($process = dba::fetch($processes)) { if ($process = dba::fetch($processes)) {
$listitem[$entry["priority"]] = $entry["priority"].":".$process["running"]."/".$entry["entries"]; $listitem[0] = "0:".$process["running"];
} }
dba::close($processes); dba::close($processes);
}
dba::close($entries);
$processlist = ' ('.implode(', ', $listitem).')';
$s = q("SELECT COUNT(*) AS `total` FROM `workerqueue` WHERE `executed` <= '%s'", dbesc(NULL_DATE)); // Now adding all processes with workerqueue entries
$entries = $s[0]["total"]; $entries = dba::p("SELECT COUNT(*) AS `entries`, `priority` FROM `workerqueue` GROUP BY `priority`");
while ($entry = dba::fetch($entries)) {
$processes = dba::p("SELECT COUNT(*) AS `running` FROM `process` INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid` WHERE `priority` = ?", $entry["priority"]);
if ($process = dba::fetch($processes)) {
$listitem[$entry["priority"]] = $entry["priority"].":".$process["running"]."/".$entry["entries"];
}
dba::close($processes);
}
dba::close($entries);
$processlist = ' ('.implode(', ', $listitem).')';
}
$entries = poller_total_entries();
if (Config::get("system", "worker_fastlane", false) && ($queues > 0) && ($entries > 0) && ($active >= $queues)) { if (Config::get("system", "worker_fastlane", false) && ($queues > 0) && ($entries > 0) && ($active >= $queues)) {
$s = q("SELECT `priority` FROM `workerqueue` WHERE `executed` <= '%s' ORDER BY `priority` LIMIT 1", dbesc(NULL_DATE)); $top_priority = poller_highest_priority();
$top_priority = $s[0]["priority"]; $high_running = poller_process_with_priority_active($top_priority);
$s = q("SELECT `id` FROM `workerqueue` WHERE `priority` <= %d AND `executed` > '%s' LIMIT 1",
intval($top_priority), dbesc(NULL_DATE));
$high_running = dbm::is_result($s);
if (!$high_running && ($top_priority > PRIORITY_UNDEFINED) && ($top_priority < PRIORITY_NEGLIGIBLE)) { if (!$high_running && ($top_priority > PRIORITY_UNDEFINED) && ($top_priority < PRIORITY_NEGLIGIBLE)) {
logger("There are jobs with priority ".$top_priority." waiting but none is executed. Open a fastlane.", LOGGER_DEBUG); logger("There are jobs with priority ".$top_priority." waiting but none is executed. Open a fastlane.", LOGGER_DEBUG);
@ -557,6 +600,49 @@ function poller_passing_slow(&$highest_priority) {
return $passing_slow; return $passing_slow;
} }
/**
* @brief Find and claim the next worker process for us
*
* @return boolean Have we found something?
*/
function find_worker_processes() {
// Check if we should pass some low priority process
$highest_priority = 0;
$found = false;
if (poller_passing_slow($highest_priority)) {
// Are there waiting processes with a higher priority than the currently highest?
$result = dba::e("UPDATE `workerqueue` SET `executed` = ?, `pid` = ?
WHERE `executed` <= ? AND `priority` < ?
ORDER BY `priority`, `created` LIMIT 5",
datetime_convert(), getmypid(), NULL_DATE, $highest_priority);
if ($result) {
$found = (dba::affected_rows() > 0);
}
if (!$found) {
// Give slower processes some processing time
$result = dba::e("UPDATE `workerqueue` SET `executed` = ?, `pid` = ?
WHERE `executed` <= ? AND `priority` > ?
ORDER BY `priority`, `created` LIMIT 1",
datetime_convert(), getmypid(), NULL_DATE, $highest_priority);
if ($result) {
$found = (dba::affected_rows() > 0);
}
}
}
// If there is no result (or we shouldn't pass lower processes) we check without priority limit
if (!$found) {
$result = dba::e("UPDATE `workerqueue` SET `executed` = ?, `pid` = ? WHERE `executed` <= ? ORDER BY `priority`, `created` LIMIT 5",
datetime_convert(), getmypid(), NULL_DATE);
if ($result) {
$found = (dba::affected_rows() > 0);
}
}
return $found;
}
/** /**
* @brief Returns the next worker process * @brief Returns the next worker process
* *
@ -564,84 +650,18 @@ function poller_passing_slow(&$highest_priority) {
*/ */
function poller_worker_process() { function poller_worker_process() {
// Check if we should pass some low priority process $stamp = (float)microtime(true);
$highest_priority = 0;
if (poller_passing_slow($highest_priority)) { $found = find_worker_processes();
dba::lock('workerqueue');
// Are there waiting processes with a higher priority than the currently highest? logger('Duration: '.number_format(microtime(true) - $stamp, 3), LOGGER_DEBUG);
$r = q("SELECT * FROM `workerqueue`
WHERE `executed` <= '%s' AND `priority` < %d
ORDER BY `priority`, `created` LIMIT 1",
dbesc(NULL_DATE),
intval($highest_priority));
if (dbm::is_result($r)) {
return $r;
}
// Give slower processes some processing time
$r = q("SELECT * FROM `workerqueue`
WHERE `executed` <= '%s' AND `priority` > %d
ORDER BY `priority`, `created` LIMIT 1",
dbesc(NULL_DATE),
intval($highest_priority));
if (dbm::is_result($r)) { if ($found) {
return $r; $r = q("SELECT * FROM `workerqueue` WHERE `pid` = %d", intval(getmypid()));
}
} else {
dba::lock('workerqueue');
} }
// If there is no result (or we shouldn't pass lower processes) we check without priority limit
if (!dbm::is_result($r)) {
$r = q("SELECT * FROM `workerqueue` WHERE `executed` <= '%s' ORDER BY `priority`, `created` LIMIT 1", dbesc(NULL_DATE));
}
// We only unlock the tables here, when we got no data
if (!dbm::is_result($r)) {
dba::unlock();
}
return $r; return $r;
} }
/**
* @brief Assigns a workerqueue entry to the current process
*
* When we are sure that the table locks are working correctly, we can remove the checks from here
*
* @param array $queue Workerqueue entry
*
* @return boolean "true" if the claiming was successful
*/
function poller_claim_process($queue) {
$mypid = getmypid();
$success = dba::update('workerqueue', array('executed' => datetime_convert(), 'pid' => $mypid),
array('id' => $queue["id"], 'pid' => 0));
dba::unlock();
if (!$success) {
logger("Couldn't update queue entry ".$queue["id"]." - skip this execution", LOGGER_DEBUG);
return false;
}
// Assure that there are no tasks executed twice
$id = q("SELECT `pid`, `executed` FROM `workerqueue` WHERE `id` = %d", intval($queue["id"]));
if (!$id) {
logger("Queue item ".$queue["id"]." vanished - skip this execution", LOGGER_DEBUG);
return false;
} elseif ((strtotime($id[0]["executed"]) <= 0) || ($id[0]["pid"] == 0)) {
logger("Entry for queue item ".$queue["id"]." wasn't stored - skip this execution", LOGGER_DEBUG);
return false;
} elseif ($id[0]["pid"] != $mypid) {
logger("Queue item ".$queue["id"]." is to be executed by process ".$id[0]["pid"]." and not by me (".$mypid.") - skip this execution", LOGGER_DEBUG);
return false;
}
return true;
}
/** /**
* @brief Removes a workerqueue entry from the current process * @brief Removes a workerqueue entry from the current process
*/ */

View file

@ -7,6 +7,7 @@ require_once('include/items.php');
require_once('include/ostatus.php'); require_once('include/ostatus.php');
function pubsubpublish_run(&$argv, &$argc){ function pubsubpublish_run(&$argv, &$argc){
global $a;
if ($argc > 1) { if ($argc > 1) {
$pubsubpublish_id = intval($argv[1]); $pubsubpublish_id = intval($argv[1]);
@ -17,7 +18,8 @@ function pubsubpublish_run(&$argv, &$argc){
foreach ($r as $rr) { foreach ($r as $rr) {
logger("Publish feed to ".$rr["callback_url"], LOGGER_DEBUG); logger("Publish feed to ".$rr["callback_url"], LOGGER_DEBUG);
proc_run(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), 'include/pubsubpublish.php', $rr["id"]); proc_run(array('priority' => PRIORITY_HIGH, 'created' => $a->queue['created'], 'dont_fork' => true),
'include/pubsubpublish.php', (int)$rr["id"]);
} }
} }

View file

@ -100,6 +100,7 @@ class App {
*/ */
public $template_engine_instance = array(); public $template_engine_instance = array();
public $process_id; public $process_id;
public $queue;
private $ldelim = array( private $ldelim = array(
'internal' => '', 'internal' => '',
'smarty3' => '{{' 'smarty3' => '{{'

View file

@ -57,10 +57,11 @@ class Lock {
$memcache = self::connectMemcache(); $memcache = self::connectMemcache();
if (is_object($memcache)) { if (is_object($memcache)) {
$wait_sec = 0.2;
$cachekey = get_app()->get_hostname().";lock:".$fn_name; $cachekey = get_app()->get_hostname().";lock:".$fn_name;
do { do {
// We only lock to be sure that nothing happens at exactly the same time
dba::lock('locks');
$lock = $memcache->get($cachekey); $lock = $memcache->get($cachekey);
if (!is_bool($lock)) { if (!is_bool($lock)) {
@ -76,16 +77,17 @@ class Lock {
$memcache->set($cachekey, getmypid(), MEMCACHE_COMPRESSED, 300); $memcache->set($cachekey, getmypid(), MEMCACHE_COMPRESSED, 300);
$got_lock = true; $got_lock = true;
} }
dba::unlock();
if (!$got_lock && ($timeout > 0)) { if (!$got_lock && ($timeout > 0)) {
usleep($wait_sec * 1000000); usleep(rand(10000, 200000));
} }
} while (!$got_lock && ((time() - $start) < $timeout)); } while (!$got_lock && ((time() - $start) < $timeout));
return $got_lock; return $got_lock;
} }
$wait_sec = 2;
do { do {
dba::lock('locks'); dba::lock('locks');
$lock = dba::select('locks', array('locked', 'pid'), array('name' => $fn_name), array('limit' => 1)); $lock = dba::select('locks', array('locked', 'pid'), array('name' => $fn_name), array('limit' => 1));
@ -113,7 +115,7 @@ class Lock {
dba::unlock(); dba::unlock();
if (!$got_lock && ($timeout > 0)) { if (!$got_lock && ($timeout > 0)) {
sleep($wait_sec); usleep(rand(100000, 2000000));
} }
} while (!$got_lock && ((time() - $start) < $timeout)); } while (!$got_lock && ((time() - $start) < $timeout));