Xataface Email Module 0.2
Email/Mailmerge Module for Xataface
actions/email_cron_job.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class actions_email_cron_job {
00004         const JOB_IN_PROGRESS = 201;
00005         const JOB_NOT_FOUND = 404;
00006         const JOB_COMPLETE = 200;
00007         const JOB_CANCELLED = 301;
00008         
00009         private $mutex;
00010         function handle($params){
00011                 session_write_close();
00012                 ignore_user_abort(true);
00013                 set_time_limit(0);
00014                 $app = Dataface_Application::getInstance();
00015                 $app->_conf['nocache'] = 1;
00016                 $query = $app->getQuery();
00017                 
00018                 if ( !@$query['--job-id'] ){
00019                         throw new Exception("No job id provided");
00020                 }
00021                 
00022                 $res = $this->performJob($query['--job-id']);
00023                 
00024                 switch ($res){
00025                 
00026                         case self::JOB_IN_PROGRESS:
00027                                 $res2 = df_q("select * from xataface__email_jobs where job_id='".addslashes($query['--job-id'])."'");
00028                                 $row = mysql_fetch_assoc($res2);
00029                                 if ( !$row ){
00030                                         // No progress listed
00031                                         throw new Exception("The job could not be found", self::JOB_NOT_FOUND);
00032                                 }
00033                                 $row['start_time'] = strftime('%c', $row['start_time']);
00034                                 $out = array(
00035                                         'code' => self::JOB_IN_PROGRESS,
00036                                         'message' => 'Job in progress',
00037                                         'data' => $row
00038                                 );
00039                                 
00040                                 $this->out($out);
00041                                 break;
00042                                 
00043                         case self::JOB_COMPLETE:
00044                                 $res2 = df_q("select * from xataface__email_jobs where job_id='".addslashes($query['--job-id'])."'");
00045                                 $row = mysql_fetch_assoc($res2);
00046                                 if ( !$row ){
00047                                         // No progress listed
00048                                         throw new Exception("The job could not be found", self::JOB_NOT_FOUND);
00049                                 }
00050                                 $row['start_time'] = strftime('%c', $row['start_time']);
00051                                 $row['end_time'] = strftime('%c', $row['end_time']);
00052                                 $out = array(
00053                                         'code' => self::JOB_COMPLETE,
00054                                         'message' => 'Job complete',
00055                                         'data' => $row
00056                                 );
00057                                 
00058                                 $this->out($out);
00059                                 break;
00060                                 
00061                         case self::JOB_CANCELLED:
00062                                 $res2 = df_q("select * from xataface__email_jobs where job_id='".addslashes($query['--job-id'])."'");
00063                                 $row = mysql_fetch_assoc($res2);
00064                                 if ( !$row ){
00065                                         // No progress listed
00066                                         throw new Exception("The job could not be found", self::JOB_NOT_FOUND);
00067                                 }
00068                                 $row['start_time'] = strftime('%c', $row['start_time']);
00069                                 $out = array(
00070                                         'code' => self::JOB_CANCELLED,
00071                                         'message' => 'Job paused',
00072                                         'data' => $row
00073                                 );
00074                                 
00075                                 $this->out($out);
00076                                 break;
00077                         
00078                                 
00079                                 
00080                         default:
00081                                 $out = array(
00082                                         'code' => $res,
00083                                         'message' => 'An error occurred trying to perform job.'
00084                                 );
00085                                 $this->out($out);
00086                                 break;
00087                 }
00088                 
00089                 
00090         
00091         }
00092         
00093         
00094         function performJob($jobId){
00095                 if  (!$this->mutex('email_cron_job_'.basename($jobId)) ){
00096                         return self::JOB_IN_PROGRESS;
00097                 }
00098                 require_once dirname(__FILE__).'/email.php';
00099                 $action = new actions_email;
00100                 
00101                 $res = df_q("select * from xataface__email_jobs where job_id='".addslashes($jobId)."'");
00102                 
00103                 $row = mysql_fetch_assoc($res);
00104                 if ( !$row ){
00105                         return self::JOB_NOT_FOUND;             
00106                 }
00107                 
00108                 if ( $row['complete'] ){
00109                         return self::JOB_COMPLETE;
00110                 }
00111                 
00112                 if ( $row['cancelled'] ){
00113                         return self::JOB_CANCELLED;
00114                 }
00115                 
00116                 //echo "\nSending mail for job $row[job_id] ...";
00117                 $res2 = mysql_query("delete from `".$row['join_table']."` where recipient_email='' and messageid='".addslashes($row['email_id'])."'", df_db());
00118                 $action->sendMail($row['email_id'],$row['email_table'],$row['join_table'],$row['recipients_table'],$row['email_column']);               
00119                 
00120                 // check to see if all the messages for this job have been sent yet
00121                 $res2 = mysql_query("select count(*) from `".$row['join_table']."` where sent<>1 and messageid='".addslashes($row['email_id'])."'", df_db());
00122                 if ( !$res2 ) trigger_error(mysql_error(df_db()), E_USER_ERROR);
00123                 list($num)=mysql_fetch_row($res2);
00124                 @mysql_free_result($res2);
00125                 if ( $num==0 ){
00126                 
00127                         $res2 = df_q("update xataface__email_jobs set active=0, complete=1, end_time='".addslashes(time())."' where job_id='".addslashes($jobId)."'", df_db());
00128                         
00129                         return self::JOB_COMPLETE;
00130                         //echo "\nJob $row[job_id] is complete!  Deleting job...";
00131                         //$res2 = mysql_query("delete from dataface__email_jobs where job_id='".addslashes($row['job_id'])."' limit 1", df_db());
00132                         //if ( !$res2 ) trigger_error(mysql_error(df_db()), E_USER_ERROR);
00133                 } else {
00134                         //echo "\nAfter sending mail for job $row[job_id], there are still $num messages left to send.";
00135                         $res = df_q("select * from xataface__email_jobs where job_id='".addslashes($jobId)."'");
00136                 
00137                         $row = mysql_fetch_assoc($res);
00138                         if ( !$row ){
00139                                 return self::JOB_NOT_FOUND;             
00140                         }
00141                         
00142                         if ( $row['cancelled'] ){
00143                                 return self::JOB_CANCELLED;
00144                         } else {
00145                         
00146                                 return self::JOB_IN_PROGRESS;
00147                         }
00148                 }
00149                 
00150         }
00151         
00152         
00153         
00162         function mutex($name){
00163                 
00164                 $path = sys_get_temp_dir().'/'.$name.'.mutex';
00165                 //echo $path;
00166                 $this->mutex = fopen($path, 'w');
00167                 if ( flock($this->mutex, LOCK_EX | LOCK_NB) ){
00168                         register_shutdown_function(array($this,'clear_mutex'));
00169                         return true;
00170                 } else {
00171                         return false;
00172                 }
00173                 
00174         }
00175         
00179         function clear_mutex(){
00180                 
00181                 if ( $this->mutex ){
00182                         fclose($this->mutex);
00183                 }
00184         }
00185         
00186         
00187         function out($out){
00188                 header('Content-type: text/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"');
00189                 echo json_encode($out);
00190         }
00191 
00192 }
 All Data Structures Files Functions Variables Enumerations