From ba2a49e9875eda489385e370bb9f2259779caa7b Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 2 Dec 2014 19:22:48 +0100 Subject: Tweaks and fixes --- inc/property.inc.php | 3 ++- inc/taskmanager.inc.php | 3 ++- inc/taskmanagercallback.inc.php | 56 ++++++++++++++++++++++++++++++++++++++++- inc/trigger.inc.php | 48 +++++++++++++++++------------------ 4 files changed, 83 insertions(+), 27 deletions(-) (limited to 'inc') diff --git a/inc/property.inc.php b/inc/property.inc.php index dd70b8d4..6a639dd2 100644 --- a/inc/property.inc.php +++ b/inc/property.inc.php @@ -57,10 +57,11 @@ class Property return self::get('server-ip', 'none'); } - public static function setServerIp($value) + public static function setServerIp($value, $automatic = false) { if ($value === self::getServerIp()) return false; + EventLog::info('Server IP changed from ' . self::getServerIp() . ' to ' . $value . ($automatic ? ' (auto detected)' : '')); self::set('server-ip', $value); Event::serverIpChanged(); } diff --git a/inc/taskmanager.inc.php b/inc/taskmanager.inc.php index dd16c62d..05db5b5d 100644 --- a/inc/taskmanager.inc.php +++ b/inc/taskmanager.inc.php @@ -128,7 +128,8 @@ class Taskmanager } /** - * Check whether the given task can be considered failed. + * Check whether the given task can be considered failed. This + * includes that the task id is invalid, etc. * * @param array $task struct representing task, obtained by ::status * @return boolean true if task failed, false if finished successfully or still waiting/running diff --git a/inc/taskmanagercallback.inc.php b/inc/taskmanagercallback.inc.php index 3ef4745c..951a1de3 100644 --- a/inc/taskmanagercallback.inc.php +++ b/inc/taskmanagercallback.inc.php @@ -5,7 +5,7 @@ */ class TaskmanagerCallback { - + /** * Add a callback for given task id. This is the only exception in this class, * as this is not a callback, but a function to define one :) @@ -31,6 +31,51 @@ class TaskmanagerCallback )); } + /** + * Get all pending callbacks from the callback table. + * + * @return array list of array(taskid => list of callbacks) + */ + public static function getPendingCallbacks() + { + $retval = array(); + $res = Database::simpleQuery("SELECT taskid, cbfunction FROM callback"); + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $retval[$row['taskid']][] = $row; + } + return $retval; + } + + /** + * Handle the given callback. Will delete the entry from the callback + * table if appropriate. + * + * @param array $callback entry from the callback table (cbfunction + taskid) + * @param array $status status of the task as returned by the taskmanager. If NULL it will be queried. + */ + public static function handleCallback($callback, $status = NULL) + { + if (is_null($status)) + $status = Taskmanager::status($callback['taskid']); + if ($status === false) // No reply from Taskmanager, retry later + return; + if (Taskmanager::isFailed($status) || Taskmanager::isFinished($status)) { + $del = Database::exec("DELETE FROM callback WHERE taskid = :task AND cbfunction = :cb LIMIT 1", array('task' => $callback['taskid'], 'cb' => $callback['cbfunction'])); + if ($del === 0) // No entry deleted, so someone else must have deleted it - race condition, do nothing + return; + } + if (Taskmanager::isFinished($status)) { + $func = array('TaskmanagerCallback', preg_replace('/\W/', '', $callback['cbfunction'])); + if (!call_user_func_array('method_exists', $func)) { + Eventlog::warning("handleCallback: Callback {$callback['cbfunction']} doesn't exist."); + } else { + call_user_func($func, $status); + } + } + } + + // #################################################################### + /** * Result of trying to (re)launch ldadp. */ @@ -40,4 +85,13 @@ class TaskmanagerCallback EventLog::warning("Could not start/stop LDAP-AD-Proxy instances", $task['data']['messages']); } + public static function dbRestored($task) + { + error_log("dbRestored."); + if (Taskmanager::isFinished($task) && !Taskmanager::isFailed($task)) { + error_log("LOGGING."); + EventLog::info('Configuration backup restored.'); + } + } + } diff --git a/inc/trigger.inc.php b/inc/trigger.inc.php index 790323b5..a5a149c8 100644 --- a/inc/trigger.inc.php +++ b/inc/trigger.inc.php @@ -65,11 +65,11 @@ class Trigger } } if ($publicCandidate !== 'none' && $publicCandidate !== 'many') { - Property::setServerIp($publicCandidate); + Property::setServerIp($publicCandidate, true); return true; } if ($privateCandidate !== 'none' && $privateCandidate !== 'many') { - Property::setServerIp($privateCandidate); + Property::setServerIp($privateCandidate, true); return true; } return false; @@ -109,26 +109,29 @@ class Trigger * To be called if the server ip changes, as it's embedded in the AD module configs. * This will then recreate all AD tgz modules. */ - public static function rebuildAdModules() + public static function rebuildAdModules($parent = NULL) { - $task = Taskmanager::submit('LdadpLauncher', array('ids' => array())); // Stop all running instances + $task = Taskmanager::submit('LdadpLauncher', array( + 'parentTask' => $parent, + 'failOnParentFail' => false, + 'ids' => array() + )); // Stop all running instances $ads = ConfigModule::getAdConfigs(); if (empty($ads)) - return; + return false; - $parent = isset($task['id']) ? $task['id'] : NULL; + if (isset($task['id'])) + $parent = $task['id']; foreach ($ads as $ad) { $ad['parentTask'] = $parent; $ad['failOnParentFail'] = false; + $ad['proxyip'] = Property::getServerIp(); $task = Taskmanager::submit('CreateAdConfig', $ad); if (isset($task['id'])) $parent = $task['id']; } - if (Taskmanager::waitComplete($parent, 2000) === false) { - EventLog::warning('Rebuilding LDAP-AD-Proxy config failed. AD Auth might not work properly.'); - sleep(1); - } - Trigger::ldadp(); + Trigger::ldadp($parent); + return $parent; } /** @@ -157,23 +160,20 @@ class Trigger } /** - * Check and process all callbacks + * Check and process all callbacks. */ public static function checkCallbacks() { - $res = Database::simpleQuery("SELECT taskid, cbfunction FROM callback"); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - $status = Taskmanager::status($row['taskid']); - if (Taskmanager::isFailed($status) || Taskmanager::isFinished($status)) - Database::exec("DELETE FROM callback WHERE taskid = :task AND cbfunction = :cb LIMIT 1", array('task' => $row['taskid'], 'cb' => $row['cbfunction'])); - if (Taskmanager::isFinished($status)) { - $func = array('TaskmanagerCallback', preg_replace('/\W/', '', $row['cbfunction'])); - if (!call_user_func_array('method_exists', $func)) { - Eventlog::warning("Callback {$row['cbfunction']} doesn't exist."); - } else { - call_user_func($func, $status); - } + $callbackList = TaskmanagerCallback::getPendingCallbacks(); + foreach ($callbackList as $taskid => $callbacks) { + $status = Taskmanager::status($taskid); + if ($status === false) + continue; + foreach ($callbacks as $callback) { + TaskmanagerCallback::handleCallback($callback, $status); } + if (Taskmanager::isFailed($status) || Taskmanager::isFinished($status)) + Taskmanager::release($status); } } -- cgit v1.2.3-55-g7522