Browse Source

增加了定时任务组件

panda 5 months ago
parent
commit
1178a4073b
4 changed files with 16 additions and 13 deletions
  1. 1 0
      application/command.php
  2. 4 4
      application/task/Kernel.php
  3. 3 9
      application/task/time/DemoTime.php
  4. 8 0
      public/.htaccess

+ 1 - 0
application/command.php

@@ -17,4 +17,5 @@ return [
     'app\admin\command\Min',
     'app\admin\command\Addon',
     'app\admin\command\Api',
+    'app\admin\command\TimedTask',
 ];

+ 4 - 4
application/task/Kernel.php

@@ -26,17 +26,17 @@ class Kernel{
      */
     public function schedule()
     {
-        $this->command('DemoTime','1','minute');// 默认间隔1分钟触发 daily 单位分钟
+        $this->command(DemoTime::class,'1','minute');// 默认间隔1分钟触发 daily 单位分钟
     }
 
     /**
      * 定时任务触发时间
-     * @param string $class
+     * @param $class
      * @param string $daily
      * @param string $type
      * @return false|\think\console\Output
      */
-    private function command(string $class,string $daily = '',string $type = 'minute')
+    private function command($class,string $daily = '',string $type = 'minute')
     {
         // 按分钟、按每天指定点数
         if (!in_array($type,['minute','at'])) {
@@ -62,6 +62,6 @@ class Kernel{
             }
         }
 
-        return Console::call($class);
+        return (new $class)->execute();
     }
 }

+ 3 - 9
application/task/time/DemoTime.php

@@ -15,18 +15,12 @@ use think\console\Output;
  * Class Kernel
  * @package app\task
  */
-class DemoTime extends Command{
-
-    protected function configure()
-    {
-        $this->setName('DemoTime')->setDescription('Here is the remark ');
-    }
-
+class DemoTime{
     // 业务代码 业务代码执行时间过长,会造成堵塞,请合理安排执行任务
-    protected function execute(Input $input, Output $output)
+    // 建议结合 queue 执行超长任务
+    public function execute()
     {
 //        Queue::work(DemoJob::class,['a' => 1]);
-        echo 11;
         return 0;
     }
 }

+ 8 - 0
public/.htaccess

@@ -0,0 +1,8 @@
+location ~* (runtime|application)/{
+	return 403;
+}
+location / {
+	if (!-e $request_filename){
+		rewrite  ^(.*)$  /index.php?s=$1  last;   break;
+	}
+}