Trouble with a script emailing too much

Posted on 2006-03-22
Last Modified: 2013-12-12
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 = "";

$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);
      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 ="";
            $myMail->FromName ="mail System monitor";

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

                  echo "There has been a mail error sending to " . $to . "<br>";

            // Clear all addresses and attachments for next loop

      }//end for each
 }// end is_array
Question by:sebastiz
    1 Comment
    LVL 15

    Accepted Solution

    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)";

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now