1
\$\begingroup\$

The following is a sample cron job I've written that sends an email with some conditions. I haven't run it yet but I'm using another cron job (and will rewrite very soon) that does the same thing but written using mysqli_query, mysqli_fetch_assoc, mysqli_fetch_array, mysqli_error instead.

  • Which one supposed to perform better?
  • How can I monitor any errors that this cron might have? How should I report the errors accordingly?

$info_query = 'SELECT status FROM scheduled_tasks WHERE file_name = ?';
$file = 'thank_you.php';
   if($cron_info = $conn->prepare($info_query)) {
      $cron_info->bind_param('s', $file);
      $info_exe = $cron_info->execute();
      if($info_exe === FALSE){
           // Do some error reporting
      } else {
        $cron_info->bind_result($status_cron);
        $cron_info->fetch();
        $cron_info->close();
      }
  }
  if($status_cron=='active'){
     //================Check Timezone===============//
     $core_query = "SELECT t1.Id,t2.timezone FROM $Events as t1
                INNER JOIN locations as t2 ON t1.Location_id=t2.id WHERE t1.Status ='Active'";
    if($Core_Result = $conn->prepare($core_query)) {
       $Core_Result_exe = $Core_Result->execute();
       if($Core_Result_exe === FALSE){
           // Do some error reporting
           // Some way to monitor performance?
      } else {
        $Core_Result->bind_result($Id, $timezone);
        while($Core_Result->fetch()){
          date_default_timezone_set($timezone);
          $date = date("Y-m-d");
          $day_current = date( 'Y-m-d', strtotime( $date . ' -1 day' ) );
          $hour = date("H");
          $min  = date("i");

          $core_result = "SELECT First_Name,Mobile_Phone,Meeting_Location,SVC_Contact,SVC_Number,Map_link,Start_Time,User_Email,Mobile_Phone
FROM $Details WHERE User_Id > '0' and Confirmed='Completed' AND Date=? AND Core_Event_Id=?";
          if($query_user = $conn->prepare($core_result)){
             $query_user->bind_param('ss', $day_current, $Id);
             $query_user_exe = $query_user->execute();
            if($query_user_exe === FALSE){
              // Do some error reporting
              // Some way to monitor performance?
            } else {
             $query_user->store_result();
             $query_user->bind_result( $First_Name,$Mobile_Phone,$Meeting_Location,$SVC_Contact,$SVC_Number,$Map_link,$Start_Time,$User_Email,$Mobile_Phone);
             while($query_user->fetch()){
                   if($Meeting_Location=='Phone Interview'){ $html_text = "phone interview complete"; } else { $html_text = "general interview complete"; }
                       $args = array(
                             'key' => $key,  
                              'message' => array(
                              "html" => $html_text,
                              "text" => null,
                              "from_email" => $admin_email,
                              "from_name" => "Coordinator",
                              "subject" =>"Received!",
                              "to" => array(
                                array("email" => 'mail@example.com')),
                                  "track_opens" => true,
                                  "track_clicks" => true,
                                  "auto_text" => true
                                )  
                        );

                        $curl = curl_init('https://mandrillapp.com/api/1.0/messages/send.json');
                        curl_setopt($curl, CURLOPT_POST, true);

                        curl_setopt($curl, CURLOPT_HEADER, false);
                        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

                        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($args));
                        $response = curl_exec($curl);
                        echo "<pre>";print_r($response);
                        curl_close($curl);

                        file_put_contents('interview_error.txt', print_r($response, true), FILE_APPEND);
                    }
                 }
              }
           }
        }
    }
}
?>
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.