【发布时间】:2012-12-18 10:16:32
【问题描述】:
我必须构建一个 PHP 队列系统,并找到了这个 brilliant article
我用它来创建一个 PHP 队列系统,它非常易于设置和使用。http://squirrelshaterobots.com/programming/php/building-a-queue-server-in-php-part-1-understanding-the-project
下面是 queue.php 的代码,从 shell(puTTy 或类似的)运行。
<?PHP
//. set this constant to false if we ever need to debug
//. the application in a terminal.
define('QUEUESERVER_FORK', true);
//////// fork into a background process ////////
if(QUEUESERVER_FORK){
$pid = pcntl_fork();
if($pid === -1) die('error: unable to fork.');
else if($pid) exit(0);
posix_setsid();
sleep(1);
ob_start();
}
$queue = array();
//////// setup our named pipe ////////
$pipefile = '/tmp/queueserver-input';
if(file_exists($pipefile))
if(!unlink($pipefile))
die('unable to remove stale file');
umask(0);
if(!posix_mkfifo($pipefile, 0666))
die('unable to create named pipe');
$pipe = fopen($pipefile,'r+');
if(!$pipe) die('unable to open the named pipe');
stream_set_blocking($pipe, false);
//////// process the queue ////////
while(1){
while($input = trim(fgets($pipe))){
stream_set_blocking($pipe, false);
$queue[] = $input;
}
$job = current($queue);
$jobkey = key($queue);
if($job){
echo 'processing job ', $job, PHP_EOL;
process($job);
next($queue);
unset($job, $queue[$jobkey]);
}else{
echo 'no jobs to do - waiting...', PHP_EOL;
stream_set_blocking($pipe, true);
}
if(QUEUESERVER_FORK) ob_clean();
}
?>
最困难的部分是让 pcntl 函数在我的服务器上运行。
我的问题是“当/如果服务器必须重新启动时,我如何让作业自动启动?”
如 cmets 所述,编辑了断开的链接,并为后代提供了优秀的网络存档。
【问题讨论】:
-
您的问题究竟是什么是?
-
@MartinBean 当/如果服务器必须重新启动时,我如何让作业自动启动?
-
您可以修改服务器启动脚本来执行此操作,或者您可以添加将报告脚本/服务器状态的脚本和用于重新启动作业的第二个脚本,然后从本地或远程主机使用它们来监控服务器/重启作业(使用 cronjob)
-
@Calos 你有没有保存页面的精彩文章?该网站似乎已关闭,而这正是我个人所需要的 :)
-
这是一篇精彩的文章 - 我为它添加了书签是有充分理由的。我认为this 是我们现在最好的选择 - 2016 年 1 月有效。
标签: php queue system daemon queuing