SystemCron.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\System\Cron;
  4. use SixShop\Core\Attribute\Cron;
  5. use think\Cache;
  6. use Workerman\Crontab\Crontab;
  7. use Workerman\Worker;
  8. class SystemCron
  9. {
  10. public function __construct(private Cache $cache, private bool $isStart = false)
  11. {
  12. }
  13. #[Cron('1 * * * * *', 'system.cron')]
  14. public function onWorkerStart(): void
  15. {
  16. $crontabList = [];
  17. foreach (Crontab::getAll() as $item) {
  18. /* @var Crontab $item */
  19. $item = [
  20. 'rule' => $item->getRule(),
  21. 'name' => $item->getName(),
  22. 'id' => $item->getId(),
  23. 'time' => date('Y-m-d H:i:s'),
  24. ];
  25. $crontabList[] = $item;
  26. if (!$this->isStart) {
  27. Worker::safeEcho(sprintf('[%s] [%s] [%s] [%s]', $item['time'], $item['id'], $item['name'], $item['rule']) . "\n");
  28. }
  29. }
  30. $this->cache->set('crontab_list', $crontabList);
  31. $this->isStart = true;
  32. }
  33. #[Cron('1 0 0 * * *', 'system.cron.change_log_file')]
  34. public function changeLogFile(): void
  35. {
  36. Worker::$logFile = root_path('runtime/log') . date('Ymd') . '.crontab.log';
  37. }
  38. }