views:

58

answers:

2

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-&gt;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;
} 
A: 

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

JurisM
that sounds like a good approach. or put it inside the while loop and check is_resource to see if the connection exists, else recreate it. will try something like this. any others?
fjallstrom
+1  A: 

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.

netcoder
+1, this works. Typically, when I write something like, it opens a regular connection on startup, and uses it for the duration. If a query fails, try to reconnect a couple of times, and if that doesn't work, fail gracefully.
timdev