mluex/endless-cron-command
Composer 安装命令:
composer require mluex/endless-cron-command
包简介
Extension of wrep/daemonizable-command to easily run endless Symfony commands as cronjobs.
README 文档
README
This package is a small extension of wrep/daemonizable-command. If no Upstart or systemd are available to set up a daemon, a simple cronjob and Symfony Lock are sufficient to produce an almost similar result.
Detailed information on Daemonizable / Endless Commands
Check out the documentation of wrep/daemonizable-command for more information on how to create endless commands: wrep/daemonizable-command
Installation
composer require mluex/endless-cron-command
Which version to use?
- Version 5.* for Symfony 7 and higher
- Version 4.* for Symfony 6
Usage
1. Configuration
EndlessCronCommands require symfony/lock to ensure that only one instance is executed at a time. Please configure the bundle accordingly in your .env.local, e.g.
###> symfony/lock ### LOCK_STORE_DSN=mysql:host=127.0.0.1;dbname=app ###< symfony/lock ###
⚠️ Make sure to use a lock store that supports expiry! Check the Symfony Lock documentation for more information.
2. Implementation
Create a Symfony command that extends EndlessCronCommand and off you go. Here is a minimal example:
namespace Acme\DemoBundle\Command; use Mluex\EndlessCronCommand\EndlessCronCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class MinimalDemoCommand extends EndlessCronCommand { // This is just a normal Command::configure() method protected function configure(): void { $this->setName('acme:minimaldemo') ->setDescription('An EndlessCronCommand implementation example'); } // Method will be called in an endless loop protected function execute(InputInterface $input, OutputInterface $output): int { // Do some work file_put_contents('/tmp/acme-timestamp.txt', time()); } }
If your command includes Monolog's FingersCrossed Log Handlers, you may want to clear them after every iteration as wrep pointed out in his documentation. EndlessCronCommand provides a method for this:
namespace Acme\DemoBundle\Command; use Mluex\EndlessCronCommand\EndlessCronCommand; use Psr\Log\LoggerInterface; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class MinimalDemoCommand extends EndlessCronCommand { public function __construct( private readonly LoggerInterface $logger, string $name = null ) { parent::__construct($name); } // This is just a normal Command::configure() method protected function configure(): void { $this->setName('acme:minimaldemo') ->setDescription('An EndlessCronCommand implementation example'); } // Method will be called in an endless loop protected function execute(InputInterface $input, OutputInterface $output): int { // Do some work file_put_contents('/tmp/acme-timestamp.txt', time()); // Clear FingersCrossed Log Handlers $this->clearFingersCrossedLogHandlers($this->logger); } }
3. Cronjob setup
Add a cronjob to your crontab to run the command every minute.
* * * * * php /path/to/your/project/bin/console acme:minimaldemo >/dev/null 2>&1
4. Advanced usage
It may make sense to limit the runtime of an instance and restart the command regularly. Use the runtime option for this:
* * * * * php /path/to/your/project/bin/console acme:minimaldemo --runtime=600 >/dev/null 2>&1
In order to run the command's execute() method more often in the main loop (default is 5 sec), you can use the frequency option to set a lower timeout between iterations (1 sec in this example):
* * * * * php /path/to/your/project/bin/console acme:minimaldemo --frequency=1 >/dev/null 2>&1
Every iteration will refresh the lock and push its expiry time further into the future. If the command crashes, the lock will expire after the lock-ttl time and the next cronjob will start a new instance. You can set the lock-ttl to a lower value to ensure that the command is restarted more quickly after a crash:
* * * * * php /path/to/your/project/bin/console acme:minimaldemo --lock-ttl=60 >/dev/null 2>&1
TODO
- Examples
- Unit Tests
mluex/endless-cron-command 适用场景与选型建议
mluex/endless-cron-command 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 58 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 10 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「cron」 「daemon」 「background process」 「long running process」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mluex/endless-cron-command 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mluex/endless-cron-command 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mluex/endless-cron-command 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony bundle to monitor and execute commands
The bundle for easy using json-rpc api on your project
Swoole server process management
Scheduling extension for Yii2 framework
base daemon
Monitoring for scheduled jobs
统计信息
- 总下载量: 58
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 31
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-13