Browse Source

定时任务优化

吴昊天 2 years ago
parent
commit
61c67d2717

+ 2 - 2
crmeb/app/api/controller/v1/TimerController.php

@@ -20,9 +20,9 @@ class TimerController
      * @email 442384644@qq.com
      * @date 2023/02/17
      */
-    public function timerRun()
+    public function crontabRun()
     {
-        app()->make(SystemTimerServices::class)->timerRun();
+        app()->make(SystemTimerServices::class)->crontabRun();
     }
 
     /**

+ 1 - 1
crmeb/app/api/route/v1.php

@@ -391,7 +391,7 @@ Route::group(function () {
 
     /** 定时任务接口 */
     //定时任务调用接口
-    Route::get('timer/run', 'v1.TimerController/timerRun')->name('timerRun');
+    Route::get('crontab/run', 'v1.TimerController/crontabRun')->name('crontabRun');
     //检测定时任务接口
     Route::get('timer/check', 'v1.TimerController/timerCheck')->name('timerCheck');
     //未支付自动取消订单

+ 14 - 32
crmeb/app/listener/crontab/SystemCrontab.php

@@ -23,9 +23,8 @@ class SystemCrontab implements ListenerInterface
     public function handle($event): void
     {
         //自动写入文件方便检测是否启动定时任务命令
-        $time = time();
-        new Crontab('*/6 * * * * *', function () use ($time) {
-            file_put_contents(root_path() . 'runtime/.timer', $time);
+        new Crontab('*/6 * * * * *', function () {
+            file_put_contents(root_path() . 'runtime/.timer', time());
         });
 
         /** @var SystemTimerServices $systemTimerServices */
@@ -38,9 +37,7 @@ class SystemCrontab implements ListenerInterface
             if ($item['mark'] == 'order_cancel') {
                 new Crontab($timeStr, function () {
                     try {
-                        /** @var StoreOrderServices $orderServices */
-                        $orderServices = app()->make(StoreOrderServices::class);
-                        $orderServices->orderUnpaidCancel();
+                        app()->make(StoreOrderServices::class)->orderUnpaidCancel();
                         $this->crontabLog(' 执行未支付自动取消订单');
                     } catch (\Throwable $e) {
                         Log::error('自动取消订单失败,失败原因:' . $e->getMessage());
@@ -51,9 +48,7 @@ class SystemCrontab implements ListenerInterface
             elseif ($item['mark'] == 'pink_expiration') {
                 new Crontab($timeStr, function () {
                     try {
-                        /** @var StorePinkServices $storePinkServices */
-                        $storePinkServices = app()->make(StorePinkServices::class);
-                        $storePinkServices->statusPink();
+                        app()->make(StorePinkServices::class)->statusPink();
                         $this->crontabLog(' 执行拼团到期订单处理');
                     } catch (\Throwable $e) {
                         Log::error('拼团到期订单处理失败,失败原因:' . $e->getMessage());
@@ -64,9 +59,7 @@ class SystemCrontab implements ListenerInterface
             elseif ($item['mark'] == 'agent_unbind') {
                 new Crontab($timeStr, function () {
                     try {
-                        /** @var AgentManageServices $agentManage */
-                        $agentManage = app()->make(AgentManageServices::class);
-                        $agentManage->removeSpread();
+                        app()->make(AgentManageServices::class)->removeSpread();
                         $this->crontabLog(' 执行自动解绑上级绑定');
                     } catch (\Throwable $e) {
                         Log::error('自动解除上级绑定失败,失败原因:' . $e->getMessage());
@@ -77,9 +70,7 @@ class SystemCrontab implements ListenerInterface
             elseif ($item['mark'] == 'live_product_status') {
                 new Crontab($timeStr, function () {
                     try {
-                        /** @var LiveGoodsServices $liveGoods */
-                        $liveGoods = app()->make(LiveGoodsServices::class);
-                        $liveGoods->syncGoodStatus();
+                        app()->make(LiveGoodsServices::class)->syncGoodStatus();
                         $this->crontabLog(' 执行更新直播商品状态');
                     } catch (\Throwable $e) {
                         Log::error('更新直播商品状态失败,失败原因:' . $e->getMessage());
@@ -90,11 +81,8 @@ class SystemCrontab implements ListenerInterface
             elseif ($item['mark'] == 'live_room_status') {
                 new Crontab($timeStr, function () {
                     try {
-                        /** @var LiveRoomServices $liveRoom */
-                        $liveRoom = app()->make(LiveRoomServices::class);
-                        $liveRoom->syncRoomStatus();
+                        app()->make(LiveRoomServices::class)->syncRoomStatus();
                         $this->crontabLog(' 执行更新直播间状态');
-
                     } catch (\Throwable $e) {
                         Log::error('更新直播间状态失败,失败原因:' . $e->getMessage());
                     }
@@ -104,9 +92,7 @@ class SystemCrontab implements ListenerInterface
             elseif ($item['mark'] == 'take_delivery') {
                 new Crontab($timeStr, function () {
                     try {
-                        /** @var StoreOrderTakeServices $services */
-                        $services = app()->make(StoreOrderTakeServices::class);
-                        $services->autoTakeOrder();
+                        app()->make(StoreOrderTakeServices::class)->autoTakeOrder();
                         $this->crontabLog(' 执行自动收货');
                     } catch (\Throwable $e) {
                         Log::error('自动收货失败,失败原因:' . $e->getMessage());
@@ -117,9 +103,7 @@ class SystemCrontab implements ListenerInterface
             elseif ($item['mark'] == 'advance_off') {
                 new Crontab($timeStr, function () {
                     try {
-                        /** @var StoreProductServices $product */
-                        $product = app()->make(StoreProductServices::class);
-                        $product->downAdvance();
+                        app()->make(StoreProductServices::class)->downAdvance();
                         $this->crontabLog(' 执行预售到期商品自动下架');
                     } catch (\Throwable $e) {
                         Log::error('预售到期商品自动下架失败,失败原因:' . $e->getMessage());
@@ -130,9 +114,7 @@ class SystemCrontab implements ListenerInterface
             elseif ($item['mark'] == 'product_replay') {
                 new Crontab($timeStr, function () {
                     try {
-                        /** @var StoreOrderServices $orderServices */
-                        $orderServices = app()->make(StoreOrderServices::class);
-                        $orderServices->autoComment();
+                        app()->make(StoreOrderServices::class)->autoComment();
                         $this->crontabLog(' 执行自动好评');
                     } catch (\Throwable $e) {
                         Log::error('自动好评失败,失败原因:' . $e->getMessage());
@@ -143,9 +125,7 @@ class SystemCrontab implements ListenerInterface
             elseif ($item['mark'] == 'clear_poster') {
                 new Crontab($timeStr, function () {
                     try {
-                        /** @var SystemAttachmentServices $attach */
-                        $attach = app()->make(SystemAttachmentServices::class);
-                        $attach->emptyYesterdayAttachment();
+                        app()->make(SystemAttachmentServices::class)->emptyYesterdayAttachment();
                         $this->crontabLog(' 执行清除昨日海报');
                     } catch (\Throwable $e) {
                         Log::error('清除昨日海报失败,失败原因:' . $e->getMessage());
@@ -159,7 +139,8 @@ class SystemCrontab implements ListenerInterface
         }
     }
 
-    /** 定时任务日志
+    /**
+     * 定时任务日志
      * @param $msg
      */
     public function crontabLog($msg)
@@ -170,6 +151,7 @@ class SystemCrontab implements ListenerInterface
             Log::notice($date . $msg);
         }
     }
+
     /**
      *  0   1   2   3   4   5
      * |   |   |   |   |   |

+ 27 - 14
crmeb/app/services/system/timer/SystemTimerServices.php

@@ -203,55 +203,68 @@ class SystemTimerServices extends BaseServices
      * @email 442384644@qq.com
      * @date 2023/02/17
      */
-    public function timerRun()
+    public function crontabRun()
     {
-        $time = time();
-        $date = date('Y-m-d H:i:s', time());
-        $timer_log_open = config("log.timer_log", false);
-        file_put_contents(root_path() . 'runtime/.timer', $time); //检测定时任务是否正常
+        file_put_contents(root_path() . 'runtime/.timer', time()); //检测定时任务是否正常
         $list = $this->dao->selectList(['is_open' => 1, 'is_del' => 0])->toArray();
         foreach ($list as $item) {
             if ($item['next_execution_time'] < $time) {
                 if ($item['mark'] == 'order_cancel') {
                     //未支付自动取消订单
                     app()->make(StoreOrderServices::class)->orderUnpaidCancel();
-                    if($timer_log_open) Log::notice($date . ' 执行未支付自动取消订单');
+                    $this->crontabLog(' 执行未支付自动取消订单');
                 } elseif ($item['mark'] == 'pink_expiration') {
                     //拼团到期订单处理
                     app()->make(StorePinkServices::class)->statusPink();
-                    if($timer_log_open) Log::notice($date . ' 执行拼团到期订单处理');
+                    $this->crontabLog(' 执行拼团到期订单处理');
                 } elseif ($item['mark'] == 'agent_unbind') {
                     //自动解绑上级绑定
                     app()->make(AgentManageServices::class)->removeSpread();
-                    if($timer_log_open) Log::notice($date . ' 执行自动解绑上级绑定');
+                    $this->crontabLog(' 执行自动解绑上级绑定');
                 } elseif ($item['mark'] == 'live_product_status') {
                     //更新直播商品状态
                     app()->make(LiveGoodsServices::class)->syncGoodStatus();
-                    if($timer_log_open) Log::notice($date . ' 执行更新直播商品状态');
+                    $this->crontabLog(' 执行更新直播商品状态');
                 } elseif ($item['mark'] == 'live_room_status') {
                     //更新直播间状态
                     app()->make(LiveRoomServices::class)->syncRoomStatus();
-                    if($timer_log_open) Log::notice($date . ' 执行更新直播间状态');
+                    $this->crontabLog(' 执行更新直播间状态');
                 } elseif ($item['mark'] == 'take_delivery') {
                     //自动收货
                     app()->make(StoreOrderTakeServices::class)->autoTakeOrder();
-                    if($timer_log_open) Log::notice($date . ' 执行自动收货');
+                    $this->crontabLog(' 执行自动收货');
                 } elseif ($item['mark'] == 'advance_off') {
                     //查询预售到期商品自动下架
                     app()->make(StoreProductServices::class)->downAdvance();
-                    if($timer_log_open) Log::notice($date . ' 执行预售到期商品自动下架');
+                    $this->crontabLog(' 执行预售到期商品自动下架');
                 } elseif ($item['mark'] == 'product_replay') {
                     //自动好评
                     app()->make(StoreOrderServices::class)->autoComment();
-                    if($timer_log_open) Log::notice($date . ' 执行自动好评');
+                    $this->crontabLog(' 执行自动好评');
                 } elseif ($item['mark'] == 'clear_poster') {
                     //清除昨日海报
                     app()->make(SystemAttachmentServices::class)->emptyYesterdayAttachment();
-                    if($timer_log_open) Log::notice($date . ' 执行清除昨日海报');
+                    $this->crontabLog(' 执行清除昨日海报');
                 }
                 //写入本次执行时间和下次执行时间
                 $this->dao->update(['mark' => $item['mark']], ['last_execution_time' => $time, 'next_execution_time' => $this->getTimerCycleTime($item)]);
             }
         }
     }
+
+    /**
+     * 定时任务日志
+     * @param $msg
+     * @author 吴汐
+     * @email 442384644@qq.com
+     * @date 2023/02/21
+     */
+    public function crontabLog($msg)
+    {
+        $timer_log_open = config("log.timer_log", false);
+        if ($timer_log_open) {
+            $date = date('Y-m-d H:i:s', time());
+            Log::notice($date . $msg);
+        }
+    }
 }