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