diff --git a/apps/config/http_base.php b/apps/config/http_base.php index 8a35cb8..40f9588 100644 --- a/apps/config/http_base.php +++ b/apps/config/http_base.php @@ -209,6 +209,16 @@ ], ], + // 定时器配置 + 'timer' => [ + 'crontab' => [ + 'class' => \apps\task\CronTabTimer::class, + 'type' => \Rid\Base\Timer::TICK, + 'msec' => 1 * 1000, // 单位为毫秒 + 'callback' => 'init' + ] + ], + // 类库配置 'libraries' => [ 'swiftmailer' => [ diff --git a/apps/task/CronTabTimer.php b/apps/task/CronTabTimer.php new file mode 100644 index 0000000..51d028f --- /dev/null +++ b/apps/task/CronTabTimer.php @@ -0,0 +1,19 @@ +{$type}($msec, [$this, $callback]); + } } diff --git a/framework/Http/HttpServer.php b/framework/Http/HttpServer.php index 4bbca3d..0aa7189 100644 --- a/framework/Http/HttpServer.php +++ b/framework/Http/HttpServer.php @@ -5,6 +5,7 @@ use Rid\Base\BaseObject; use Rid\Base\TaskInterface; +use Rid\Base\Timer; use Rid\Exceptions\TaskException; use Rid\Helpers\ProcessHelper; @@ -108,6 +109,16 @@ protected function onWorkerStart() $app->setServ($this->_server); $app->setWorker($workerId); $app->loadAllComponents(); + + if ($workerId == 0) { // 将系统设置中的 Timer 添加到 worker #0 中 + foreach (app()->env('timer') as $timer_name => $timer_config) { + $timer_class = $timer_config['class']; + $timer = new $timer_class(); + if ($timer instanceof \Rid\Base\TimerInterface) { + $timer->run($timer_config); + } + } + } }); } diff --git a/framework/Rid.php b/framework/Rid.php index a37eecc..ecd28de 100644 --- a/framework/Rid.php +++ b/framework/Rid.php @@ -7,7 +7,7 @@ class Rid { // 版本号 - const VERSION = 'v0.1.2-alpha'; + const VERSION = 'v0.1.3-alpha'; // App实例 protected static $_app;