ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
- 1.写一个要执行的时间woker,继承TimeWorkerBase ~~~ <?php /** * @Author: 陈静 * @Date: 2018/05/08 20:24:43 * @Description: */ namespace app\worker\controller; use app\index\controller\Send; use think\Log; class TestTimeWorker extends TimeWorkerBase { //这是继承过后要执行的方法 /** * @Author: 陈静 * @Date: 2018/05/09 22:27:48 * @Description: * @param $data 要执行的数据 */ public function runHandle($data) { Log::notice( "测试".date('Y-m-d H:i:s') ); //发送短信方法 (new Send())->sendMsg(); } } ~~~ - 2. 写一个命令循环执行 ~~~ <?php /** * @Author: 陈静 * @Date: 2018/04/18 22:13:03 * @Description: */ namespace app\console; use app\base\controller\Redis; use app\worker\controller\WorkerCommandBase; use think\Config; use think\console\Input; use think\console\Output; use think\Exception; use think\Log; /** * Created by PhpStorm. * Power by Mikkle * QQ:776329498 * Date: 2017/6/12 * Time: 15:07 */ class Task extends WorkerCommandBase { protected function configure() { $this->setName('task:queue')->setDescription('Here is the task\'s command '); } } ~~~ - 3. 一个Test调用添加执行的值 ~~~ <?php /** * @Author: 陈静 * @Date: 2018/05/08 23:06:08 * @Description: */ namespace app\worker\controller; class Test { public function addtest() { TestTimeWorker::add(['name' => 'test1'],strtotime('2018-05-09 22:55:00')); } } ~~~ - 执行顺序 1.开启命令 `php think task:queue` ![](https://box.kancloud.cn/c813b2f6ca85f2ec358cdb38e0699892_538x178.png) 2.执行添加数据 直接浏览器运行 ,或者你自己的方式添加 ![](https://box.kancloud.cn/4cab8ef733612172f02232837346cca6_629x128.png) 3.等待执行 到了指定时间就执行了 ![](https://box.kancloud.cn/47679b1fd058105bc7e35bfd4561cf92_489x468.png)