public function enable() { if (Globals::RUNTIME_MODE_CLI !== App::environment(Globals::ENV_RUNTIME_MODE)) { return false; } return true; }
public function handle() { if (Globals::RUNTIME_MODE_CLI !== App::environment(Globals::ENV_RUNTIME_MODE)) { throw new ConsoleException("Only run in command line mode"); } //加载Worker并为每个Worker创建一个子进程,然后进入休眠,当接收到信号量时,则执行相应的进程调度操作。 if (!function_exists("pcntl_signal")) { throw new ConsoleException("PHP does not appear to be compiled with the PCNTL extension.This is neccesary for daemonization"); } if (function_exists("gc_enable")) { gc_enable(); } $daemon = Daemon::make($_SERVER['argc'], $_SERVER['argv']); try { /** * @var $daemon Daemon */ if (isset($daemon)) { $daemon->start(); sleep(1); Console::line("Daemon [" . $daemon->getPID() . "] started."); } } catch (\Exception $ex) { throw new DaemonException($daemon->getName(), $ex->getMessage(), $ex); } }
public static function load($key) { $path = App::path($key); if (isset($path) && is_readable($path)) { return require_once $path; } return null; }
/** * @param string $database * @return DB */ private static function getInstance($database) { if (!isset(self::$instances) || !array_key_exists($database, self::$instances) || self::$currentDatabase !== $database) { self::$instances[$database] = self::create(App::environment(Globals::ENV_DATABASE_PROVIDER), $database); self::$currentDatabase = $database; } return self::$instances[$database]; }
/** * @param $key * @return string */ private function getPath($key) { $fileName = md5($key); $folder = App::path('cache') . $this->cacheFolder; $path = $folder . '/' . $fileName . '.dat'; Directory::makeDir($folder); return $path; }
private static function getFilePath($viewName) { $filePath = str_replace('.', '/', $viewName); $extension = App::environment(Globals::ENV_MVC_VIEW_FILE_EXTENSION); if (!isset($extension) || $extension === '') { $extension = self::ENV_VIEW_FILE_EXT; } return App::path('app') . self::VIEW_BASE_PATH . $filePath . '.' . $extension; }
private function clearTableSchema($name) { if (preg_match('#m[\\d]+(Create|Alter)(?<table>[a-zA-Z][a-zA-Z0-9\\_]+)Table#i', $name, $matches)) { $table = strtolower(Strings::camelToSnake($matches['table'])); $path = App::path('cache') . "/schema/" . DB::getDatabaseName() . "/{$table}.dat"; if (file_exists($path)) { unlink($path); } } }
public function appStart() { App::path('migration', App::path('app') . '/database/migration'); App::path('migration_history', App::path('migration') . '/history.dat'); App::path('cache', App::path('app') . '/cache'); App::path('exception', App::path('config') . '/exception.php'); App::path('runtime', App::path('app') . '/runtime'); App::path('daemons', App::path('config') . '/daemons.php'); App::path('commands', App::path('config') . '/commands.php'); App::path('log', App::path('app') . '/log'); }
private function getModelPath() { $path = App::path('models'); if (!isset($path)) { $path = App::path('app'); if (isset($path)) { $path = "{$path}/models"; } else { throw new ConsoleException("Can't find app path."); } } return $path; }
public function __construct($table) { if (!isset($table)) { throw new DataTableException($table, "Table name invalid - [{$table}]"); } $this->tableSymbol = TableSymbol::create($table); $this->table = $this->tableSymbol->getName(); $schema = App::path('schema'); if (!isset($schema)) { throw new DatabaseException("Can't find schema cache directory."); } $this->source = $schema . '/' . DB::getDatabaseName() . '/' . $this->table . '.dat'; $this->initialize(); }
private function showAllDaemon() { $messageQueueKey = ftok(App::path('cache') . "/queue/daemon.queue", "a"); $messageQueue = msg_get_queue($messageQueueKey, 0666); $messageQueueState = msg_stat_queue($messageQueue); $msgCount = $messageQueueState['msg_qnum']; if (0 === $msgCount) { Console::line("None service is running."); } while ($msgCount > 0) { /** @var MessageQueue $message */ msg_receive($messageQueue, 0, $messageType, 1024, $message, true, MSG_IPC_NOWAIT); Console::line("PID:{$message->pid},NAME:{$message->name},TIME:" . date("Y-m-d H:i:s", $message->timestamp) . "Alive."); $messageQueueState = msg_stat_queue($messageQueue); $msgCount = $messageQueueState['msg_qnum']; } }
public function execute() { if (isset($this->data) && !empty($this->data)) { $json = $this->data; if (isset($this->object)) { if (is_object($this->object)) { $json[strtolower(get_class($this->object))] = $this->object; } else { $json[] = $this->object; } } } else { $json = $this->object; } echo json_encode($json); App::killModule('UbenchModule'); }
public function appStart() { $this->classLoader = new ClassLoader(); $paths = Settings::load('autoload'); if (!isset($paths)) { $paths = array(); } $paths[] = '/service'; $paths[] = '/job'; $paths[] = '/command'; $paths[] = '/daemon'; $paths[] = '/module'; $paths[] = '/models'; $paths[] = '/test'; $paths[] = '/database/migration'; foreach ($paths as $item) { $this->classLoader->addPath(App::path('app') . $item); } }
public function execute() { $json = array(); $json['status'] = $this->statusCode; $json['message'] = $this->message; if (isset($this->data) && !empty($this->data)) { $json['data'] = $this->data; } if (isset($this->params)) { $json['data'] = array(); if (is_array($this->params)) { foreach ($this->params as $key => $value) { $json['data'][$key] = $value; } } else { $json['data'] = $this->params; } } echo json_encode($json); App::killModule('UbenchModule'); }
public function handle() { $this->router = Router::getInstance(App::path('routes'), App::path('filter')); $defaultRouterToken = App::environment(Globals::ROUTER_DEFAULT_TOKENS); if (isset($defaultRouterToken) && !empty($defaultRouterToken)) { $this->router->baseMappings['default']['default'] = $defaultRouterToken; } else { $defaultRouterToken = $this->router->baseMappings['default']['default']; } $this->router->registerMatchedHandler(function ($params) { DI::bindSingleton(Request::class, new Request($params)); DI::bindSingleton(Response::class, new Response()); }); $this->router->registerDefaultMatchedHandler(function ($params) use($defaultRouterToken) { $namespace = null; if (array_key_exists('namespace', $params)) { $namespace = $params['namespace']; } if (array_key_exists('controller', $params)) { $controller = $params['controller']; } if (array_key_exists('action', $params)) { $action = $params['action']; } if (!isset($controller)) { $controller = $defaultRouterToken['controller']; } if (!isset($action)) { $action = $defaultRouterToken['action']; } Controller::invoke($controller, $action, $namespace); }); $this->router->missing(function () { throw new URLRouterException("URL Routing missing."); }); $this->router->routing(); }
public static function fork($target) { if (!is_callable($target) && !$target instanceof Runnable) { throw new ConsoleException("Must be callable or Runnable."); } $pid = pcntl_fork(); if (0 === $pid) { try { if ($target instanceof Runnable) { $result = $target->run(); } else { $result = call_user_func($target); } } catch (\Exception $ex) { throw new ConsoleException($ex->getMessage(), $ex); } App::end($result); } if (0 > $pid) { throw new ConsoleException("Process start failure."); } $process = new Process($pid); return $process; }
private function forgetMigration() { if (!isset($this->args[1])) { throw new ConsoleException("Invalid migration argument in [--forget]."); } $selectedIndex = intval($this->args[1]); $history = Settings::get(App::path('migration_history')); if (isset($history)) { $history = unserialize($history); if (!empty($history)) { $this->history = $history; $date = "Unknown datetime"; foreach ($this->history as $key => $value) { if ($key === $selectedIndex) { unset($this->history[$selectedIndex]); if (preg_match('#^m(?<timestamp>\\d+)#i', $value, $match)) { if (array_key_exists('timestamp', $match)) { $timestamp = intval($match['timestamp']); $date = date("Y-m-d H:i:s", $timestamp); } } Settings::save(App::path('migration_history'), serialize($this->history)); Console::line("{$key}.[{$date}][{$value}] has forget."); return; } } Console::line("Can't find index [{$selectedIndex}]."); return; } } Console::line("History is empty."); }
<?php /** * Created by PhpStorm. * User: lbob * Date: 2015/12/8 * Time: 14:25 */ use Xaircraft\Core\IO\File; use Xaircraft\Exception\DaemonException; use Xaircraft\Exception\HttpAuthenticationException; return array(HttpAuthenticationException::class => function ($ex) { var_dump("aa"); }, DaemonException::class => function ($ex) { $path = \Xaircraft\App::path('log') . "/daemon/" . date("Ymd", time()) . '.log'; File::appendText($path, $ex->getMessage); });
<?php use Xaircraft\App; /** * Created by PhpStorm. * User: lbob * Date: 2015/11/11 * Time: 18:29 */ $app = App::instance(); $app->bindPaths(require_once __DIR__ . '/paths.php'); $app->environment(\Xaircraft\Globals::ENV_MODE, \Xaircraft\Globals::MODE_DEV); return $app;
public function beforeStop() { File::appendText(App::path('cache') . "/" . get_called_class() . "_stop.log", $this->getPID() . "_" . posix_getpid() . "_" . time() . "\r\n"); }
private function initialize() { if (!function_exists("pcntl_signal_dispatch")) { declare (ticks=10); } $this->registeSignalHandler(function ($signal) { switch ($signal) { case SIGTERM: case SIGHUP: case SIGQUIT: $this->onStop(); App::end(); break; default: return false; } return true; }); if (function_exists("gc_enable")) { gc_enable(); } }
private function getControllerPath($className) { $className = substr($className, 0, strpos($className, "_controller")); $className = str_replace('_', DIRECTORY_SEPARATOR, $className); return App::path("app") . '/controllers/' . $className . '.php'; }