Explorar o código

优化定时任务模块(支持守护进程)

Yahoo %!s(int64=4) %!d(string=hai) anos
pai
achega
97c3f0fd70
Modificáronse 3 ficheiros con 8 adicións e 9 borrados
  1. 3 2
      app/console/command/Timer.php
  2. 0 1
      app/console/task/Store.php
  3. 5 6
      app/console/task/Task.php

+ 3 - 2
app/console/command/Timer.php

@@ -8,7 +8,7 @@
 // +----------------------------------------------------------------------
 // | Author: 萤火科技 <admin@yiovo.com>
 // +----------------------------------------------------------------------
-declare (strict_types = 1);
+declare (strict_types=1);
 
 namespace app\console\command;
 
@@ -85,6 +85,7 @@ class Timer extends Command
         return $this->timer = \Workerman\Lib\Timer::add($this->interval, function () use (&$task) {
             try {
                 // 这里执行系统预设的定时任务事件
+                echo 'timer...' . PHP_EOL;
                 Event::trigger('StoreTask');
             } catch (\Throwable $e) {
                 echo 'ERROR: ' . $e->getMessage() . PHP_EOL;
@@ -97,7 +98,7 @@ class Timer extends Command
      * 停止/删除定时器
      * @return bool
      */
-    public function stop()
+    public function stop(): bool
     {
         return \Workerman\Lib\Timer::del($this->timer);
     }

+ 0 - 1
app/console/task/Store.php

@@ -27,7 +27,6 @@ class Store extends Task
      */
     public function handle()
     {
-        echo 'StoreTask' . PHP_EOL;
         // 遍历商城列表并执行定时任务
         $storeIds = StoreModel::getStoreIds();
         foreach ($storeIds as $storeId) {

+ 5 - 6
app/console/task/Task.php

@@ -8,7 +8,7 @@
 // +----------------------------------------------------------------------
 // | Author: 萤火科技 <admin@yiovo.com>
 // +----------------------------------------------------------------------
-declare (strict_types = 1);
+declare (strict_types=1);
 
 namespace app\console\task;
 
@@ -33,7 +33,7 @@ class Task
     {
         if (!$this->hasTaskId($storeId, $key)) {
             $this->setTaskId($storeId, $key, $expire);
-            return $callback();
+            return call_user_func($callback);
         }
     }
 
@@ -41,9 +41,9 @@ class Task
      * 获取任务ID
      * @param int $storeId
      * @param string $key
-     * @return mixed
+     * @return bool
      */
-    protected function hasTaskId(int $storeId, string $key)
+    protected function hasTaskId(int $storeId, string $key): bool
     {
         return Cache::has("Listener:$storeId:$key");
     }
@@ -56,9 +56,8 @@ class Task
      * @param int $expire 任务ID过期时长(单位:秒)
      * @return bool
      */
-    protected function setTaskId(int $storeId, string $key, int $expire = 60)
+    protected function setTaskId(int $storeId, string $key, int $expire = 60): bool
     {
         return Cache::set("Listener:$storeId:$key", true, $expire);
     }
-
 }