mysql connection from daemon written in php

倖福魔咒の 提交于 2019-12-11 06:25:58

问题


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;
} 

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/4063230/mysql-connection-from-daemon-written-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!