|
@@ -5,33 +5,26 @@ declare(strict_types=1);
|
|
|
namespace SixShop\System\Hook;
|
|
namespace SixShop\System\Hook;
|
|
|
|
|
|
|
|
use SixShop\Core\Attribute\Hook;
|
|
use SixShop\Core\Attribute\Hook;
|
|
|
|
|
+use SixShop\System\Entity\CronJobLogEntity;
|
|
|
use SixShop\System\Event\CronJobAfterEvent;
|
|
use SixShop\System\Event\CronJobAfterEvent;
|
|
|
-use think\Cache;
|
|
|
|
|
|
|
|
|
|
class CronJobHook
|
|
class CronJobHook
|
|
|
{
|
|
{
|
|
|
- private const CACHE_KEY = 'crontab_list';
|
|
|
|
|
-
|
|
|
|
|
- public function __construct(private Cache $cache)
|
|
|
|
|
|
|
+ public function __construct(private CronJobLogEntity $cronJobLogEntity)
|
|
|
{
|
|
{
|
|
|
- // 清空上次运行的缓存数据
|
|
|
|
|
- $this->cache->delete(self::CACHE_KEY);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 每次任务执行后更新最后执行时间和运行状态
|
|
|
|
|
|
|
+ * 每次任务执行后记录执行日志到数据库
|
|
|
*/
|
|
*/
|
|
|
#[Hook(CronJobAfterEvent::class)]
|
|
#[Hook(CronJobAfterEvent::class)]
|
|
|
public function onJobExecuted(CronJobAfterEvent $event): void
|
|
public function onJobExecuted(CronJobAfterEvent $event): void
|
|
|
{
|
|
{
|
|
|
- $crontabList = $this->cache->get(self::CACHE_KEY, []);
|
|
|
|
|
- $crontabList[$event->name] = [
|
|
|
|
|
|
|
+ $this->cronJobLogEntity->save([
|
|
|
'name' => $event->name,
|
|
'name' => $event->name,
|
|
|
'rule' => $event->rule,
|
|
'rule' => $event->rule,
|
|
|
- 'last_time' => date('Y-m-d H:i:s'),
|
|
|
|
|
'duration' => $event->duration,
|
|
'duration' => $event->duration,
|
|
|
'success' => $event->isSuccessful(),
|
|
'success' => $event->isSuccessful(),
|
|
|
- ];
|
|
|
|
|
- $this->cache->set(self::CACHE_KEY, $crontabList);
|
|
|
|
|
|
|
+ ]);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|