Trouble with a script emailing too much

I have a script that is supposed to email me based on certain criteria. It runs as a cron job The first criteria is that the task sent to our file converter fails (is a field value in the database). This part works fine. The second criteria is that a task has been stuck on converting for more than half an hour. This isnt working. I get emailed when the cron job runs and a task is converting at the time- it doesnt seem to check that it has been converting for half an hour. Consequently, i get loads of emails for tasks that are converting as per normal.The script is as follows:

<?
include ("include.php");


// CASE: Failed file conversion.
// ACTION: Alert SysAdmin.

echo "CASE: ConvertFailed or Converting for too long.<hr>";

// this email is cc'd to admins
$SysAdminEmail = "sebastiz@mail.com";

$Values = 't.TaskID ';
$Table = 'Task t';
$Where = "t.TimePosted > NOW() - INTERVAL 60 MINUTE AND (t.TaskStatus = 'ConvertFailed' OR t.TaskStatus = 'Converting' AND t.LastUpdated > NOW() - INTERVAL 30 MINUTE)";

$Results = $mySQL->dbGetValueWhere($myDb,$Values,$Table,$Where);
if(is_array($Results)){
      foreach($Results as $Result){
            $TaskID = $Result[TaskID];
              $SystemEmail = $Result[Email];

            $to = $SystemEmail;
            // send email
            $body ="";
            $body .="This task $TaskID is in trouble.\n\n";
            
            $myMail->Priority = 1;
            $myMail->Body = $body;
            $myMail->Subject = '';
            $myMail->From ="sysadmin@mail.com";
            $myMail->FromName ="mail System monitor";
            $myMail->IsSendmail();

            // Add the SysAdmin
            $myMail->IsSendmail();
            $myMail->AddCC($SysAdminEmail); // works with SendMail

            $myMail->AddAddress($to);
            if(!$myMail->Send())
                  echo "There has been a mail error sending to " . $to . "<br>";

            // Clear all addresses and attachments for next loop
            $myMail->ClearAddresses();
            $myMail->ClearAttachments();

      }//end for each
 }// end is_array
?>
sebastizAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TomeeboyCommented:
Looks like a problem with your query.  According to your query, you want to select everything that was posted in the last 60 minutes and has a status of "ConvertFailed" or "Converting" with a LastUpdated time in the last 30 minutes (that is what > NOW() - INTERVAL 30 MINUTE would mean).  The last part of your query appears to be the problem to me, as I think you would want it to retrieve items that have NOT been updated in the last 30 minutes (meaning they are stuck, timed out, etc.).  Try this:

$Where = "t.TimePosted > NOW() - INTERVAL 60 MINUTE AND (t.TaskStatus = 'ConvertFailed' OR t.TaskStatus = 'Converting' AND t.LastUpdated < NOW() - INTERVAL 30 MINUTE)";

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.