Exakat\Tasks\Jobqueue::run PHP Метод

run() публичный Метод

public run ( )
    public function run()
    {
        $this->jobQueueLog = fopen($this->config->projects_root . '/projects/log/jobqueue.log', 'a');
        $this->log('Open Job Queue ' . date('r') . "\n");
        $this->log->log('Started jobQueue : ' . time() . "\n");
        $queue = array();
        // @todo add this to doctor
        if (!file_exists($this->config->projects_root . '/out')) {
            mkdir($this->config->projects_root . '/out', 0755);
        }
        if (!file_exists($this->config->projects_root . '/in')) {
            mkdir($this->config->projects_root . '/in', 0755);
        }
        if (!file_exists($this->config->projects_root . '/progress')) {
            mkdir($this->config->projects_root . '/progress', 0755);
        }
        //////// setup our named pipe ////////
        // @todo put this in config
        if (file_exists($this->pipefile)) {
            if (!unlink($this->pipefile)) {
                die('unable to remove existing PipeFile "' . $this->pipefile . '". Aborting.' . "\n");
            }
        }
        umask(0);
        if (!posix_mkfifo($this->pipefile, 0666)) {
            die('unable to create named pipe');
        }
        $pipe = fopen($this->pipefile, 'r+');
        if (!$pipe) {
            die('unable to open the named pipe');
        }
        stream_set_blocking($pipe, false);
        //////// process the queue ////////
        while (1) {
            while ($input = trim(fgets($pipe))) {
                stream_set_blocking($pipe, false);
                $queue[] = $input;
            }
            $job = current($queue);
            $jobkey = key($queue);
            if ($job) {
                switch ($job) {
                    case 'quit':
                        display("Received quit command. Bye\n");
                        $this->log('Quit command');
                        $this->log->log('Quit jobQueue : ' . time() . "\n");
                        die;
                    case 'ping':
                        print 'pong' . PHP_EOL;
                        break;
                    case file_exists($this->config->projects_root . '/projects/' . $job):
                        display('processing project job ' . $job . PHP_EOL);
                        $this->log('Start job : ' . $job);
                        $b = microtime(true);
                        shell_exec($this->config->php . ' ' . $this->config->executable . ' project -p ' . $job);
                        $e = microtime(true);
                        $this->log('End job : ' . $job . '(' . number_format($e - $b, 2) . ' s)');
                        display('processing project job ' . $job . ' done (' . number_format($e - $b, 2) . ' s)' . PHP_EOL);
                        break;
                    case file_exists($this->config->projects_root . '/in/' . $job . '.php'):
                        display('processing onepage job ' . $job . PHP_EOL);
                        $this->process($job);
                    default:
                }
                next($queue);
                unset($job, $queue[$jobkey]);
            } else {
                display('no jobs to do - waiting...' . PHP_EOL);
                stream_set_blocking($pipe, true);
            }
        }
    }

Usage Example

Пример #1
0
 public function execute(Config $config)
 {
     switch ($config->command) {
         case 'doctor':
             $doctor = new Tasks\Doctor($this->gremlin, $this->config);
             $doctor->run($config);
             break;
         case 'init':
             $task = new Tasks\Initproject($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'anonymize':
             $task = new Tasks\Anonymize($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'files':
             $task = new Tasks\Files($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'load':
             $task = new Tasks\Load($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'stat':
             $task = new Tasks\Stat($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'analyze':
             $task = new Tasks\Analyze($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'results':
             $task = new Tasks\Results($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'export':
             $task = new Tasks\Export($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'report':
             $task = new Tasks\Report2($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'project':
             $task = new Tasks\Project($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'magicnumber':
             $task = new Tasks\Magicnumber($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'clean':
             $task = new Tasks\Clean($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'status':
             $task = new Tasks\Status($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'help':
             $task = new Tasks\Help($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'cleandb':
             $task = new Tasks\CleanDb($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'onepage':
             $task = new Tasks\OnePage($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'update':
             $task = new Tasks\Update($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'onepagereport':
             $task = new Tasks\OnepageReport($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'phploc':
             $task = new Tasks\Phploc($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'findextlib':
             $task = new Tasks\FindExternalLibraries($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'dump':
             $task = new Tasks\Dump($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'jobqueue':
             $task = new Tasks\Jobqueue($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'queue':
             $task = new Tasks\Queue($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'test':
             $task = new Tasks\Test($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'remove':
             $task = new Tasks\Remove($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'server':
             $task = new Tasks\Server($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'upgrade':
             $task = new Tasks\Upgrade($this->gremlin, $this->config);
             $task->run($config);
             break;
         case 'version':
         default:
             $version = self::VERSION;
             $build = self::BUILD;
             $date = date('r', filemtime(__FILE__));
             echo "\n ________                 __              _    \n|_   __  |               [  |  _         / |_  \n  | |_ \\_| _   __  ,--.   | | / ]  ,--. `| |-' \n  |  _| _ [ \\ [  ]`'_\\ :  | '' <  `'_\\ : | |   \n _| |__/ | > '  < // | |, | |`\\ \\ // | |,| |,  \n|________|[__]`\\_]\\'-;__/[__|  \\_]\\'-;__/\\__/  \n                                               \n\nExakat : @ 2014-2016 Damien Seguy. \nVersion : ", $version, ' - Build ', $build, ' - ', $date, "\n";
             break;
     }
 }