* @return boolean was the lock successful?
*/
static public function lock($table) {
- return self::e("LOCK TABLES `".self::$dbo->escape($table)."` WRITE");
+ // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
+ self::e("SET autocommit=0");
+ $success = self::e("LOCK TABLES `".self::$dbo->escape($table)."` WRITE");
+ if (!$success) {
+ self::e("SET autocommit=1");
+ } else {
+ self::$in_transaction = true;
+ }
+ return $success;
}
/**
* @return boolean was the unlock successful?
*/
static public function unlock() {
- return self::e("UNLOCK TABLES");
+ // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
+ self::e("COMMIT");
+ $success = self::e("UNLOCK TABLES");
+ self::e("SET autocommit=1");
+ self::$in_transaction = false;
+ return $success;
}
/**
$highest_priority = 0;
if (poller_passing_slow($highest_priority)) {
- dba::e('LOCK TABLES `workerqueue` WRITE');
+ dba::lock('workerqueue');
// Are there waiting processes with a higher priority than the currently highest?
$r = q("SELECT * FROM `workerqueue`
return $r;
}
} else {
- dba::e('LOCK TABLES `workerqueue` WRITE');
+ dba::lock('workerqueue');
}
// If there is no result (or we shouldn't pass lower processes) we check without priority limit
// We only unlock the tables here, when we got no data
if (!dbm::is_result($r)) {
- dba::e('UNLOCK TABLES');
+ dba::unlock();
}
return $r;
$success = dba::update('workerqueue', array('executed' => datetime_convert(), 'pid' => $mypid),
array('id' => $queue["id"], 'pid' => 0));
- dba::e('UNLOCK TABLES');
+ dba::unlock();
if (!$success) {
logger("Couldn't update queue entry ".$queue["id"]." - skip this execution", LOGGER_DEBUG);
if (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS)))
$contact["url"] = clean_contact_url($contact["url"]);
- $r = q("SELECT `id`, `last_contact`, `last_failure`, `network` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 2",
+ dba::lock('gcontact');
+ $r = q("SELECT `id`, `last_contact`, `last_failure`, `network` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
dbesc(normalise_link($contact["url"])));
- if ($r) {
+ if (dbm::is_result($r)) {
$gcontact_id = $r[0]["id"];
// Update every 90 days
$doprobing = in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""));
}
}
+ dba::unlock();
if ($doprobing) {
logger("Last Contact: ". $last_contact_str." - Last Failure: ".$last_failure_str." - Checking: ".$contact["url"], LOGGER_DEBUG);
proc_run(PRIORITY_LOW, 'include/gprobe.php', bin2hex($contact["url"]));
}
- if ((dbm::is_result($r)) AND (count($r) > 1) AND ($gcontact_id > 0) AND ($contact["url"] != ""))
- q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d",
- dbesc(normalise_link($contact["url"])),
- intval($gcontact_id));
-
return $gcontact_id;
}
$memcache = self::memcache();
if (is_object($memcache)) {
- $wait_sec = 1;
+ $wait_sec = 0.2;
$cachekey = get_app()->get_hostname().";lock:".$fn_name;
do {
$got_lock = true;
}
if (!$got_lock) {
- sleep($wait_sec);
+ usleep($wait_sec * 1000000);
}
- } while (!$got_lock AND ((time() - $start) < $timeout));
+ } while (!$got_lock AND ((time(true) - $start) < $timeout));
return $got_lock;
}