services.inc.php 3.48 KB
Newer Older
tbrehm's avatar
tbrehm committed
1
<?php
redray's avatar
redray committed
2

tbrehm's avatar
tbrehm committed
3
/*
redray's avatar
redray committed
4
Copyright (c) 2007, Till Brehm, projektfarm Gmbh
tbrehm's avatar
tbrehm committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of ISPConfig nor the names of its contributors
      may be used to endorse or promote products derived from this software without
      specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

class services {

	var $registered_services = array();
	var $delayed_restarts = array();
35
	var $debug = false;
36 37

	// This function adds a request for restarting
tbrehm's avatar
tbrehm committed
38
	// a service at the end of the configuration run.
39
	function restartServiceDelayed($service_name, $action = 'restart') {
tbrehm's avatar
tbrehm committed
40 41 42 43
		global $app;
		if(is_array($this->registered_services[$service_name])) {
			$this->delayed_restarts[$service_name] = $action;
		} else {
44
			$app->log("Unable to add a delayed restart for '$service_name'. Service not registered.", LOGLEVEL_WARNING);
tbrehm's avatar
tbrehm committed
45
		}
46

tbrehm's avatar
tbrehm committed
47
	}
48

tbrehm's avatar
tbrehm committed
49
	// This function restarts a service when the function is called
50
	function restartService($service_name, $action = 'restart') {
tbrehm's avatar
tbrehm committed
51
		global $app;
52

tbrehm's avatar
tbrehm committed
53
		if(is_array($this->registered_services[$service_name])) {
54 55
			$module_name = $this->registered_services[$service_name]['module'];
			$function_name = $this->registered_services[$service_name]['function'];
56
			$app->log("Calling function '$function_name' from module '$module_name'.", LOGLEVEL_DEBUG);
57
			// call_user_method($function_name,$app->loaded_modules[$module_name],$action);
58
			return call_user_func(array($app->loaded_modules[$module_name], $function_name), $action);
tbrehm's avatar
tbrehm committed
59
		} else {
60
			$app->log("Unable to restart $service_name. Service not registered.", LOGLEVEL_WARNING);
61
			return array('output' => '', 'retval' => 0);
tbrehm's avatar
tbrehm committed
62
		}
63

tbrehm's avatar
tbrehm committed
64
	}
65

tbrehm's avatar
tbrehm committed
66
	// This function is used to register callback functions for services that can be restarted
67
	function registerService($service_name, $module_name, $function_name) {
tbrehm's avatar
tbrehm committed
68 69
		global $app;
		$this->registered_services[$service_name] = array('module' => $module_name, 'function' => $function_name);
70
		if($this->debug) $app->log("Registered Service '$service_name' in module '$module_name' for processing function '$function_name'", LOGLEVEL_DEBUG);
tbrehm's avatar
tbrehm committed
71
	}
72

tbrehm's avatar
tbrehm committed
73 74 75 76
	// This function is called at the end of the server script to restart services.
	function processDelayedActions() {
		global $app;
		foreach($this->delayed_restarts as $service_name => $action) {
77
			$this->restartService($service_name, $action);
tbrehm's avatar
tbrehm committed
78 79 80 81
		}
	}

}
82

83
?>