P
P
Prescott2013-06-25 20:18:02
Message Queues
Prescott, 2013-06-25 20:18:02

Maintain the number of running copies of a PHP script?

Hello.
There is a message queue on memcacheQ. It is necessary to maintain the number of PHP scripts that listens to it at a certain level, so that at any given time there will be at least N pieces. Writing your own demon that will do this (high-quality) is not a hunt, besides, once I saw in the comments on the hub a link to a software that someone made for the same purposes before me.
Help me find it please

Answer the question

In order to leave comments, you need to log in

5 answer(s)
L
la0, 2013-06-25
@Prescott

The problem is solved by supervisord.org/ . the solution is stable and working.

A
Alexey Zhurbitsky, 2013-06-25
@blo

Gearman queue server ?
habrahabr.ru/post/123451/
habrahabr.ru/post/142210/

K
KEKSOV, 2013-06-26
@KEKSOV

I would use pcntl functions. In short, the code looks like this (just to clarify the idea)

<?php

runDaemon(10);

function runDaemon( $aMaxWorkers )
{
    $i = 0;
    for ( ; $i < $aMaxWorkers; $i++ )
    {
        forkWorker( $i );
    }
    
    while ( ( $pid = pcntl_wait( $status ) ) != -1 )
    { 
        echo "Exit $pid\n";
        forkWorker( $i++ );
    }
}

function forkWorker( $aNumber )
{
    $pid = pcntl_fork(); 

    if ( $pid )
    {
        echo "Start #$aNumber, pid $pid\n";
        return;
    }
    
    // Делаем свою работу и выходим
    sleep(5);
    exit;
}

?>

But it has one minor drawback - it does not work pcntl_wait may not catch the simultaneous exit of two workers and will not start the required number of new ones.
Luckily, smart people have figured it out. Here is the working code . Just add the start of new workers to this endless loop:
while(count($this->currentJobs)){
    echo "Waiting for current jobs to finish... \n";
    sleep(1);
}

D
deadkrolik, 2013-06-26
@deadkrolik

habrahabr.ru/qa/40647/

I
Ilya Evseev, 2013-06-28
@IlyaEvseev

A bike:

#!/bin/bash
N=5
while : ; do
    n=$(pgrep -flc "php ...")
    while $n -le $N; do
        logger "Run $n/$N"
        php ...
        : $[n++]
    done
    sleep 60
done

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question