Пример #1
0
 public function __destruct()
 {
     if (!is_null($this->log)) {
         if (empty($_SERVER['QUERY_STRING'])) {
             $this->log->debug("{$_SERVER['REQUEST_METHOD']} {$_SERVER['REQUEST_URI']} {$_SERVER['SERVER_PROTOCOL']}");
         } else {
             $this->log->debug("{$_SERVER['REQUEST_METHOD']} {$_SERVER['REQUEST_URI']}?{$_SERVER['QUERY_STRING']} {$_SERVER['SERVER_PROTOCOL']}");
         }
         $this->log->debug('TIME: ' . Formater::msec(microtime(true) - $this->time));
         $this->log->debug('RAM: ' . Formater::size(memory_get_peak_usage(true)));
     }
 }
Пример #2
0
 public function dispatch($argc, $argv)
 {
     if (!$this->boot) {
         $this->boot();
     }
     if ($argc < 2) {
         throw new Exception\Worker('Missing worker name');
     }
     if (!preg_match('/[a-zA-Z0-9-_]+/', $argv[1])) {
         throw new Exception\Worker("Invalid worker name '{$argv[1]}'");
     }
     if ($argc == 2) {
         $argc++;
         $argv[2] = 'start';
     }
     $class = 'Worker\\' . ucfirst($argv[1]) . 'Worker';
     if (!class_exists($class)) {
         throw new Exception\Worker("Worker not found ({$class})");
     }
     switch ($argv[2]) {
         case 'start':
             if ($class::CONCURRENT === false && !$this->lock($argv[1])) {
                 echo 'worker cannot run in concurrent mode', PHP_EOL;
                 break;
             }
             echo 'worker started (', date('d/m/Y H:i:s'), ')', PHP_EOL;
             echo 'worker name: ', $argv[1], PHP_EOL;
             $pid = getmypid();
             echo 'worker pid: ', $pid, PHP_EOL;
             $pidf = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "{$argv[1]}.pid";
             @file_put_contents($pidf, $pid);
             $worker = new $class();
             if (function_exists('cli_set_process_title')) {
                 cli_set_process_title($worker->name());
                 echo 'process name: ', $worker->name(), PHP_EOL;
             }
             $time = microtime(true);
             $worker->run(array_slice($argv, 3));
             $time = microtime(true) - $time;
             echo 'worker finished (', Formater::msec($time), ')', PHP_EOL;
             @unlink($pidf);
             $this->unlock();
             break;
         case 'stop':
             $pidf = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "{$argv[1]}.pid";
             if (is_file($pidf)) {
                 if ($class::CONCURRENT === false && !$this->lock($argv[1])) {
                     echo 'worker not running', PHP_EOL;
                     break;
                 }
                 echo 'worker stop', PHP_EOL;
                 $pid = @file_get_contents($pidf);
                 echo 'worker pid: ', $pid, PHP_EOL;
                 exec("kill -9 {$pid}");
             } else {
                 echo 'worker not running', PHP_EOL;
             }
             break;
         default:
             throw new Exception\Worker("Invalid operation '{$argv[2]}'");
     }
 }