setono/cron-builder
Composer 安装命令:
composer require setono/cron-builder
包简介
Generate a crontab from a config directory
README 文档
README
Build your crontab from configuration files in your repository. The configuration files are written in PHP and can be placed anywhere in your repository.
Installation
composer require setono/cron-builder
Usage
The following two code snippets outline the simplest usage of the cron builder.
1. Add your commands
<?php # etc/cronjobs/jobs.php declare(strict_types=1); use Setono\CronBuilder\Context; use Setono\CronBuilder\CronJob; return static function (Context $context): iterable { yield new CronJob('0 0 * * *', '/usr/bin/php {{ release_path }}/send-report.php {{ args|join(" ") }}', 'Run every day at midnight'); if ($context->get('env') === 'prod') { yield new CronJob('0 0 * * *', '/usr/bin/php {{ release_path }}/process.php {{ args|join(" ") }}'); } };
Notice the {{ release_path }} and {{ args|join(" ") }} placeholders.
These are placeholders that will be replaced with the values from the context array.
This is done using the Twig templating engine, which means
you can use all the features of Twig in your cronjob definitions.
If the placeholders contain any character(s) that will be interpreted by Twig (e.g. /), you can use the
context Twig function like so:
yield new CronJob('0 0 * * *', '{{ context("bin/php") }} {{ release_path }}/process.php');
2. Build the crontab
When your cronjobs are defined, you can output the crontab file:
<?php use Setono\CronBuilder\CronBuilder; use Symfony\Component\Finder\Finder; echo (new CronBuilder(__DIR__ . '/etc/cronjobs', [ 'release_path' => '/home/johndoe/public_html', 'env' => 'prod', 'args' => ['--verbose'], ]))->build();
This will output the following:
###> Automatically generated by Setono Cron Builder - DO NOT EDIT ###
0 0 * * * /usr/bin/php /home/johndoe/public_html/send-report.php --verbose # Run every day at midnight
0 0 * * * /usr/bin/php /home/johndoe/public_html/process.php --verbose
###< Automatically generated by Setono Cron Builder - DO NOT EDIT ###
You can save this in a file named crontab.txt and add it to your crontab like this:
cat crontab.txt | crontab -
setono/cron-builder 适用场景与选型建议
setono/cron-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.07k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 09 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 setono/cron-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 setono/cron-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 26.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 2
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-03