diff --git a/include/dba.php b/include/dba.php index 1fff51f3d1..1f428bf465 100644 --- a/include/dba.php +++ b/include/dba.php @@ -816,7 +816,15 @@ class dba { * @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; } /** @@ -825,7 +833,12 @@ class dba { * @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; } /** diff --git a/include/poller.php b/include/poller.php index 3d53be0abb..1de9126d2d 100644 --- a/include/poller.php +++ b/include/poller.php @@ -573,7 +573,7 @@ function poller_worker_process() { $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` @@ -595,7 +595,7 @@ function poller_worker_process() { 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 @@ -605,7 +605,7 @@ function poller_worker_process() { // We only unlock the tables here, when we got no data if (!dbm::is_result($r)) { - dba::e('UNLOCK TABLES'); + dba::unlock(); } return $r; @@ -625,7 +625,7 @@ function poller_claim_process($queue) { $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); diff --git a/include/socgraph.php b/include/socgraph.php index fbac08cc97..7a39e388be 100644 --- a/include/socgraph.php +++ b/include/socgraph.php @@ -1995,10 +1995,11 @@ function get_gcontact_id($contact) { 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 @@ -2036,17 +2037,13 @@ function get_gcontact_id($contact) { $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; } diff --git a/src/Util/Lock.php b/src/Util/Lock.php index e8011bf59f..7cc3472e69 100644 --- a/src/Util/Lock.php +++ b/src/Util/Lock.php @@ -57,7 +57,7 @@ class Lock { $memcache = self::memcache(); if (is_object($memcache)) { - $wait_sec = 1; + $wait_sec = 0.2; $cachekey = get_app()->get_hostname().";lock:".$fn_name; do { @@ -77,9 +77,9 @@ class Lock { $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; }