/** * @param array $parameters * @return void|\zaboy\async\Promise\Interfaces\PromiseInterface * @throws CallbackException */ public function asyncCall(array $parameters = []) { if (is_null($this->script)) { if (!is_file(self::SCRIPT_NAME)) { throw new CallbackException('The handler script "scriptProxy.php" does not exist in the folder "script"'); } $this->script = self::SCRIPT_NAME; } $promise = $this->promiseBroker->make(); // Merge the options from config with passed options /** @var array|null $parameters */ $options = array_merge(['promise' => $promise->getId()], $parameters); // Files names for stdout and stderr $stdOutFilename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('stdout_', 1); $stdErrFilename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('stderr_', 1); /** @var CommandLineWorker $commandLineWorker */ $commandLineWorker = new CommandLineWorker(); $cmd = $this->commandPrefix . ' ' . $this->script; $cmd .= $commandLineWorker->makeParamsString(['scriptOptions' => $commandLineWorker->encodeParams($options)]); $cmd .= " 1>{$stdOutFilename} 2>{$stdErrFilename} & echo \$!"; $output = trim(shell_exec($cmd)); // if (!is_numeric($output)) { // throw new CallbackException("The output of the script is ambiguous." // . "Probably there is an error in the script"); // } // $pId = intval($output); // // $this->scriptBroker->setFileInfo($promise->getId(), $pId, $stdOutFilename, $stdErrFilename); return $promise; }
/** * {@inherit} * * {@inherit} */ public function call(array $options = []) { $cmd = $this->commandPrefix . ' ' . $this->script; $cmd .= $this->commandLineWorker->makeParamsString(['scriptOptions' => $this->commandLineWorker->encodeParams($options)]); // Files names for stdout and stderr $stdOutFilename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('stdout_', 1); $stdErrFilename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('stderr_', 1); $cmd .= " 1>{$stdOutFilename} 2>{$stdErrFilename}"; if (substr(php_uname(), 0, 7) == "Windows") { pclose(popen($cmd, "r")); } else { $cmd .= " & echo \$!"; exec($cmd, $output); } $errors = $this->parser->parseFile($stdErrFilename); $output = $this->parser->parseFile($stdOutFilename); if ($errors['fatalStatus']) { throw new CallbackException($errors['message']); } return $output['message']; }
<?php chdir(getcwd()); require './vendor/autoload.php'; use zaboy\scheduler\FileSystem\CommandLineWorker; use zaboy\scheduler\DataStore\UTCTime; $commandLineWorker = new CommandLineWorker(); $options = $commandLineWorker->getCallOptions($_SERVER['argv']); $serviceName = 'tick_log_datastore'; /** @var Zend\ServiceManager\ServiceManager $container */ $container = (include './config/container.php'); if (!$container->has($serviceName)) { throw new Exception("The service \"{$serviceName}\" must be specified in config/datastore"); } $log = $container->get('tick_log_datastore'); $itemData = ['tick_id' => UTCTime::getUTCTimestamp(), 'step' => print_r($options, 1)]; $log->create($itemData);