如何构建PHP队列系统

时间:2023-01-24 18:31:03

I had to build a PHP Queue System, and found this brilliant article http://squirrelshaterobots.com/programming/php/building-a-queue-server-in-php-part-1-understanding-the-project/ and I used it to create a PHP queue system, its very easy to set-up and use.

我不得不构建一个PHP队列系统,并发现了这篇精彩的文章http://squirrelshaterobots.com/programming/php/building-a- queueserverin PHP -part-1-understand -project/,我使用它创建了一个PHP队列系统,它非常容易设置和使用。

Below is the code for queue.php, run from shell (puTTy or somesuch).

下面是队列的代码。php,从shell (puTTy或somesuch)运行。

<?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();

}

?>

The hardest part was getting the pcntl functions to work on my server.

最难的部分是让pcntl函数在我的服务器上工作。

My question is "How do i get the job to start automatically when/if the server has to restart?"

我的问题是“当/如果服务器必须重新启动时,如何让作业自动启动?”

1 个解决方案

#1


9  

My question is "How do i get the job to start automatically when/if the server has to restart?"

我的问题是“当/如果服务器必须重新启动时,如何让作业自动启动?”

By adding it to the list of things started when the server starts. Unfortunately the instructions for doing so vary wildly by operating system and OS version. You probably want to use something slightly more cross-platform. I've had a great deal of luck with supervisor, which you can probably find in the package repos on your OS of choice.

将它添加到服务器启动时启动的列表中。不幸的是,在操作系统和操作系统版本中,这样做的说明差别很大。您可能需要使用一些稍微跨平台的东西。我和主管相处得很好,你可以在你选择的操作系统的软件包里找到。

That said, you are going down the route of madness. The thing you're doing has been done before, better, by awesome people. Check out the Gearman work queue system and the accompnaying PECL extension. It happens that supervisor is pretty handy for keeping your Gearman workers alive as well.

也就是说,你正在沿着疯狂的道路前进。你正在做的事情以前就已经被很棒的人做过了。检查齿轮组工作队列系统和同步PECL扩展。碰巧的是,监督员也能很方便地让你的员工继续工作。

#1


9  

My question is "How do i get the job to start automatically when/if the server has to restart?"

我的问题是“当/如果服务器必须重新启动时,如何让作业自动启动?”

By adding it to the list of things started when the server starts. Unfortunately the instructions for doing so vary wildly by operating system and OS version. You probably want to use something slightly more cross-platform. I've had a great deal of luck with supervisor, which you can probably find in the package repos on your OS of choice.

将它添加到服务器启动时启动的列表中。不幸的是,在操作系统和操作系统版本中,这样做的说明差别很大。您可能需要使用一些稍微跨平台的东西。我和主管相处得很好,你可以在你选择的操作系统的软件包里找到。

That said, you are going down the route of madness. The thing you're doing has been done before, better, by awesome people. Check out the Gearman work queue system and the accompnaying PECL extension. It happens that supervisor is pretty handy for keeping your Gearman workers alive as well.

也就是说,你正在沿着疯狂的道路前进。你正在做的事情以前就已经被很棒的人做过了。检查齿轮组工作队列系统和同步PECL扩展。碰巧的是,监督员也能很方便地让你的员工继续工作。