From 46caff6949080d40c2c16b0fafd17f79f467c238 Mon Sep 17 00:00:00 2001 From: giaogiao Date: Fri, 8 Mar 2024 17:50:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=A4=A7=E6=A6=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common.php | 16 +++++++++ app/controller/Index.php | 67 ++++++++++++++++++++++++++++++++++++++ app/controller/JobTest.php | 2 +- app/job/Hello.php | 13 ++++++-- composer.json | 3 +- composer.lock | 2 +- 6 files changed, 97 insertions(+), 6 deletions(-) diff --git a/app/common.php b/app/common.php index 57cb6d8..d1420c4 100644 --- a/app/common.php +++ b/app/common.php @@ -1,2 +1,18 @@ $status, + "message" => $message, + "result" => $data + ]; + return json($result, $httpStatus); +} diff --git a/app/controller/Index.php b/app/controller/Index.php index 48be096..925e6f8 100644 --- a/app/controller/Index.php +++ b/app/controller/Index.php @@ -1,7 +1,10 @@ addDays(21); + $data = ['date' => $trialExpires->toDateString(), 'tip' => ' ']; + // $result = Db::table("demo")->where("id",1)->find(); + // dump($result); + Db::table('time_log')->insert($data); + return show(200, 'ok', ['time' => $trialExpires]); + } + /** + * 每天访问一次的任务 + */ + public function scheduled_tasks($name = 'ThinkPHP6') + { + $db_time=Db::table('time_log')->where('id', Db::table('time_log')->max('id'))->find();; + //dump($db_time); + $time =$db_time['date']; + // 获取当前时间 + $current = Carbon::now(); + for ($i=0; $i < 8; $i++) { + $current->subDay($i); + if(strcmp($current->toDateString(),$time)==0){ + + echo "to do "; + //$this->updated(""); + $i==7?$this->updated(""):''; + return show(200, 'ok', ['time' => $current->toDateString(),'time2'=>$time]); + } + } + } + /** + * 更新时间 + */ + public function send_msg($title,$body) + { + // 添加 30 天到当前时间 + // 1.当前任务将由哪个类来负责处理。 + // 当轮到该任务时,系统将生成一个该类的实例,并调用其 fire 方法 + $jobHandlerClassName = 'app\job\Hello'; + + // 2.当前任务归属的队列名称,如果为新队列,会自动创建 + $jobQueueName = "helloJobQueue"; + + // 3.当前任务所需的业务数据 . 不能为 resource 类型,其他类型最终将转化为json形式的字符串 + // ( jobData 为对象时,存储其public属性的键值对 ) + $jobData = ['ts' => time(), 'bizId' => uniqid(), 'a' => 1,'title'=>$title,'body'=>$body]; + + // 4.将该任务推送到消息队列,等待对应的消费者去执行 + $isPushed = Queue::push($jobHandlerClassName, $jobData, $jobQueueName); + + // database 驱动时,返回值为 1|false ; redis 驱动时,返回值为 随机字符串|false + if ($isPushed !== false) { + Log::info(date('Y-m-d H:i:s')."消息已经推送"); + } else { + Log::error(date('Y-m-d H:i:s')."消息推送失败请检查队列驱动"); + } + } } diff --git a/app/controller/JobTest.php b/app/controller/JobTest.php index 0a12570..c30e22b 100644 --- a/app/controller/JobTest.php +++ b/app/controller/JobTest.php @@ -23,7 +23,7 @@ namespace app\controller; // 3.当前任务所需的业务数据 . 不能为 resource 类型,其他类型最终将转化为json形式的字符串 // ( jobData 为对象时,存储其public属性的键值对 ) - $jobData = [ 'ts' => time(), 'bizId' => uniqid() , 'a' => 1 ] ; + $jobData = ['ts' => time(), 'bizId' => uniqid(), 'a' => 1,'title'=>"周雪莲子",'body'=>'女神节快乐']; // 4.将该任务推送到消息队列,等待对应的消费者去执行 $isPushed = Queue::push( $jobHandlerClassName , $jobData , $jobQueueName ); diff --git a/app/job/Hello.php b/app/job/Hello.php index 99966e2..0b04dbf 100644 --- a/app/job/Hello.php +++ b/app/job/Hello.php @@ -64,9 +64,16 @@ class Hello private function doHelloJob($data) { $curl = new Curl(); - $title=""; - $mssg=""; - $curl->get('https://api.day.app/3vrp4DbTGmSWxVbzHnUnPB/'.$title.$mssg); + $title=$data['title']; + $mssg=$data['body']; + $curl->get('https://api.day.app/3vrp4DbTGmSWxVbzHnUnPB/'.$title.'/'.$mssg); + if ($curl->error) { + echo 'Error: ' . $curl->errorMessage . "\n"; + $curl->diagnose(); + } else { + echo 'Response:' . "\n"; + var_dump($curl->response); + } print("Hello Job Started. job Data is: " . var_export($data, true) . " \n"); print("Hello Job is Fired at " . date('Y-m-d H:i:s') . " \n"); print("Hello Job is Done!" . " \n"); diff --git a/composer.json b/composer.json index deb1732..d06e2b2 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,8 @@ "topthink/think-orm": "^2.0", "topthink/think-filesystem": "^1.0", "topthink/think-queue": "^3.0", - "php-curl-class/php-curl-class": "^9.19" + "php-curl-class/php-curl-class": "^9.19", + "nesbot/carbon": "^2.72" }, "require-dev": { "symfony/var-dumper": "^4.2", diff --git a/composer.lock b/composer.lock index cf8fec4..987ac31 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "6f1dedd32ad3424b3151c92ff5c86d89", + "content-hash": "dea40365caf14bbc8f865784ad92e670", "packages": [ { "name": "carbonphp/carbon-doctrine-types",