rogerthomas84 / daemoniser
Daemoniser 是一个用于帮助创建守护进程应用程序的 PHP 库
1.1.5
2019-12-10 12:24 UTC
Requires
- php: >=5.0
- ext-pcntl: *
- ext-posix: *
README
__ \ _)
| | _` | _ \ __ `__ \ _ \ __ \ | __| _ \ __|
| | ( | __/ | | | ( | | | |\__ \ __/ |
____/ \__,_|\___|_| _| _|\___/ _| _|_|____/\___|_|
这是什么
Daemoniser 是一个 PHP 库,用于减轻创建基于 PHP 的守护进程的压力。如果您对此有疑问,那么这个库可能不适合您。通常情况下,PHP 不应该用于创建守护进程。
与 Composer 一起使用
$ composer require rogerthomas84/daemoniser
如何使用
有关编写第一个守护进程的帮助,请参阅 example/example.php
中的示例。
守护进程需要一个 DaemonConfig
实例通过 execute
方法传递。
DaemonConfig 构造函数
DaemonAbstract $daemon
/ 您的守护进程实例(它扩展了DaemonAbstract
)- 必需
$errorLogFilePath
/ 用于记录错误的日志文件的完整路径。- 可选,默认为
tmp/log/error_My_Class_Name.log
(假设完全限定的类名为\My\Class\Name
)
- 可选,默认为
$infoLogFilePath
/ 用于记录 echo'd 内容的日志文件的完整路径。- 可选,默认为
tmp/log/info_My_Class_Name.log
(假设完全限定的类名为\My\Class\Name
)
- 可选,默认为
$pidFilePath
/ 用于存储守护进程实例的 pid 的文件的完整路径。- 可选,默认为
tmp/pid/My_Class_Name.pid
(假设完全限定的类名为\My\Class\Name
)
- 可选,默认为
$softStopFilePath
/ 软停止文件的路径。- 可选,默认为
tmp/stops/stop_My_Class_Name.log
(假设完全限定的类名为\My\Class\Name
)
- 可选,默认为
$sleepDuration
/ 在每次调用您的run()
方法后休眠多长时间。- 可选,默认为
5
秒
- 可选,默认为
$maxLogFileSize
/ 在允许日志文件变得多大之前(以字节为单位)。- 可选,默认为
100000000
字节(100 MB)
- 可选,默认为
$iniSettings
/ 要与ini_set
一起使用的设置键 => 值数组。例如,['memory_limit' => '1024MB']
- 可选,默认为空数组
$whitelistedUsers
/ 包含允许执行此命令的用户名的数组。- 可选,默认为空数组,允许任何用户。
所有配置选项也都在配置对象中的 set
和 get
方法中可用。
用法
Daemoniser 有几个有用的命令。
php my-daemon.php status
- 获取守护进程的状态php my-daemon.php start
- 启动守护进程php my-daemon.php soft-stop
- 通过停止文件优雅地停止守护进程。php my-daemon.php stop
- 立即停止守护进程(这不是最佳做法)php my-daemon.php restart
- 重新启动守护进程(这不是最佳做法)php my-daemon.php pid
- 获取守护进程的 PIDphp my-daemon.php rm-logs
- 删除此守护进程的历史记录。php my-daemon.php rm-logs all
- 删除此守护进程的所有日志。php my-daemon.php help
- 显示帮助内容
您绝对不应该使用 stop
。这样做会导致进程立即被杀死。但是,如果您没有运行可能导致数据丢失的进程,这就不会成为大问题。
如果您“必须”立即停止,您必须在您的守护进程实现canImmediatelyStop()
方法并返回true
。这应不惜一切代价避免,因为停止过程只是立即终止pid
。
扩展命令
您可以通过实现protected function getAdditionalCommands()
方法轻松地扩展命令以执行run()
循环之外的不同操作。
您需要返回一个对象数组。每个对象都是一个DaemonCommand
的实例。在DaemonCommand
上有一个有用的::build()
方法,您可以使用它轻松地构建新功能。
我应该使用这个吗?
如果您在问自己这个问题,那么答案是:不,您不应该使用。这个库满足非常具体的要求。
完整示例文件
<?php chdir(dirname(__FILE__)); error_reporting(E_ALL | E_STRICT); require_once '../vendor/autoload.php'; use Daemoniser\DaemonAbstract; use Daemoniser\DaemonCommand; use Daemoniser\DaemonConfig; use Daemoniser\DaemonException; /** * Class ExampleOneDaemon */ class ExampleOneDaemon extends DaemonAbstract { public function run() { $this->logInfo((new DateTime())->format('Y-m-d H:i:s')); $this->logError('This logs an error This logs an error This logs an error This logs an error This logs an error This logs an error '); } /** * @return string */ public function randomAnimal() { $animals = ['Cat', 'Dog', 'Giraffe']; $this->echoLine($animals[array_rand($animals)]); } /** * @return DaemonCommand[] * @throws DaemonException */ protected function getAdditionalCommands() { return [ DaemonCommand::build('animal', 'Get a random animal.', 'randomAnimal') ]; } /** * @return bool */ protected function canImmediatelyStop() { return true; // Can we just kill the process? } } try { $daemon = new ExampleOneDaemon(); $config = new DaemonConfig( $daemon, '/path/to/error.log', '/path/to/info.log', '/path/to/mypid.pid', '/path/to/stops/filename.stop', 10, DaemonConfig::ONE_HUNDRED_MB, [ 'memory_limit' => '512MB', 'display_startup_errors' => 1, 'display_errors' => 1, ], [ 'www-data' ] ); $config->setMaxLogFileSize(1000); /** * Alternatively you can elect to set them via set methods. If you chose this route, you only need to explicitely * pass an instance of the daemon. */ // $config = new DaemonConfig($daemon); // $config->setErrorLogFilePath('/path/to/error.log'); // $config->setInfoLogFilePath('/path/to/info.log'); // $config->setPidFilePath('/path/to/pid/filename.pid'); // $config->setSoftStopFilePath('/path/to/stops/filename.stop'); // $config->setSleepBetweenRuns(10); // Sleep for 10 seconds between calls to `run()` // $config->setMaxLogFileSize(100000000); // Set the max log file size to 2,000,000 bytes before being rotated // $config->setIniSettings( // [ // 'memory_limit' => '512MB', // 'display_startup_errors' => 1, // 'display_errors' => 1, // ] // ); // $config->setWhitelistedUsers( // [ // 'www-data' // ] // ); $daemon->execute( $config, $argv ); } catch (DaemonException $e) { echo ' Exception executing command:' . PHP_EOL; echo sprintf(' Message: %s', $e->getMessage()) . PHP_EOL; echo sprintf(' File: %s', $e->getFile()) . PHP_EOL; echo sprintf(' Line: %s', $e->getLine()) . PHP_EOL; exit(1); }