mstrychalski/supervisor
Composer 安装命令:
composer require mstrychalski/supervisor
包简介
:vertical_traffic_light: Process supervisor for PHP.
README 文档
README
This library implements CLI process supervisors and aggregate supervisor supervisors in an attempt to limit damage done by failing scripts.
It can be installed in whichever way you prefer, but we recommend [Composer][packagist].
$ composer require graze/supervisor
Documentation
<?php use Graze\Supervisor\ProcessSupervisor; use Symfony\Component\Process\Process; $while = new Process('/usr/bin/python while_true.py'); $sup = new ProcessSupervisor($while); $sup->start(); $sup->supervise(0.001); // Check the "while" process every 0.001s (blocking)
Assuming everything went well with the child process, the supervise method
will stop watching the process and you can continue on your business. But what
happens to processes that fall flat on their face?
Uncaught exception 'Graze\Supervisor\Exception\TerminatedProcessException' with message
The process was unexpectedly terminated
[process] /usr/bin/python while_true.py
[code] 143
[text] Termination (request to terminate)
[stderr] Terminated
[stdout]
Now we've gone from one script failing to two scripts failing, but how does that help? Well, it doesn't, but that's where handlers come in.
Handlers
Handlers help you control what to do when a child process fails (and when they successfully terminate). You can do anything you like with the handlers:
- Retry the process
- Alert your developers or infrastructure team
- Requeue the job in some fancy queuing service
- Alert your error logging service
- Start a different script
- Stop other related scripts
- Simply throw an exception
<?php use Graze\Supervisor\Handler\RetryHandler; use Graze\Supervisor\ProcessSupervisor; use Symfony\Component\Process\Process; $while = new Process('/usr/bin/python while_true.py'); $retry = new RetryHandler(3); $sup = new ProcessSupervisor($while, $retry); $sup->start(); $sup->supervise();
Now if your process dies, the retry handler will restart it up to a maximim of 3 times. We can do even better than this though; suppose you want to retry 3 times then alert developers and log the error, all before throwing an exception. Just decorate!
<?php use Graze\Supervisor\Handler\ExceptionHandler; use Graze\Supervisor\Handler\RetryHandler; use Graze\Supervisor\Handler\UnexpectedTerminationHandler; use Graze\Supervisor\ProcessSupervisor; use Symfony\Component\Process\Process; $while = new Process('/usr/bin/python while_true.py'); $handler = new RetryHandler(3, new MyPagerDutyHandler($pagerduty, new MyBugSnagErrorHandler($bugsnag, new ExceptionHandler( new UnexpectedTerminationHandler() ) ) ) ); $sup = new ProcessSupervisor($while, $handler); $sup->start(); $sup->supervise();
This library currently only comes bundled with a few handlers, but by implementing a simple interface you can quickly use your own. Additional core handlers are always welcome!
Supervising the supervisors
So having a supervisor to watch your process is great, but what if you want to supervise multiple processes that are logically linked (ie. batch processing)? You can achieve this by supervising your individual process supervisors.
<?php use Graze\Supervisor\ProcessSupervisor; use Graze\Supervisor\SupervisorSupervisor; use Symfony\Component\Process\Process; $batchA = new ProcessSupervisor(Process('/usr/bin/python batch.py --id=a')); $batchB = new ProcessSupervisor(Process('/usr/bin/python batch.py --id=b')); $batchC = new ProcessSupervisor(Process('/usr/bin/python batch.py --id=c')); $sup = new SupervisorSupervisor([$batchA, $batchB, $batchC]); $sup->start(); $sup->supervise();
Just like the process supervisors, the supervisor supervisors handle successful and unsuccessful termination with handlers.
<?php use Graze\Supervisor\Handler\ExceptionHandler; use Graze\Supervisor\Handler\RetryHandler; use Graze\Supervisor\Handler\UnexpectedTerminationHandler; use Graze\Supervisor\ProcessSupervisor; use Graze\Supervisor\SupervisorSupervisor; use Symfony\Component\Process\Process; $batchA = new ProcessSupervisor(Process('/usr/bin/python batch.py --id=a')); $batchB = new ProcessSupervisor(Process('/usr/bin/python batch.py --id=b')); $batchC = new ProcessSupervisor(Process('/usr/bin/python batch.py --id=c')); // Retry all supervised processes if one fails (max 3 times) $handler = new RetryHandler(3, new ExceptionHandler( new UnexpectedTerminationHandler() ) ); $sup = new SupervisorSupervisor([$batchA, $batchB, $batchC], $handler); $sup->start(); $sup->supervise();
Who supervises the supervisor supervisor?
Depending on the complexity of logically grouped processes, you may need to have many tiers of failure management. This is entirely possible by passing supervisor supervisors into a parent supervisor supervisor. In fact, you can even mix the types of supervisors you supervise!
<?php use Graze\Supervisor\Handler\ExceptionHandler; use Graze\Supervisor\Handler\RetryHandler; use Graze\Supervisor\Handler\UnexpectedTerminationHandler; use Graze\Supervisor\ProcessSupervisor; use Graze\Supervisor\SupervisorSupervisor; use Symfony\Component\Process\Process; $batchA = new ProcessSupervisor(Process('/usr/bin/python batch.py --id=a --half=a')); $batchB = new ProcessSupervisor(Process('/usr/bin/python batch.py --id=b --half=a')); $batchC = new ProcessSupervisor(Process('/usr/bin/python batch.py --id=c --half=a')); $halfA = new SupervisorSupervisor([$batchA, $batchB, $batchC]); $batchD = new ProcessSupervisor(Process('/usr/bin/python batch.py --id=d --half=b')); $batchE = new ProcessSupervisor(Process('/usr/bin/python batch.py --id=e --half=b')); $batchF = new ProcessSupervisor(Process('/usr/bin/python batch.py --id=f --half=b')); $halfB = new SupervisorSupervisor([$batchD, $batchE, $batchF]); $daemon = new ProcessSupervisor(Process('/usr/bin/php daemon.php'), new RetryHandler(1, new ExceptionHandler())); $handler = new RetryHandler(3, new ExceptionHandler( new UnexpectedTerminationHandler() ) ); $sup = new SupervisorSupervisor([$halfA, $halfB, $daemon], $handler); $sup->start(); $sup->supervise();
You can see how things could get crazy pretty quickly. This library, however, is by no means a replacement for true system daemon management. You should use something like systemd or upstart for that.
Contributing
We accept contributions to the source via Pull Request, but passing unit tests must be included before it will be considered for merge.
$ composer install
$ composer test
If you've found a bug, please include a failing test when you create an issue.
License
The content of this library is released under the MIT License by Nature Delivered Ltd..
You can find a copy of this license in LICENSE or at http://opensource.org/licenses/mit.
mstrychalski/supervisor 适用场景与选型建议
mstrychalski/supervisor 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 07 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「process」 「supervisor」 「daemon」 「processing」 「multi-process」 「child」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mstrychalski/supervisor 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mstrychalski/supervisor 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mstrychalski/supervisor 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony 3/4/5 bundle which allows you to use @Supervisor annotations to configure how Supervisor runs your console commands.
Doctrine implementation of the MetaborStd (Statemachine) for PHP 8.2+
Shell command module for PHP.
A fluent PHP library for supervised master-child process control using pcntl and pipes
Swoole server process management
Library that let you catch fatal errors
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-07-03