OcR Envy
06-19-2009, 10:29 AM
The following cron job script has been reported to me by my host as using a lot of CPU. To the point where our site has started to return error messages while it is running. I am looking for suggestions on how to optimize any way possible.
// ---------------------------------------------------
// Start Set PHP Environment
// ---------------------------------------------------
error_reporting(E_ALL & ~E_NOTICE);
if (!is_object($vbulletin->db))
{
echo "No vBulletin Found!";
exit;
}
// ---------------------------------------------------
// Start Cronjob
// ---------------------------------------------------
global $vbulletin;
global $db;
$updatetime=time();
$usersoffline=0;
putenv("TZ=US/Eastern");
$checktime=date("h:i A", time());
$checkdate=date("m/d/y", time());
$vbulletin->db->query_write("UPDATE xblusers SET time='$checktime', date='$checkdate' WHERE title='checked'");
if ($xml = simplexml_load_file("http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=outcast%20reborn")) {
$account=$xml->AccountStatus;
if($account=="Gold") {
$vbulletin->db->query_write("UPDATE xblusers SET time='00:00PM', date='00/00/00' WHERE title='crashed'");
$logmsg = "Whos on XBL Updated";
} else {
$offline="true";
}
} else {
$offline="true";
}
if($offline=="true") {
$grabchecked = $vbulletin->db->query("SELECT time, date FROM xblusers WHERE title='crashed'");
$gchecked=$vbulletin->db->fetch_array($grabchecked);
$checkedt=$gchecked['time'];
if($checkedt=="00:00PM") {
$vbulletin->db->query_write("UPDATE xblusers SET time='$checktime', date='$checkdate' WHERE title='crashed'");
}
$logmsg = "Whos on XBL Not Updated: Stream Down<br />";
log_cron_action($logmsg, $nextitem, 0);
die;
}
$gamertaggrab=$vbulletin->db->query("SELECT userfield.field5, userfield.field11, userfield.userid FROM userfield INNER JOIN user ON user.userid = userfield.userid AND userfield.field5 > '' WHERE usergroupid IN (6,48,52,51,26,58,60,55,46)");
while($userrow = $vbulletin->db->fetch_array($gamertaggrab)) {
$gamertag=$userrow['field5'];
if ($gamertag) {
if ($xml = @simplexml_load_file("http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=". urlencode($gamertag))) {
$coretag=$xml->Gamertag;
$tagvalid=$xml->PresenceInfo->Valid;
$onlinestatus=$xml->PresenceInfo->Online;
$statustext=$xml->PresenceInfo->StatusText;
$title=$xml->PresenceInfo->Title;
$info=$xml->PresenceInfo->Info;
$info2=$xml->PresenceInfo->Info2;
$lastseen=$xml->PresenceInfo->LastSeen;
$statustext=addslashes($statustext);
$title=addslashes($title);
$info=addslashes($info);
$info2=addslashes($info2);
if ($onlinestatus=="true") {
$online=1;
$logmsg = $logmsg . "<li><a href='http://www.outcastreborn.com/member.php?u=" .$userrow['userid']. "'>" . $gamertag . "</a>: " . " " . $statustext . " " . $title . " " . $info2 . " </li>";
$sms_string = $sms_string + $gamertag . " ";
} else {
$online=0;
$usersoffline = $usersoffline + 1;
}
foreach ($xml->xpath('//Game') as $gameset) {
$gamename=$gameset->Name;
$gametile=$gameset->Image32url;
$gamename=addslashes($gamename);
$vbulletin->db->query_write("INSERT IGNORE INTO xblgames (name,tile) VALUES ('".$gamename."','".$gametile."')");
}
if ($tagvalid=="true") {
$vbulletin->db->query_write("INSERT INTO xblstatus (userid,gamertag,online,statustext,title,info,info 2,lastseen,updatetime)
VALUES ('".$userrow['userid']."','$coretag','$online','$statustext','$title','$in fo','$info2','$lastseen',FROM_UNIXTIME($updatetime )) ON DUPLICATE KEY
UPDATE userid='".$userrow['userid']."',gamertag='$coretag',online='$online',statustext= '$statustext',title='$title',info='$info',info2='$ info2',
lastseen='$lastseen',updatetime=FROM_UNIXTIME($upd atetime)");
$vbulletin->db->query_write("UPDATE userfield SET field12='$statustext',field13='$title',field14='$i nfo2' WHERE userid=".$userrow['userid']);
}
}
}
$gamertag=$userrow['field11'];
if ($gamertag) {
if ($xml = @simplexml_load_file("http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=". urlencode($gamertag))) {
$coretag=$xml->Gamertag;
$tagvalid=$xml->PresenceInfo->Valid;
$onlinestatus=$xml->PresenceInfo->Online;
$statustext=$xml->PresenceInfo->StatusText;
$title=$xml->PresenceInfo->Title;
$info=$xml->PresenceInfo->Info;
$info2=$xml->PresenceInfo->Info2;
$lastseen=$xml->PresenceInfo->LastSeen;
$statustext=addslashes($statustext);
$title=addslashes($title);
$info=addslashes($info);
$info2=addslashes($info2);
if ($onlinestatus=="true") {
$online=1;
$logmsg = $logmsg . "<li><a href='http://www.outcastreborn.com/member.php?u=" .$userrow['userid']. "'>" . $gamertag . "</a>: " . " " . $statustext . " " . $title . " " . $info2 . " </li>";
$sms_string = $sms_string + $gamertag . " ";
} else {
$online=0;
$usersoffline = $usersoffline + 1;
}
if ($tagvalid=="true") {
$vbulletin->db->query_write("INSERT INTO sxblstatus (userid,gamertag,online,statustext,title,info,info 2,lastseen,updatetime)
VALUES ('".$userrow['userid']."','$coretag','$online','$statustext','$title','$in fo','$info2','$lastseen',FROM_UNIXTIME($updatetime )) ON DUPLICATE KEY
UPDATE userid='".$userrow['userid']."',gamertag='$coretag',online='$online',statustext= '$statustext',title='$title',info='$info',info2='$ info2',
lastseen='$lastseen',updatetime=FROM_UNIXTIME($upd atetime)");
$vbulletin->db->query("UPDATE userfield SET field15='$statustext',field16='$title',field17='$i nfo2' WHERE userid=".$userrow['userid']);
}
}
}
}
$logmsg = $logmsg . "<li> Users offline: " . $usersoffline ."</li></ul>";
log_cron_action($logmsg, $nextitem, 0);
The main SQL SELECT userfield.field5, userfield.field11, userfield.userid FROM userfield INNER JOIN user ON user.userid = userfield.userid AND userfield.field5 > '' WHERE usergroupid IN (6,48,52,51,26,58,60,55,46) Returns 87 Results. The while statement runs 87 times.
I am not giving permission to reuse any of this code at this time.
// ---------------------------------------------------
// Start Set PHP Environment
// ---------------------------------------------------
error_reporting(E_ALL & ~E_NOTICE);
if (!is_object($vbulletin->db))
{
echo "No vBulletin Found!";
exit;
}
// ---------------------------------------------------
// Start Cronjob
// ---------------------------------------------------
global $vbulletin;
global $db;
$updatetime=time();
$usersoffline=0;
putenv("TZ=US/Eastern");
$checktime=date("h:i A", time());
$checkdate=date("m/d/y", time());
$vbulletin->db->query_write("UPDATE xblusers SET time='$checktime', date='$checkdate' WHERE title='checked'");
if ($xml = simplexml_load_file("http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=outcast%20reborn")) {
$account=$xml->AccountStatus;
if($account=="Gold") {
$vbulletin->db->query_write("UPDATE xblusers SET time='00:00PM', date='00/00/00' WHERE title='crashed'");
$logmsg = "Whos on XBL Updated";
} else {
$offline="true";
}
} else {
$offline="true";
}
if($offline=="true") {
$grabchecked = $vbulletin->db->query("SELECT time, date FROM xblusers WHERE title='crashed'");
$gchecked=$vbulletin->db->fetch_array($grabchecked);
$checkedt=$gchecked['time'];
if($checkedt=="00:00PM") {
$vbulletin->db->query_write("UPDATE xblusers SET time='$checktime', date='$checkdate' WHERE title='crashed'");
}
$logmsg = "Whos on XBL Not Updated: Stream Down<br />";
log_cron_action($logmsg, $nextitem, 0);
die;
}
$gamertaggrab=$vbulletin->db->query("SELECT userfield.field5, userfield.field11, userfield.userid FROM userfield INNER JOIN user ON user.userid = userfield.userid AND userfield.field5 > '' WHERE usergroupid IN (6,48,52,51,26,58,60,55,46)");
while($userrow = $vbulletin->db->fetch_array($gamertaggrab)) {
$gamertag=$userrow['field5'];
if ($gamertag) {
if ($xml = @simplexml_load_file("http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=". urlencode($gamertag))) {
$coretag=$xml->Gamertag;
$tagvalid=$xml->PresenceInfo->Valid;
$onlinestatus=$xml->PresenceInfo->Online;
$statustext=$xml->PresenceInfo->StatusText;
$title=$xml->PresenceInfo->Title;
$info=$xml->PresenceInfo->Info;
$info2=$xml->PresenceInfo->Info2;
$lastseen=$xml->PresenceInfo->LastSeen;
$statustext=addslashes($statustext);
$title=addslashes($title);
$info=addslashes($info);
$info2=addslashes($info2);
if ($onlinestatus=="true") {
$online=1;
$logmsg = $logmsg . "<li><a href='http://www.outcastreborn.com/member.php?u=" .$userrow['userid']. "'>" . $gamertag . "</a>: " . " " . $statustext . " " . $title . " " . $info2 . " </li>";
$sms_string = $sms_string + $gamertag . " ";
} else {
$online=0;
$usersoffline = $usersoffline + 1;
}
foreach ($xml->xpath('//Game') as $gameset) {
$gamename=$gameset->Name;
$gametile=$gameset->Image32url;
$gamename=addslashes($gamename);
$vbulletin->db->query_write("INSERT IGNORE INTO xblgames (name,tile) VALUES ('".$gamename."','".$gametile."')");
}
if ($tagvalid=="true") {
$vbulletin->db->query_write("INSERT INTO xblstatus (userid,gamertag,online,statustext,title,info,info 2,lastseen,updatetime)
VALUES ('".$userrow['userid']."','$coretag','$online','$statustext','$title','$in fo','$info2','$lastseen',FROM_UNIXTIME($updatetime )) ON DUPLICATE KEY
UPDATE userid='".$userrow['userid']."',gamertag='$coretag',online='$online',statustext= '$statustext',title='$title',info='$info',info2='$ info2',
lastseen='$lastseen',updatetime=FROM_UNIXTIME($upd atetime)");
$vbulletin->db->query_write("UPDATE userfield SET field12='$statustext',field13='$title',field14='$i nfo2' WHERE userid=".$userrow['userid']);
}
}
}
$gamertag=$userrow['field11'];
if ($gamertag) {
if ($xml = @simplexml_load_file("http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=". urlencode($gamertag))) {
$coretag=$xml->Gamertag;
$tagvalid=$xml->PresenceInfo->Valid;
$onlinestatus=$xml->PresenceInfo->Online;
$statustext=$xml->PresenceInfo->StatusText;
$title=$xml->PresenceInfo->Title;
$info=$xml->PresenceInfo->Info;
$info2=$xml->PresenceInfo->Info2;
$lastseen=$xml->PresenceInfo->LastSeen;
$statustext=addslashes($statustext);
$title=addslashes($title);
$info=addslashes($info);
$info2=addslashes($info2);
if ($onlinestatus=="true") {
$online=1;
$logmsg = $logmsg . "<li><a href='http://www.outcastreborn.com/member.php?u=" .$userrow['userid']. "'>" . $gamertag . "</a>: " . " " . $statustext . " " . $title . " " . $info2 . " </li>";
$sms_string = $sms_string + $gamertag . " ";
} else {
$online=0;
$usersoffline = $usersoffline + 1;
}
if ($tagvalid=="true") {
$vbulletin->db->query_write("INSERT INTO sxblstatus (userid,gamertag,online,statustext,title,info,info 2,lastseen,updatetime)
VALUES ('".$userrow['userid']."','$coretag','$online','$statustext','$title','$in fo','$info2','$lastseen',FROM_UNIXTIME($updatetime )) ON DUPLICATE KEY
UPDATE userid='".$userrow['userid']."',gamertag='$coretag',online='$online',statustext= '$statustext',title='$title',info='$info',info2='$ info2',
lastseen='$lastseen',updatetime=FROM_UNIXTIME($upd atetime)");
$vbulletin->db->query("UPDATE userfield SET field15='$statustext',field16='$title',field17='$i nfo2' WHERE userid=".$userrow['userid']);
}
}
}
}
$logmsg = $logmsg . "<li> Users offline: " . $usersoffline ."</li></ul>";
log_cron_action($logmsg, $nextitem, 0);
The main SQL SELECT userfield.field5, userfield.field11, userfield.userid FROM userfield INNER JOIN user ON user.userid = userfield.userid AND userfield.field5 > '' WHERE usergroupid IN (6,48,52,51,26,58,60,55,46) Returns 87 Results. The while statement runs 87 times.
I am not giving permission to reuse any of this code at this time.