From 80d6389aaf21aa3b31a5a84f0cbae31fe82844eb Mon Sep 17 00:00:00 2001 From: zhangrunyu Date: Wed, 12 Jan 2022 13:10:52 +0800 Subject: [PATCH 1/3] Add roadrunner cron --- framework/base/router.class.php | 38 +++++++++++++------ module/cron/control.php | 26 ++++++++++--- module/cron/model.php | 21 +++++++++++ roadrunner/.rr.yaml | 15 ++++++++ roadrunner/README.md | 13 +++++++ roadrunner/composer.json | 6 +++ roadrunner/cron-worker.php | 65 +++++++++++++++++++++++++++++++++ roadrunner/psr-worker.php | 31 ++++++++++++++++ 8 files changed, 197 insertions(+), 18 deletions(-) create mode 100644 roadrunner/.rr.yaml create mode 100644 roadrunner/README.md create mode 100644 roadrunner/composer.json create mode 100644 roadrunner/cron-worker.php create mode 100644 roadrunner/psr-worker.php diff --git a/framework/base/router.class.php b/framework/base/router.class.php index 15b02574ca..b4e9fdf481 100644 --- a/framework/base/router.class.php +++ b/framework/base/router.class.php @@ -1891,23 +1891,37 @@ class baseRouter $defaultParams[$name] = $default; } - /** - * 根据PATH_INFO或者GET方式设置请求的参数。 - * Set params according PATH_INFO or GET. - */ - if($this->config->requestType != 'GET') + if ('cli' === PHP_SAPI) { - $this->setParamsByPathInfo($defaultParams); + if ($this->params) + { + $this->params = array_merge($defaultParams, $this->params); + } + else + { + $this->params = $defaultParams; + } } else { - $this->setParamsByGET($defaultParams); - } + /** + * 根据PATH_INFO或者GET方式设置请求的参数。 + * Set params according PATH_INFO or GET. + */ + if($this->config->requestType != 'GET') + { + $this->setParamsByPathInfo($defaultParams); + } + else + { + $this->setParamsByGET($defaultParams); + } - if($this->config->framework->filterParam == 2) - { - $_GET = validater::filterParam($_GET, 'get'); - $_COOKIE = validater::filterParam($_COOKIE, 'cookie'); + if($this->config->framework->filterParam == 2) + { + $_GET = validater::filterParam($_GET, 'get'); + $_COOKIE = validater::filterParam($_COOKIE, 'cookie'); + } } /* 调用该方法 Call the method. */ diff --git a/module/cron/control.php b/module/cron/control.php index 2486ba3f3f..ca2bfd7f84 100644 --- a/module/cron/control.php +++ b/module/cron/control.php @@ -134,9 +134,12 @@ class cron extends control */ public function ajaxExec($restart = false) { - ignore_user_abort(true); - set_time_limit(0); - session_write_close(); + if ('cli' !== PHP_SAPI) + { + ignore_user_abort(true); + set_time_limit(0); + session_write_close(); + } /* Check cron turnon. */ if(empty($this->config->global->cron)) die(); @@ -183,11 +186,21 @@ class cron extends control /* Skip cron that status is running and run time is less than max. */ if($cronInfo->status == 'running' and (time() - strtotime($cronInfo->lastTime)) < $this->config->cron->maxRunTime) continue; /* Skip cron that last time is more than this cron time. */ - if($cronInfo->lastTime > $cron['time']->format(DT_DATETIME1)) die(); + if ('cli' === PHP_SAPI) + { + if($cronInfo->lastTime >= $cron['time']->format(DT_DATETIME1)) continue; + } + else + { + if($cronInfo->lastTime > $cron['time']->format(DT_DATETIME1)) die(); + } if($now > $cron['time']) { - $this->cron->changeStatus($id, 'running'); + if (!$this->cron->changeStatusRunning($id, $cronInfo->lastTime)) + { + continue; + } $parsedCrons[$id]['time'] = $cron['cron']->getNextRunDate(); /* Execution command. */ @@ -239,11 +252,12 @@ class cron extends control sleep($sleepTime); /* Break while. */ - if(connection_status() != CONNECTION_NORMAL) break; + if('cli' !== PHP_SAPI && connection_status() != CONNECTION_NORMAL) break; if(((time() - $startedTime) / 3600 / 24) >= $this->config->cron->maxRunDays) break; } /* Revert cron status to stop. */ $this->cron->markCronStatus('stop', $configID); } + } diff --git a/module/cron/model.php b/module/cron/model.php index ab9822d8fc..e857b4dcca 100644 --- a/module/cron/model.php +++ b/module/cron/model.php @@ -94,6 +94,27 @@ class cronModel extends model return dao::isError() ? false : true; } + /** + * Change cron status to running + * + * @param int $cronID + * @param string $lastTime + * @access public + * @return bool|int + */ + public function changeStatusRunning($cronID, $lastTime) + { + $data = new stdclass(); + $data->status = 'running'; + $data->lastTime = date(DT_DATETIME1); + $rows = $this->dao->update(TABLE_CRON)->data($data) + ->where('id')->eq($cronID) + ->andWhere('status')->ne('running') + ->andWhere('lastTime')->eq($lastTime) + ->exec(); + return dao::isError() ? false : $rows; + } + /** * Log cron. * diff --git a/roadrunner/.rr.yaml b/roadrunner/.rr.yaml new file mode 100644 index 0000000000..e680602899 --- /dev/null +++ b/roadrunner/.rr.yaml @@ -0,0 +1,15 @@ +server: + command: "php psr-worker.php" + +http: + address: 0.0.0.0:8080 + pool: + num_workers: 4 + +service: + cron: + command: php cron-worker.php + process_num: 4 + exec_timeout: 0 + remain_after_exit: true + restart_sec: 1 diff --git a/roadrunner/README.md b/roadrunner/README.md new file mode 100644 index 0000000000..741026a58a --- /dev/null +++ b/roadrunner/README.md @@ -0,0 +1,13 @@ +# 说明 + +## 环境安装 + +安装 RoadRunner: + +Composer: `composer update` + +## 启动服务 + +```shell +rr serve +``` diff --git a/roadrunner/composer.json b/roadrunner/composer.json new file mode 100644 index 0000000000..67092734e8 --- /dev/null +++ b/roadrunner/composer.json @@ -0,0 +1,6 @@ +{ + "require": { + "spiral/roadrunner": "^2.6", + "nyholm/psr7": "^1.4" + } +} diff --git a/roadrunner/cron-worker.php b/roadrunner/cron-worker.php new file mode 100644 index 0000000000..af88db57cf --- /dev/null +++ b/roadrunner/cron-worker.php @@ -0,0 +1,65 @@ + + * @package ZenTaoPMS + * @version $Id: index.php 5036 2013-07-06 05:26:44Z wyd621@gmail.com $ + * @link http://www.zentao.net + */ +/* Set the error reporting. */ +error_reporting(0); + +/* Load the framework. */ +include '../framework/router.class.php'; +include '../framework/control.class.php'; +include '../framework/model.class.php'; +include '../framework/helper.class.php'; + +/* Log the time and define the run mode. */ +$startTime = getTime(); + +/* Instance the app. */ +$app = router::createApp('pms', dirname(__DIR__), 'router'); + +// $_GET[$app->config->moduleVar] = 'cron'; +// $_GET[$app->config->methodVar] = 'ajaxExec'; +// $_SERVER['REQUEST_METHOD'] = 'GET'; +// $_SERVER['REQUEST_URI'] = '/'; +// $_GET['once'] = true; + +// $app->moduleName = 'cron'; +// $app->methodName = 'ajaxExec'; + +/* Run the app. */ +$common = $app->loadCommon(); + +// var_dump(1); +/* Check entry. */ +// $common->checkEntry(); +// var_dump(2); +// $common->loadConfigFromDB(); +// var_dump(3); + +/* Set default params. */ +// if(!$app->version) $config->requestType = 'GET'; +// $config->default->view = 'json'; + +// $app->parseRequest(); + +// /* Old version need check priv here, new version check priv in entry. */ +// if(!$app->version) $common->checkPriv(); + +// $app->moduleName = 'cron'; +// $app->methodName = 'ajaxExec'; + +// $app->parseRequest(); +// $common->checkPriv(); +$app->moduleName = 'cron'; +$app->methodName = 'ajaxExec'; +$app->setControlFile(); +$app->loadModule(); diff --git a/roadrunner/psr-worker.php b/roadrunner/psr-worker.php new file mode 100644 index 0000000000..92bc198ec8 --- /dev/null +++ b/roadrunner/psr-worker.php @@ -0,0 +1,31 @@ +waitRequest(); + + if (!($request instanceof \Psr\Http\Message\ServerRequestInterface)) { // Termination request received + break; + } + } catch (\Throwable) { + $psr7->respond(new Psr7\Response(400)); // Bad Request + continue; + } + + try { + // Application code logic + $psr7->respond(new Psr7\Response(200, [], 'Hello RoadRunner!')); + } catch (\Throwable) { + $psr7->respond(new Psr7\Response(500, [], 'Something Went Wrong!')); + } +} From debd83264f7ab81183def79ae94021a9f7d354c3 Mon Sep 17 00:00:00 2001 From: zhangrunyu Date: Wed, 12 Jan 2022 13:20:48 +0800 Subject: [PATCH 2/3] clean code --- roadrunner/cron-worker.php | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/roadrunner/cron-worker.php b/roadrunner/cron-worker.php index af88db57cf..f58bdd732e 100644 --- a/roadrunner/cron-worker.php +++ b/roadrunner/cron-worker.php @@ -26,39 +26,9 @@ $startTime = getTime(); /* Instance the app. */ $app = router::createApp('pms', dirname(__DIR__), 'router'); -// $_GET[$app->config->moduleVar] = 'cron'; -// $_GET[$app->config->methodVar] = 'ajaxExec'; -// $_SERVER['REQUEST_METHOD'] = 'GET'; -// $_SERVER['REQUEST_URI'] = '/'; -// $_GET['once'] = true; - -// $app->moduleName = 'cron'; -// $app->methodName = 'ajaxExec'; - /* Run the app. */ $common = $app->loadCommon(); -// var_dump(1); -/* Check entry. */ -// $common->checkEntry(); -// var_dump(2); -// $common->loadConfigFromDB(); -// var_dump(3); - -/* Set default params. */ -// if(!$app->version) $config->requestType = 'GET'; -// $config->default->view = 'json'; - -// $app->parseRequest(); - -// /* Old version need check priv here, new version check priv in entry. */ -// if(!$app->version) $common->checkPriv(); - -// $app->moduleName = 'cron'; -// $app->methodName = 'ajaxExec'; - -// $app->parseRequest(); -// $common->checkPriv(); $app->moduleName = 'cron'; $app->methodName = 'ajaxExec'; $app->setControlFile(); From 0230675445ab1b4dd1d9d6ae9c1ef158549dff53 Mon Sep 17 00:00:00 2001 From: zhangrunyu Date: Thu, 13 Jan 2022 09:25:59 +0800 Subject: [PATCH 3/3] Remove server and http in roadrunner --- roadrunner/.rr.yaml | 8 -------- roadrunner/psr-worker.php | 31 ------------------------------- 2 files changed, 39 deletions(-) delete mode 100644 roadrunner/psr-worker.php diff --git a/roadrunner/.rr.yaml b/roadrunner/.rr.yaml index e680602899..e98c8a112c 100644 --- a/roadrunner/.rr.yaml +++ b/roadrunner/.rr.yaml @@ -1,11 +1,3 @@ -server: - command: "php psr-worker.php" - -http: - address: 0.0.0.0:8080 - pool: - num_workers: 4 - service: cron: command: php cron-worker.php diff --git a/roadrunner/psr-worker.php b/roadrunner/psr-worker.php deleted file mode 100644 index 92bc198ec8..0000000000 --- a/roadrunner/psr-worker.php +++ /dev/null @@ -1,31 +0,0 @@ -waitRequest(); - - if (!($request instanceof \Psr\Http\Message\ServerRequestInterface)) { // Termination request received - break; - } - } catch (\Throwable) { - $psr7->respond(new Psr7\Response(400)); // Bad Request - continue; - } - - try { - // Application code logic - $psr7->respond(new Psr7\Response(200, [], 'Hello RoadRunner!')); - } catch (\Throwable) { - $psr7->respond(new Psr7\Response(500, [], 'Something Went Wrong!')); - } -}