adrian0350 / php-daemon
使用PHP将工作者带到您的Web项目中!
1.3.4
2018-02-09 17:26 UTC
Requires
- php: >=5.3.0
README
使用PHPDaemon,您将能够在任何需要运行外部脚本的情况下运行简单的单次守护进程,这意味着您需要在Web项目中添加助手或其他。
该类通过将pid存储在您的脚本目录内作为.script_file.pid
(以点开始的文件默认为隐藏)来实现。
脚本输出(记录)将设置在$options
中,键为(布尔值) log
和(字符串) log_path
。
如果log
设置为true且未提供或提供了无效的log_path
,它将在脚本目录中记录。
如果log
设置为true且提供了有效的log_path
,它将记录在给定的log_path中。
日志的名称将是您的script_file
名称,以.log
结尾.
示例:对于pid
/your/daemon/path/to/script_file.php
,将是/your/daemon/path/to/.script_file.php.pid
,而对于没有log_path的log
/your/daemon/path/to/script_file.php
,将是/your/daemon/path/to/script_file.php.log
。
PHP版本
由于使用了命名空间和PHP_BINARY常量,此类与PHP 5.4及以上
兼容。
安装
将此库添加到您的composer配置。在composer.json中
"require": { "adrian0350/php-daemon": "1.*" }
或者
如果您使用bash。
$ composer require adrian0350/php-daemon
用法
只需调用PHPDaemon实例对象的方 法即可使用。
<?php
// Start by including the class (if 1 level down).
include_once dirname(__FILE__).'/lib/PHPDaemon.php';
用法 - 实例化
准备三个您需要的参数。
- 脚本的兼容二进制文件路径[可选]。
- 守护进程脚本文件路径。
/**
* Binary file path (per se) [OPTIONAL].
* · php
* · bash
*
* You could run '$ which php' in console
* and get binary's filepath or use PHP's (^5.4) constant PHP_BINARY (although it's already being used internally).
*
* @var string $binary Binary filepath.
*/
$binary = '/opt/local/bin/php';
/**
* The daemonized † filepath & filename.
*
* @var string $script Filepath & filename.
*/
$script = dirname(__FILE__).'/daemon/test.php';
/**
* Logging options.
*
* @var array $options Includes log & log_path.
*/
$options = array(
'log' => true,
'log_path' => '/your/log/path'
);
// Instance receives 3 arguments (binary as optional).
$PHPDaemon = new PHPDaemon\PHPDaemon($script, $options, $binary);
用法 - 实例处理
if ($PHPDaemon->isAlive())
{
echo mb_convert_encoding('😈', 'UTF-8', 'HTML-ENTITIES');
echo ' Your daemon is alive and running!'.PHP_EOL;
}
else
{
if ($PHPDaemon->start())
{
echo 'You\'ve unleashed the beast!'.PHP_EOL;
}
else
{
echo $PHPDaemon->error['message'].PHP_EOL;
}
}
用法 - 停止守护进程脚本
if ($PHPDaemon->stop())
{
echo mb_convert_encoding('😈', 'UTF-8', 'HTML-ENTITIES');
echo ' Your daemon has been stopped from deminishing the world!'.PHP_EOL;
}
else
{
// Print out error message.
echo $PHPDaemon->error['message'];
}