function check_job_servers()
 {
     //loop schemas
     foreach (Doo::db()->find('Schemata') as $s) {
         $schema_id = $s->id;
         //enabled config with PID
         foreach (Doo::db()->find('GearmanJobServers', array('where' => 'local = 1 AND enabled = 1 AND schema_id = ' . $schema_id)) as $js) {
             if (!empty($js->pid)) {
                 //init
                 $p = new Process();
                 $p->setPid($js->pid);
                 //check status
                 if ($p->status()) {
                     continue;
                 }
             }
             //let start
             $js->pid = $this->start_local_job_server($js->id, $js->port, $s->id);
             $js->update();
         }
         //disabled config
         foreach (Doo::db()->find('GearmanJobServers', array('where' => 'local = 1 AND enabled = 0 AND pid IS NOT NULL AND schema_id = ' . $schema_id)) as $js) {
             //stop
             $p = new Process();
             $p->setPid($js->pid);
             $p->stop();
             $js->pid = null;
             $js->update(array('setnulls' => true));
         }
     }
 }
Example #2
0
 public static function ensureRunning($cmd, $pidfile, $restart = false)
 {
     $pidfile = '/var/run/' . $pidfile;
     // get pid from pidfile, protecting against invalid data
     $p1 = null;
     if (file_exists($pidfile) && is_readable($pidfile)) {
         $pid = substr(file_get_contents($pidfile), 0, 64);
         $p1 = new Process();
         $p1->setPid($pid);
     }
     if ($restart && $p1) {
         $p1->stop();
     }
     // check whether process with this pid is running
     if (!$p1 || !$p1->status()) {
         // check that we can write the pidfile, to avoid spawning endless processes
         if (file_put_contents($pidfile, 'test')) {
             $p2 = new Process($cmd);
             file_put_contents($pidfile, $p2->getPid());
         }
         return true;
     }
     return false;
 }
 public function crawler_status($crawler_id)
 {
     $this->layout = "ajax";
     $process = new Process();
     $process->setPid($crawler_id);
     $this->set("running", $process->status());
 }
 function check_workers()
 {
     //loop schemas
     foreach (Doo::db()->find('Schemata') as $s) {
         $schema_id = $s->id;
         $funcs = Doo::db()->fetchAll("SELECT a.id, a.schema_id, b.function_name, a.worker_count, a.enabled\r\n\t\t\t\tFROM gearman_functions a\r\n\t\t\t\tLEFT JOIN gearman_function_names b\r\n\t\t\t\tON a.function_name_id = b.id\r\n\t\t\t\tWHERE enabled = 1\r\n\t\t\t\tAND schema_id = {$schema_id}");
         //read worker config
         $worker_functions = array();
         foreach ($funcs as $f) {
             $worker_functions[$f["function_name"]] = $f["worker_count"];
         }
         //Loop enabled config
         foreach ($worker_functions as $function => $worker_count) {
             //find current workers for the function
             $runningWorkers = Doo::db()->find('GearmanWorkers', array('where' => "function_name = '{$function}' AND schema_id = {$schema_id}"));
             //scan workers to make sure they are still running
             foreach ($runningWorkers as $worker) {
                 //init
                 $p = new Process();
                 $p->setPid($worker->pid);
                 //check status
                 if (!$p->status()) {
                     //crashed! Lets re init
                     $gw = new GearmanWorkers();
                     $gw->pid = $this->start_worker($function, $schema_id);
                     $gw->function_name = $function;
                     $gw->schema_id = $schema_id;
                     $gw->insert();
                     //remove crashed pid
                     $worker->delete();
                 }
             }
             //calc delta workers
             $delta = $worker_count - count($runningWorkers);
             //add missing workers
             if ($delta > 0) {
                 for ($i = 0; $i < $delta; $i++) {
                     //run process
                     $sw = new GearmanWorkers();
                     $sw->pid = $this->start_worker($function, $schema_id);
                     $sw->function_name = $function;
                     $sw->schema_id = $schema_id;
                     $sw->insert();
                 }
             }
             //remove extra workers
             if ($delta < 0) {
                 //find current workers for the function
                 $runningWorkers = Doo::db()->find('GearmanWorkers', array('where' => "function_name = '{$function}' AND schema_id = {$schema_id}"));
                 for ($i = 0; $i < abs($delta); $i++) {
                     //kill process
                     $worker = $runningWorkers[$i];
                     $p = new Process();
                     $p->setPid($worker->pid);
                     $p->stop();
                     $worker->delete();
                 }
             }
         }
         //disabled config
         $funcs = Doo::db()->fetchAll("SELECT a.id, a.schema_id, b.function_name, a.worker_count, a.enabled\r\n\t\t\t\tFROM gearman_functions a\r\n\t\t\t\tLEFT JOIN gearman_function_names b\r\n\t\t\t\tON a.function_name_id = b.id\r\n\t\t\t\tWHERE enabled = 0\r\n\t\t\t\tAND schema_id = {$schema_id}");
         foreach ($funcs as $f) {
             $function = $f["function_name"];
             //find current workers for the function
             $disabledWorkers = Doo::db()->find('GearmanWorkers', array('where' => "function_name = '{$function}' AND schema_id = {$schema_id}"));
             //scan workers and kill to disable
             foreach ($disabledWorkers as $worker) {
                 //init
                 $p = new Process();
                 $p->setPid($worker->pid);
                 $p->stop();
                 $worker->delete();
             }
         }
     }
 }
Example #5
0
function stop()
{
    //stop function
    global $conf;
    global $db;
    $result = $db->query("SELECT count(*) AS C FROM pid");
    $row = $result->fetchArray();
    // output data of each row
    $result = $db->query("SELECT * FROM pid LIMIT 1");
    $row = $result->fetchArray();
    $process = new Process();
    $process->setPid($row["pid"]);
    if (!$process->status()) {
        $sql = "DELETE FROM pid";
        $result = $db->query($sql);
        $return_array["info"] = "process already dead";
    } else {
        $response_array["pid"] = $process->getPid();
        $process->stop();
        $sql = "DELETE FROM pid";
        $result = $db->query($sql);
        $return_array["info"] = "process stopped";
    }
    sleep(1);
    cleanpl();
    $return_array["status"] = 0;
    return $return_array;
}