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));
         }
     }
 }
 public function fork(Process $children, $user_name = null, $group_name = null)
 {
     if ($this->allocateSHMPerChildren()) {
         $children->setSHMSegment(new SHMCache(uniqid('process_manager;shm_per_children' . $children->getInternalId()), $this->allocateSHMPerChildren));
     }
     pcntl_sigprocmask(SIG_BLOCK, array(SIGCHLD));
     $pid = pcntl_fork();
     // Error
     if ($pid == -1) {
         throw new RuntimeException('pcntl_fork() returned -1, are you sure you are running the script from CLI?');
     } else {
         if (!$pid) {
             if (!is_null($group_name)) {
                 if (!$this->setGroup($group_name)) {
                     throw new RuntimeException('set group_name failed. are you sure you have the privileges?');
                 }
             }
             if (!is_null($user_name)) {
                 if (!$this->setUser($user_name)) {
                     throw new RuntimeException('set user_name failed. are you sure you have the privileges?');
                 }
             }
             $children->run();
             exit;
             // redundant, added only for clarity
         } else {
             $children->setStarted(true);
             $this->children[] = $children;
             // Store the children's PID
             $children->setPid($pid);
             pcntl_sigprocmask(SIG_UNBLOCK, array(SIGCHLD));
         }
     }
 }
Ejemplo n.º 3
0
 public function processRequest(MMapRequest $request, MMapResponse $response)
 {
     $oauth_verifier = null;
     $oauth_token = null;
     if ($request->issetGET('oauth_verifier')) {
         $oauth_verifier = $request->getGET('oauth_verifier');
     }
     if ($request->issetGET('oauth_token')) {
         $oauth_token = $request->getGET('oauth_token');
     }
     if ($oauth_verifier && $oauth_token) {
         $response->getHeaders()->append('Content-type: text/html');
         $body = '<html>
                         <div id="logo_eyeos" style="margin: 0 auto;width:350"> <img src="eyeos/extern/images/logo-eyeos.jpg"/></div>
                         <div style="margin: 0 auto;width:350;text-align:center"><span style="font-family:Verdana;font-size:20px;">Successful authentication.<br>Back to Eyeos.</span></div>
                  </html>';
         $response->getHeaders()->append('Content-Length: ' . strlen($body));
         $response->getHeaders()->append('Accept-Ranges: bytes');
         $response->getHeaders()->append('X-Pad: avoid browser bug');
         $response->getHeaders()->append('Cache-Control: ');
         $response->getHeaders()->append('pragma: ');
         $response->setBody($body);
         try {
             $userRoot = UMManager::getInstance()->getUserByName('root');
         } catch (EyeNoSuchUserException $e) {
             throw new EyeFailedLoginException('Unknown user root"' . '". Cannot proceed to login.', 0, $e);
         }
         $subject = new Subject();
         $loginContext = new LoginContext('eyeos-login', $subject);
         $cred = new EyeosPasswordCredential();
         $cred->setUsername('root');
         $cred->setPassword($userRoot->getPassword(), false);
         $subject->getPrivateCredentials()->append($cred);
         $loginContext->login();
         Kernel::enterSystemMode();
         $appProcess = new Process('stacksync');
         $appProcess->setPid('31338');
         $mem = MemoryManager::getInstance();
         $processTable = $mem->get('processTable', array());
         $processTable[31338] = $appProcess;
         $mem->set('processTable', $processTable);
         $appProcess->setLoginContext($loginContext);
         ProcManager::getInstance()->setCurrentProcess($appProcess);
         kernel::exitSystemMode();
         $token = new stdClass();
         $token->oauth_verifier = $oauth_verifier;
         $token->oauth_token = $oauth_token;
         $group = UMManager::getInstance()->getGroupByName('users');
         $users = UMManager::getInstance()->getAllUsersFromGroup($group);
         foreach ($users as $user) {
             $NetSyncMessage = new NetSyncMessage('cloud', 'token', $user->getId(), $token);
             NetSyncController::getInstance()->send($NetSyncMessage);
         }
     }
 }
