eclipxe/php-soft-daemon
Composer 安装命令:
composer require eclipxe/php-soft-daemon
包简介
PHP Library to wait for system signals and running code
README 文档
README
PHP SoftDaemon Library
SoftDaemon provides a library to run continuously some code.
I create this library to execute procedures continuously and to manipulate the time between iterations. I also use it to send signals to the processes in order to manipulate the behavior of the execution.
Do not reinvent the wheel, if cron jobs are suitable to you then use them.
How it runs
You have to create an instance of SoftDaemon, it requires minimum an Executable object that implements SoftDaemon\Executable interface.
The SoftDaemon\Executable interface requires that you create two methods:
- Will call
signalHandler(int $signo): voidto optionally do something with the signal. - Will call
runOnce(): boolon each iteration.
For example, you can use signalHandler($signo) to process SIGHUP.
Once you have instantiated a SoftDaemon object you can call the method run().
This method will enter into a loop and run Executable::runOnce() on every iteration.
At the end on every iteration the loop will wait.
Signals used
These signals are catched by SoftDaemon. All signals pass to Executable::signalHandler($signo) before SoftDaemon do its own processing.
- SIGHUP: Reset the error counter to zero. Method:
SoftDaemon::resetErrorCounter(). - SIGUSR1: Pause iterations. Method:
SoftDaemon::setPause(true). - SIGUSR2: Unpause iterations. Method:
SoftDaemon::setPause(false). - SIGTERM, SIGINT, SIGQUIT: Terminate the iterations. Method:
SoftDaemon::terminate().
How SoftDaemon knows how many seconds will wait
The pause state determines the quantity of seconds to wait for signals and continue to the next iteration.
If the SoftDaemon is on pause then it will not call runOnce, it will only try to wait 1 second.
If not on pause then it will use the counter of errors and request the sequencer to determine the number of seconds to wait.
The result of Executable::runOnce(): bool determines the number of errors. It is reset to zero when runOnce() returns true. It is increased by 1 when runOnce() returns false.
Anyhow, the number of seconds will be bounded to minWait and maxWait properties.
About sequencers
A sequencer is an object that implements SoftDaemon\Sequencer. Its purpose is to receive the number of errors and return a quantity of seconds to wait. There are some predefined Sequencers already defined in the namespace SoftDaemon\Sequencers:
- Fixed: It always returns the same quantity of seconds.
- Linear: It returns the quantity of seconds as the count of errors
(0 -> 0, 1 -> 1, 2 -> 2, ...). - Exponential: It returns the quantity of seconds as the count of errors to an exponential minus 1, if the base is 2 then it will return the following numbers:
(0 -> 0, 1 -> 1, 2 -> 3, 3 -> 7, 4 -> 15, ...).
You can create a sequencer with your own rules. The minWait and maxWait limit the boundaries of seconds returned by the sequencer.
PHP Support
This library is compatible with at least the oldest PHP Supported Version with active support. Please, try to use PHP full potential.
We adhere to Semantic Versioning. We will not introduce any compatibility backwards change on major versions.
Internal classes (using @internal annotation) are not part of this agreement
as they must only exist inside this project. Do not use them in your project.
Contributing
Contributions are welcome! Please read CONTRIBUTING for details and don't forget to take a look the TODO and CHANGELOG files.
Copyright and License
The eclipxe/php-soft-daemon library is copyright © Carlos C Soto
and licensed for use under the MIT License (MIT). Please see LICENSE for more information.
eclipxe/php-soft-daemon 适用场景与选型建议
eclipxe/php-soft-daemon 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 43 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 08 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 eclipxe/php-soft-daemon 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 eclipxe/php-soft-daemon 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 43
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-08-20