facile-it/terminable-loop-command
Composer 安装命令:
composer require facile-it/terminable-loop-command
包简介
A Shell+PHP wrapper to run Symfony console commands in loop under a daemon or Kubernetes
README 文档
README
A Shell+PHP combination to run Symfony console commands in loop under a daemon or Kubernetes, instead of using a long running process.
This package contains a shell script and an abstract Symfony Console Command class; you need to write your command extending that class, and launch it through the shell script. Ideally, the script has to be used as a container entry point, and/or launched with a supervisor, like Docker Compose, Kubernetes, supervisord.
Installation
composer require facile-it/terminable-loop-command
Usage
Launch the shell script appending the desired PHP script to be executed in a loop:
vendor/bin/terminable-loop-command.sh my_custom_command.php
... where my_custom_command.php launches your command class, which must extend AbstractTerminableCommand (see the test stub in this repo)
Example with a command in a common Symfony app
When using it inside a Symfony application, do not forget to call bin/console as first argument:
vendor/bin/terminable-loop-command.sh bin/console my:command --optionA
... where the command is something like this:
<?php namespace Acme\Command; use Facile\TerminableLoop\AbstractTerminableCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class MyCommand extends AbstractTerminableCommand { public function __construct() { parent::__construct('my:command'); } protected function commandBody(InputInterface $input, OutputInterface $output): int { $this->setSleepDuration(60); // do something if (! $this->shouldSleep()) { // you can customize sleep duration during execution, even conditionally $this->setSleepDuration(0); } return 0; } }
Why?
When running a PHP application, you may encounter the need of a background command that runs continuously. You can try to write it as a long running process, but it can be prone to memory leaks and other issues.
With this small Shell+PHP combination, you can have a simple loop that:
- starts the command
- does something
- sleeps for a custom amount of time
- shuts down and restarts back again
The shell script intercepts SIGTERM/SIGINT signals so, when they are received, the PHP script is stopped ASAP but gracefully, since the execution of the body of the command is never truncated.
This means that you can easily obtain a daemon PHP script without running in memory issues; if you run this in a Kubernetes environment this will be very powerful, since the orchestrator will take care of running the script, and at the same time it will apply the proper restart policies in case of crashes. Last but not least, the signal handling will play nice with shutdown requests, like during the roll out of a new deployment.
How it works
The shell script is pretty basic, calling the desired command in a loop, until it exits with an exit code different than 0; it also traps SIGTERM/SIGKILL signals and forwards them to the PHP process.
The PHP Command is designed to first execute a main task (the AbstractTerminableCommand::commandBody() function) and afterwards sleep for a custom amount of time, which can be customized at any time during the command execution; this is powerful since you can let your command logic decide how much to wait between two command executions, even nothing.
The PHP class also gracefully handles the signals, which means that if the signal is received during the commandBody() function, it will wait for its conclusion; if it's received during the sleep phase, it will terminate it immediately. In case of termination due to signal, the command exits with and exit code of 143, which means exactly that we're exiting due to the signal: this will interrupt the shell script loop execution, without being considered as an error from the point of view of the supervising agent, like Kubernetes.
facile-it/terminable-loop-command 适用场景与选型建议
facile-it/terminable-loop-command 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 138.47k 次下载、GitHub Stars 达 27, 最近一次更新时间为 2019 年 04 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 facile-it/terminable-loop-command 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 facile-it/terminable-loop-command 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 138.47k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 27
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-04-05