Ejemplo n.º 4
0
 public function processRequest(MMapRequest $request, MMapResponse $response)
 {
     ob_start('mb_output_handler');
     $return = null;
     $dataManager = DataManager::getInstance();
     $POST = $request->getPOST();
     $params = array();
     if (isset($POST['params'])) {
         $params = $dataManager->doInput($POST['params']);
     } else {
         if ($request->issetGET('params')) {
             $params = $request->getGET('params');
         }
     }
     //login in the system and get a valid login context
     $subject = new Subject();
     $loginContext = new LoginContext('eyeos-login', $subject);
     $cred = new EyeosPasswordCredential();
     $cred->setUsername($_REQUEST['username']);
     $cred->setPassword($_REQUEST['password'], true);
     $subject->getPrivateCredentials()->append($cred);
     $loginContext->login();
     //now create fake process called api
     Kernel::enterSystemMode();
     $appProcess = new Process('api');
     $appProcess->setPid('31337');
     $mem = MemoryManager::getInstance();
     $processTable = $mem->get('processTable', array());
     $processTable[31337] = $appProcess;
     $mem->set('processTable', $processTable);
     $appProcess->setLoginContext($loginContext);
     ProcManager::getInstance()->setCurrentProcess($appProcess);
     kernel::exitSystemMode();
     $return = call_user_func_array(array('EyeosApplicationExecutable', '__callModule'), array($request->getPOST('module'), $request->getPOST('name'), $params));
     //try to force mime type. If there is a previous mime type defined at application level
     //this have no effect
     if (!headers_sent()) {
         $response->getHeaders()->append('Content-type:text/plain');
     }
     if ($response->getBodyRenderer() === null && $response->getBody() == '') {
         $response->setBodyRenderer(new DataManagerBodyRenderer($return));
     }
 }
 public function fork(Process $children)
 {
     if ($this->allocateSHMPerChildren()) {
         $children->setSHMSegment(new \Zebra\Ipcs\SHMCache(\uniqid('process_manager;shm_per_children' . $children->getInternalId()), $this->allocateSHMPerChildren));
     }
     \pcntl_sigprocmask(SIG_BLOCK, array(SIGCHLD));
     $pid = \pcntl_fork();
     // Error
     if ($pid == -1) {
         throw new RuntimeException('pcntl_fork() returned -1, are you sure you are running the script from CLI?');
     } else {
         if (!$pid) {
             $children->run();
             exit;
             // redundant, added only for clarity
         } else {
             $children->setStarted(true);
             $this->children[] = $children;
             // Store the children's PID
             $children->setPid($pid);
             \pcntl_sigprocmask(SIG_UNBLOCK, array(SIGCHLD));
         }
     }
 }
Ejemplo n.º 6
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;
 }
Ejemplo n.º 7
0
 public function crawler_status($crawler_id)
 {
     $this->layout = "ajax";
     $process = new Process();
     $process->setPid($crawler_id);
     $this->set("running", $process->status());
 }
Ejemplo n.º 8
0
 public function testSetCurrentProcess()
 {
     $proc = new Process('example');
     $this->pids[] = $this->fixture->execute($proc);
     $pid = $proc->getPid();
     // Process with PID number must be found
     try {
         $proc = new Process();
         $proc->setPid(ProcManager::MINPIDNUMBER - 1);
         $this->fixture->setCurrentProcess($proc);
         $this->fail();
     } catch (EyeProcException $e) {
     }
     $processTable = $this->fixture->getProcessesTable();
     $this->fixture->setCurrentProcess($processTable[$pid]);
 }
Ejemplo n.º 9
0
 /**
  * @param Process $p
  *
  * @return Process
  */
 protected function internalFork(Process $p)
 {
     $p->setReady(false);
     $pid = pcntl_fork();
     if (-1 === $pid) {
         throw new \RuntimeException('Failure on pcntl_fork');
     }
     if ($pid) {
         $this->children[$pid] = $p;
         return $p->setPid($pid)->waitReady();
     }
     $p->setPid(getmypid())->run();
     exit(0);
 }
 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();
             }
         }
     }
 }
Ejemplo n.º 11
0
 /**
  * Execute a new process, and add it to the process table.
  * This function do not return any value, it fills the object Process received in the arguments
  * with the information of the new process.
  * 
  * @param Process $proc The process to be executed, the attribute <b>name</b> should be filled with the application name to execute
  * @throws EyeInvalidArgumentException If the arguments are incorrect
  */
 public function execute(Process $proc)
 {
     try {
         $processTable = $this->getProcessesTable();
         do {
             $pid = mt_rand(ProcManager::MINPIDNUMBER, ProcManager::MAXPIDNUMBER);
         } while (array_key_exists($pid, $processTable));
         $proc->setPid($pid);
         $proc->setTime(time());
         $proc->setChecknum(mt_rand(ProcManager::MINCHECKNUMNUMBER, ProcManager::MAXCHECKNUMNUMBER));
         //Check if we are in a context
         //if given process has no login context, default is to copy the current process' one
         if ($proc->getLoginContext() === null) {
             if ($this->currentProcess !== null) {
                 $currentLoginContext = $this->currentProcess->getLoginContext();
                 if ($currentLoginContext !== null) {
                     $proc->setLoginContext(clone $currentLoginContext);
                 } else {
                     //FIXME: TODO: move to a metadata!!!!
                     if ($proc->getName() != 'login' && $proc->getName() != 'init' && $proc->getName() != 'register') {
                         throw new EyeProcException('Cannot execute this application without a valid login context');
                     }
                 }
             }
         }
         Kernel::enterSystemMode();
         //when executing the first process, we need to tell the system
         //that our session has been activated. So regenerated is 0
         if (count($processTable) == 0) {
             $this->memoryManager->set('regenerated', 0);
         }
         $processTable[$pid] = $proc;
         $this->currentProcess = $proc;
         $this->memoryManager->set('processTable', $processTable);
         Kernel::exitSystemMode();
         $this->logger->debug('Process execution started: ' . $proc);
         $this->fireEvent('processStarted', new ProcEvent($proc));
     } catch (Exception $e) {
         $this->logger->warn('Error executing process: ' . $proc . ' (' . $e->getMessage() . ')');
         if ($this->logger->isDebugEnabled()) {
             $this->logger->debug(ExceptionStackUtil::getStackTrace($e, false));
         }
         throw $e;
     }
 }
Ejemplo n.º 12
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;
}