一、定时任务代码:
- 创建命令(php artisan make:command SendEmails) 会在app/Console/Commands 目录中创建一个新的命令类
- 编写调用名称,命名描述,执行命令在handle()中编写逻辑代码
-
在 app/Console/Kernel.php 文件中的 schedul() 中 调用并定时执行
二、定时任务服务器
直接运行定时任务是 php artisan XXXXXXX(定时任务名称)
查看命令是crontab -l -u www
编辑命令是crontab -e -u www
查看文件权限是 ls -l
需要特别注意,/home目录下需要www用户目录,项目的文件归属需要是www用户
git pull完成后需要执行chown -R www.www * 赋予权限
用www用户设置cron,需要在home目录下添加www目录 mkdir www
参考案例:
https://learnku.com/docs/laravel/7.x/artisan/7480
https://learnku.com/docs/laravel/7.x/scheduling/7492#scheduling-artisan-comma
三、队列代码
- 在 app/Console/Kernel.php 文件中的 schedul() 中 调用并定时执行
- 在定时任务中调用模型方法
- 命令创建队列功能,在模型中调用实例化队列, 定义队列名传送
- 在队列方法中编写写操作功能
四、队列服务器操作
- 下载安装supervisor 命令:sudo pip install supervisor
- 编写/etc/supervisord.conf 文件
命令: echo_supervisord_conf > /etc/supervisord.conf
案例改最后两行:[include] files = /etc/supervisor/*.conf - 命令:mkdir /etc/supervisor
- 新增一个 Supervisor 配置文件
- 注意:日志地址,queue名称,redis 等配置
-
重载 supervisorctl reload
-
查看运行 ps -ef|grep 'queue:work'
-
归属需要是www用户 chown -R www.www *
-
直接运行定时任务是 php artisan XXXXXXX(定时任务名称)
参考案例:
使用 Supervisor 管理 Laravel 队列进程 | Laravel | Laravel China 社区