Question

i have written a daemon to fetch some stuff from mysql and make some curl requests based on the info from mysql. since i'm fluent in php i've written this daemon in php using System_Daemon from pear.

this works fine but i'm curious about the best approach for connecting to mysql. feels weird to create a new mysql connection every couple of seconds, should i try a persistent connection? any other input? keeping potential memory leaks to a minimum is of the essence...

cleaned up the script, attached below. removed the mysql stuff for now, using a dummy array to keep this unbiased:

#!/usr/bin/php -q
<?php
require_once "System/Daemon.php";

System_Daemon::setOption("appName", "smsq");
System_Daemon::start();

$runningOkay = true;

while(!System_Daemon::isDying() && $runningOkay){

    $runningOkay = true;
    if (!$runningOkay) {
        System_Daemon::err('smsq() produced an error, '.
            'so this will be my last run');
    }

    $messages = get_outgoing();
    $messages = call_api($messages);
    #print_r($messages);

    System_Daemon::iterate(2);
}

System_Daemon::stop();  

function get_outgoing(){ # get 10 rows from a mysql table
    # dummycode, this should come from mysql
    for($i=0;$i<5;$i++){
        $message->msisdn = '070910507'.$i;
        $message->text = 'nr'.$i;
        $messages[] = $message;
        unset($message);
    }
    return $messages;
}

function call_api($messages=array()){
    if(count($messages)<=0){
        return false;
    }else{
        foreach($messages as $message){
            $message->curlhandle = curl_init();
            curl_setopt($message->curlhandle,CURLOPT_URL,'http://yadayada.com/date.php?text='.$message->text);
            curl_setopt($message->curlhandle,CURLOPT_HEADER,0);
            curl_setopt($message->curlhandle,CURLOPT_RETURNTRANSFER,1);
        }
        $mh = curl_multi_init();
        foreach($messages as $message){
            curl_multi_add_handle($mh,$message->curlhandle);
        }
        $running = null;
        do{
            curl_multi_exec($mh,$running);
         }while($running > 0);
        foreach($messages as $message){
            $message->api_response = curl_multi_getcontent($message->curlhandle);
            curl_multi_remove_handle($mh,$message->curlhandle);
            unset($message->curlhandle);
        }
        curl_multi_close($mh);
    }
    return $messages;
} 
Was it helpful?

Solution

Technically if it's a daemon, it runs in background and doesn't stop until you ask it to. There's is no need to use a persistent connection in that case, and even, you probably shouldn't. I'd expect the connection to close when I kill the daemon.

Basically, you should open a connection on startup, and close it on shutdown, and that's about it. However, you should put some error trapping in there in case the connection drops unexpectedly while the daemon is running, so it either shutdowns gracefully (by logging a connection drop somewhere) or have it retry a reconnection later.

OTHER TIPS

maybe before while statement just add mysql_pconnect, but I don't now anything about php daemons...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top