Kirin 3 settimane fa
parent
commit
d2acb370d8

+ 72 - 0
app/command/AutoTask.php

@@ -0,0 +1,72 @@
+<?php
+/**
+ * @Created by PhpStorm
+ * @author: Kirin
+ * @day: 2025/2/19
+ * @time: 16:58
+ */
+
+namespace app\command;
+
+use Exception;
+use qiniu\services\blockchain\TransactionService;
+use Swoole\Coroutine;
+use think\console\Command;
+use think\console\Input;
+use think\console\Output;
+use think\facade\Cache;
+use think\facade\Log;
+
+
+class AutoTask extends Command
+{
+
+    private $task = [
+        'test' => ['name' => '测试', 'time' => 300],
+    ];
+
+    protected function configure()
+    {
+        // 指令配置
+        $this->setName('autoTask')
+            ->setDescription('自动程序');
+    }
+
+    protected function execute(Input $input, Output $output)
+    {
+        go(function () {
+            while (true) {
+                foreach ($this->task as $k => $v) {
+                    $last_time = Cache::store('redis')->get($k . '_time', 0);
+                    if ($last_time + $v['time'] < time()) {
+                        try {
+                            $this->$k();
+                            $this->log($v['name'] . '执行');
+                        } catch (\Throwable $e) {
+                            Log::error($v['name'] . '运行出错:' . $e->getMessage() . '[' . $e->getFile() . ':' . $e->getLine() . ']');
+                            $this->log($v['name'] . '运行出错:' . $e->getMessage());
+                        }
+                        Cache::store('redis')->set($k . '_time', time());
+                    }
+                }
+                Coroutine::sleep(1);
+            }
+        });
+        $output->info("自动程序启动");
+    }
+
+    /**
+     * @throws Exception
+     */
+    private function test()
+    {
+
+    }
+
+    public function log($msg)
+    {
+        echo "[" . date('Y-m-d H:i:s') . "] " . $msg . PHP_EOL;
+    }
+
+
+}

+ 1 - 4
app/event.php

@@ -15,9 +15,7 @@ return [
             \qiniu\listeners\InitSwooleLockListen::class, //初始化
         ],
         //swoole 启动事件
-        'swoole.workerStart' => [
-//            \qiniu\listeners\SwooleCronListen::class, //定时任务
-        ],
+        'swoole.workerStart' => [],
         'swoole.workerExit' => [],
         'swoole.workerError' => [],
         'swoole.workerStop' => [],
@@ -26,7 +24,6 @@ return [
 //        'swoole.websocket.kefu' => [\app\webscoket\handler\KefuHandler::class],//socket 客服事件
 //        'swoole.websocket.admin' => [\app\webscoket\handler\AdminHandler::class],//socket 后台事件
 
-//        'crontab' => [\app\listener\system\timer\SystemTimer::class],
 
         'config.create' => [\app\listener\system\config\CreateSuccess::class],//创建配置事件
         'config.delete' => [\app\listener\system\config\DeleteSuccess::class],//删除配置事件

+ 0 - 78
app/listener/system/timer/SystemTimer.php

@@ -1,78 +0,0 @@
-<?php
-// +----------------------------------------------------------------------
-// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
-// +----------------------------------------------------------------------
-// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
-// +----------------------------------------------------------------------
-// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
-// +----------------------------------------------------------------------
-// | Author: CRMEB Team <admin@crmeb.com>
-// +----------------------------------------------------------------------
-namespace app\listener\system\timer;
-
-use qiniu\services\blockchain\TransactionService;
-use qiniu\services\CacheService;
-use qiniu\utils\Cron;
-use qiniu\interfaces\ListenerInterface;
-use think\facade\Log;
-
-/**
- * 定时任务
- * Class Create
- * @package app\listener\system\timer
- */
-class SystemTimer extends Cron implements ListenerInterface
-{
-    public $tasks = [
-        'auto' => [
-            'is_open' => 1,
-            'time' => 30,
-        ]
-    ];
-
-    /**
-     * @param $event
-     */
-    public function handle($event): void
-    {
-        $this->tick(1000, function () {
-            $time = time();
-            $list = $this->tasks;
-            /** @var CacheService $cacheService */
-            $cacheService = app()->make(CacheService::class);
-            foreach ($list as $index => $item) {
-                if ($item['is_open'] == 1) {
-                    $data = $cacheService->get($index);
-                    if ($time >= $data + $item['time']) {
-                        $this->after(1000, function () use ($index, $time, $cacheService) {
-                            $cacheService->set($index, $time);//上次执行时间保存
-                            $this->implement_timer($index);
-                        });
-                    }
-                }
-            }
-        });
-    }
-
-    /**
-     * 执行定时任务
-     * @param string $mark
-     * @return bool|void
-     */
-    public function implement_timer(string $mark)
-    {
-        switch ($mark) {
-            case 'auto': //自动取消订单
-                try {
-                    Log::debug('auto');
-                } catch (\Throwable $e) {
-                    response_log_write([
-                        'message' => '失败原因:[' . class_basename($this) . ']' . $e->getMessage(),
-                        'file' => $e->getFile(),
-                        'line' => $e->getLine()
-                    ]);
-                }
-                break;
-        }
-    }
-}

+ 1 - 0
config/console.php

@@ -6,6 +6,7 @@ return [
     // 指令定义
     'commands' => [
         'test' => \app\command\Test::class,
+        'autoTask' => \app\command\AutoTask::class,
         'make' => \qiniu\command\MakeAdmin::class,
     ],
 ];

+ 0 - 34
qiniu/listeners/SwooleCronListen.php

@@ -1,34 +0,0 @@
-<?php
-
-// +----------------------------------------------------------------------
-// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
-// +----------------------------------------------------------------------
-// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
-// +----------------------------------------------------------------------
-// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
-// +----------------------------------------------------------------------
-// | Author: CRMEB Team <admin@crmeb.com>
-// +----------------------------------------------------------------------
-
-
-namespace qiniu\listeners;
-
-use qiniu\interfaces\ListenerInterface;
-use think\facade\Log;
-
-/**
- * swoole 定时任务
- */
-class SwooleCronListen implements ListenerInterface
-{
-
-    public function handle($event): void
-    {
-        try {
-            event('crontab');//app/event.php 里面配置事件
-        } catch (\Throwable $e) {
-            Log::error('监听定时器报错: ' . $e->getMessage());
-        }
-
-    }
-}