coralme / phal-cron
PhalApi框架定时任务组件
v1.0.0
2024-05-24 07:30 UTC
Requires
- php: >=7.4
Requires (Dev)
- phpunit/phpunit: ^9.6
README
PhalApi框架配置定时任务相对复杂,本项目是对Phalapi框架定时任务功能的补充,通过非常简单的三个步骤配置,即可实现其他框架的定时任务功能。
接入步骤1:
.
├── ReadMe.md
├── bin
│ └── phalapi-cron
#!/usr/bin/env php <?php require_once dirname(__FILE__) . '/../public/init.php'; use Coralme\PhalCron\Schedule; class Cron extends Schedule { public function schedule(Schedule $schedule) { #示例: $schedule->add('*/5 * * * *', 'Admin.DelAccount.autoDelAccount')->outPutLog('/var/www/html/runtime/autoDelAccount.log'); $schedule->add('*/3 * * * *', 'Admin.DelAccount.autoDelAccount')->outPutLog('/var/www/html/runtime/autoDelAccount2.log'); $schedule->add('0 10 * * *', 'Admin.DelAccount.autoDelAccount')->outPutLog('/var/www/html/runtime/autoDelAccount3.log'); } } $cron = new Cron(); $cron->run();
接入步骤2:
我们只需在 Shedule
方法中按照示例添加自己的定时任务即可
add
方法的第一个参数是cron表达式(分 时 日 月 周),第二个参数是具体的任务执行逻辑,与phalapi-cli
的表示习惯一致。
outPutLog
自定义日志输出的位置,不使用该方法则不记录日志
接入步骤3:
需要确保phalapi-cron
和phalapi-cli
两个文件都是可执行文件。需要注意的是,文件的读写属性可能无法记录到git仓库,导致服务器拉取后文件没有可执行属性。在提交前需要进行属性重写
git update-index --chmod=+x .\bin\cron
git update-index --chmod=+x .\bin\cli
部署后,在服务器的crontab中添加一条分钟定时任务,每分钟就会检测Schedule
中是否有需要执行的任务,如果有,就会自动执行:
# 每分钟执行定时任务
* * * * * php /var/www/html/bin/phalapi-cron