} return $value; }; // Increase memory_limit if it is lower than 512M if ($memoryInBytes($memoryLimit) < 512 * 1024 * 1024) { @ini_set('memory_limit', '512M'); } unset($memoryInBytes); } unset($memoryLimit); } try { require_once __DIR__ . '/../src/bootstrap.php'; define('TELEPORT_BASE_PATH', rtrim(getcwd(), '/') . '/'); $options = array('debug' => $opt('debug'), 'verbose' => $opt('verbose', true)); if (is_readable('config.php')) { $config = (include 'config.php'); if (is_array($config)) { $options = array_merge($config, $options); } } /** @var \Teleport\Beam\Endpoint $server */ $server = \Teleport\Beam\Endpoint::instance($options); $server->run($arg(1, TELEPORT_BASE_PATH . 'profile/revo_22.profile.json')); printf("teleport endpoint listener stopped with exit code 0 in %2.4f seconds" . PHP_EOL, microtime(true) - $start); exit(0); } catch (\Exception $e) { echo 'fatal: server error [' . get_class($e) . '] ' . $e->getMessage() . ' at ' . $e->getFile() . ':' . $e->getLine() . PHP_EOL; printf("teleport endpoint listener stopped with exit code {$e->getCode()} in %2.4f seconds" . PHP_EOL, microtime(true) - $start); exit($e->getCode()); }
/** * Bind the Teleport Endpoint listener to the specified MODX profile. * * @param string $profile A valid MODX profile describing the instance to * bind the Endpoint listener to. * @param array $options An array of options for the Endpoint. * * @throws \RuntimeException If an error occurs binding the listener to the * specified MODX instance. */ public function run($profile, array $options = array()) { try { $profile = self::loadProfile($profile); $modx = $this->getMODX($profile, array_merge(array('log_target' => 'ECHO'), $options)); /** @var \modRegistry $registry */ $registry = $modx->getService('registry', 'registry.modRegistry'); $registerName = $this->getConfig()->get('endpoint.registerName', $options, 'endpoint'); $registerClass = $this->getConfig()->get('endpoint.registerClass', $options, 'registry.modFileRegister'); $registerOptions = $this->getConfig()->get('endpoint.registerOptions', $options, array('directory' => $registerName)); $register = $registry->getRegister($registerName, $registerClass, $registerOptions); $register->connect(); } catch (InvalidProfileException $e) { throw new \RuntimeException("Could not start Endpoint listener: {$e->getMessage()}", $e->getCode(), $e); } catch (\Exception $e) { throw new \RuntimeException("Could not start Endpoint listener: {$e->getMessage()}", $e->getCode(), $e); } $pollInterval = (double) $this->getConfig()->get('endpoint.pollInterval', $options, 1); /** @var \React\EventLoop\LibEventLoop $loop */ $loop = \React\EventLoop\Factory::create(); $self =& $this; /* poll MODX registry for Teleportation requests and act on them */ $loop->addPeriodicTimer($pollInterval, function () use($self, $profile, $modx, $register) { $register->subscribe('/queue/'); $msgs = $register->read(array('msg_limit' => 1, 'poll_limit' => 1, 'remove_read' => true, 'include_keys' => true)); $register->unsubscribe('/queue/'); if (!empty($msgs)) { $taskMsg = reset($msgs); $taskId = key($msgs); if ($self->getConfig()->get('verbose') || $self->getConfig()->get('debug')) { echo "msg received: {$taskId} => " . print_r($taskMsg, true) . "\n"; } $register->subscribe("/tasks/{$taskId}/"); $task = Endpoint::createTaskFactory($self, $taskId, $taskMsg, $register); $promise = new LazyPromise($task); $promise->then(function ($value) use($self, $register, $taskId) { if ($self->getConfig()->get('verbose') || $self->getConfig()->get('debug')) { echo "{$taskId} resolved [value => " . print_r($value, true) . "]\n"; } $register->send("/tasks/{$taskId}/", array(array('completed' => $value))); }, function ($reason) use($self, $register, $taskId) { if ($self->getConfig()->get('verbose') || $self->getConfig()->get('debug')) { echo "{$taskId} rejected [reason => " . print_r($reason, true) . "]\n"; } $register->send("/tasks/{$taskId}/", array(array('failed' => $reason))); }, function ($update) use($self, $register, $taskId) { if ($self->getConfig()->get('verbose') || $self->getConfig()->get('debug')) { echo "{$taskId} progress [update => " . print_r($update, true) . "]\n"; } $register->send("/tasks/{$taskId}/", array(array('progress' => $update))); }); $register->unsubscribe("/tasks/{$taskId}/"); } }); $loop->run(); }