Explorar o código

refactor(cron): 只在首次运行时输出定时任务列表

runphp hai 6 meses
pai
achega
448d33e08a
Modificáronse 1 ficheiros con 8 adicións e 4 borrados
  1. 8 4
      src/Cron/SystemCron.php

+ 8 - 4
src/Cron/SystemCron.php

@@ -8,9 +8,9 @@ use think\Cache;
 use Workerman\Crontab\Crontab;
 use Workerman\Worker;
 
-readonly class SystemCron
+class SystemCron
 {
-    public function __construct(private Cache $cache)
+    public function __construct(private Cache $cache, private bool $isStart = false)
     {
     }
 
@@ -20,14 +20,18 @@ readonly class SystemCron
         $crontabList = [];
         foreach (Crontab::getAll() as $item) {
             /* @var Crontab $item */
-            $crontabList[] = [
+            $item = [
                 'rule' => $item->getRule(),
                 'name' => $item->getName(),
                 'id' => $item->getId(),
                 'time' => date('Y-m-d H:i:s'),
             ];
-            Worker::safeEcho('[Cron] ' . $item->getName() . ' ' . $item->getRule() . "\n");
+            $crontabList[] = $item;
+            if (!$this->isStart) {
+                Worker::safeEcho(sprintf('[%s] [%s] [%s] [%s]', $item['time'], $item['id'], $item['name'], $item['rule']) . "\n");
+            }
         }
         $this->cache->set('crontab_list', $crontabList);
+        $this->isStart = true;
     }
 }