Example #1
0
//Service name is expected
$service = $opt['name'];
//name of the class in camel case
$cls = Scalr::camelize($service);
$logger = Scalr::getContainer()->logger('cron/client.php')->setLevel(Scalr::config('scalr.crontab.log_level'));
//Checking if task class exists.
if (!file_exists(SRCPATH . '/Scalr/System/Zmq/Cron/Task/' . $cls . '.php')) {
    $logger->fatal("Launch error. File %s does not exist.", SRCPATH . '/Scalr/System/Zmq/Cron/Task/' . $cls . '.php');
    exit;
}
$taskClass = 'Scalr\\System\\Zmq\\Cron\\Task\\' . $cls;
/* @var $task \Scalr\System\Zmq\Cron\AbstractTask */
$task = new $taskClass();
$taskConfig = $task->config();
$oPid = new PidFile(CACHEPATH . '/cron.client.' . $task->getName() . '.pid', '/client.php');
$oPid->setLogger($logger);
$oPid->create();
$interrupt = 0;
//Signal handler callback function
$sigHandler = function ($signo = null) use(&$interrupt, $oPid, $task, $taskConfig) {
    static $once = 0;
    $interrupt++;
    if ($once++) {
        return;
    }
    $task->log($taskConfig->daemon ? 'SERVICE' : 'DEBUG', "Client recieved termination SIGNAL:%d", intval($signo));
    //Terminating child processes (workers)
    $task->shutdown();
    //Removing pid file
    $oPid->remove();
    //No use to proceed
Example #2
0
 * It's also responsible for ZMQ MDP Broker availability.
 *
 * @author Vitaliy Demidov  <*****@*****.**>
 * @since  5.0.1 (10.09.2013)
 */
declare (ticks=1);
define('SCALR_MULTITHREADING', true);
require_once __DIR__ . "/../src/prepend.inc.php";
use Scalr\System\Zmq\Cron\Launcher;
use Scalr\System\Zmq\Cron\PidFile;
set_time_limit(0);
$logger = Scalr::getContainer()->logger('cron/service.php')->setLevel(Scalr::config('scalr.crontab.log_level'));
$oPid = new PidFile(CACHEPATH . '/cron.service.pid', '/service.php');
//If we start service each minute we should eliminate exrtra messages like "Another process already running..."
$oPid->pidExistLevel = 'WARN';
$oPid->setLogger($logger)->create();
$interrupt = 0;
//Signal handler callback function
$sigHandler = function ($signo = null) use(&$interrupt, $oPid, $logger) {
    static $once = 0;
    $interrupt++;
    if ($once++) {
        return;
    }
    //Reporting about termination
    $logger->log("SERVICE", "Service recieved termination SIGNAL:%d", intval($signo));
    //We should stop all running tasks.
    //It's enough to terminate all running clients.
    Launcher::terminateClients();
    //Without this workers may not be terminated
    sleep(1);