Locking seems to work great now

This commit is contained in:
Michael 2017-06-04 18:59:50 +00:00
parent bca5776e9c
commit 30b24a2908
2 changed files with 15 additions and 15 deletions

View File

@ -45,33 +45,32 @@ function poller_run($argv, $argc){
$a->start_process(); $a->start_process();
if ($a->min_memory_reached()) { if ($a->min_memory_reached()) {
logger('Pre check: Memory limit reached, quitting.', LOGGER_DEBUG);
return; return;
} }
if (poller_max_connections_reached()) { if (poller_max_connections_reached()) {
logger('Pre check: maximum connections reached, quitting.', LOGGER_DEBUG);
return; return;
} }
if ($a->maxload_reached()) { if ($a->maxload_reached()) {
logger('Pre check: maximum load reached, quitting.', LOGGER_DEBUG);
return; return;
} }
if ($a->max_processes_reached()) { if ($a->max_processes_reached()) {
return; logger('Pre check: maximum processes reached, quitting.', LOGGER_DEBUG);
}
if (!Lock::set('poller_worker')) {
return; return;
} }
// Checking the number of workers // Checking the number of workers
if (poller_too_much_workers()) { if (poller_too_much_workers()) {
poller_kill_stale_workers(); poller_kill_stale_workers();
logger('Pre check: Active worker limit reached, quitting.', LOGGER_DEBUG);
return; return;
} }
Lock::remove('poller_worker');
$starttime = time(); $starttime = time();
while ($r = poller_worker_process()) { while ($r = poller_worker_process()) {
@ -87,7 +86,9 @@ function poller_run($argv, $argc){
} }
if (!Lock::set('poller_worker')) { if (!Lock::set('poller_worker')) {
return; logger('Cannot get a lock, retrying.', LOGGER_DEBUG);
poller_unclaim_process();
continue;
} }
// Count active workers and compare them with a maximum value that depends on the load // Count active workers and compare them with a maximum value that depends on the load
@ -104,7 +105,7 @@ function poller_run($argv, $argc){
} }
// Quit the poller once every hour // Quit the poller once every hour
if (time() > ($starttime + 3600)) { if (time() > ($starttime + 360)) {
logger('Process lifetime reachted, quitting.', LOGGER_DEBUG); logger('Process lifetime reachted, quitting.', LOGGER_DEBUG);
return; return;
} }
@ -733,7 +734,7 @@ function poller_run_cron() {
if (array_search(__file__,get_included_files())===0){ if (array_search(__file__,get_included_files())===0){
poller_run($_SERVER["argv"],$_SERVER["argc"]); poller_run($_SERVER["argv"],$_SERVER["argc"]);
Lock::removeAll(); Lock::remove('poller_worker');
poller_unclaim_process(); poller_unclaim_process();

View File

@ -48,20 +48,16 @@ class Lock {
* *
* @param string $fn_name Name of the lock * @param string $fn_name Name of the lock
* @param integer $timeout Seconds until we give up * @param integer $timeout Seconds until we give up
* @param integer $wait_sec Time between to lock attempts
* *
* @return boolean Was the lock successful? * @return boolean Was the lock successful?
*/ */
public static function set($fn_name, $timeout = 30, $wait_sec = 2) { public static function set($fn_name, $timeout = 120) {
if ($wait_sec == 0) {
$wait_sec = 2;
}
$got_lock = false; $got_lock = false;
$start = time(); $start = time();
$memcache = self::memcache(); $memcache = self::memcache();
if (is_object($memcache)) { if (is_object($memcache)) {
$wait_sec = 1;
$cachekey = get_app()->get_hostname().";lock:".$fn_name; $cachekey = get_app()->get_hostname().";lock:".$fn_name;
do { do {
@ -87,6 +83,9 @@ class Lock {
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));