public static function runJobs_pre($offset = 200, $mode = NULL) { $job = new CRM_Mailing_BAO_Job(); $config = CRM_Core_Config::singleton(); $jobTable = CRM_Mailing_DAO_Job::getTableName(); $mailingTable = CRM_Mailing_DAO_Mailing::getTableName(); $currentTime = date('YmdHis'); $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL('m'); $workflowClause = CRM_Mailing_BAO_Job::workflowClause(); $domainID = CRM_Core_Config::domainID(); $modeClause = 'AND m.sms_provider_id IS NULL'; if ($mode == 'sms') { $modeClause = 'AND m.sms_provider_id IS NOT NULL'; } // Select all the mailing jobs that are created from // when the mailing is submitted or scheduled. $query = "\n\t\tSELECT j.*\n\t\t FROM {$jobTable} j,\n\t\t\t\t {$mailingTable} m\n\t\t WHERE m.id = j.mailing_id AND m.domain_id = {$domainID}\n {$workflowClause}\n {$modeClause}\n\t\t AND j.is_test = 0\n\t\t AND ( ( j.start_date IS null\n\t\t AND j.scheduled_date <= {$currentTime}\n\t\t AND j.status = 'Scheduled'\n\t\t AND j.end_date IS null ) )\n\t\t AND ((j.job_type is NULL) OR (j.job_type <> 'child'))\n\t\tORDER BY j.scheduled_date,\n\t\t\t\t j.start_date"; $job->query($query); // For each of the "Parent Jobs" we find, we split them into // X Number of child jobs while ($job->fetch()) { // still use job level lock for each child job $lockName = "civimail.job.{$job->id}"; $lock = new CRM_Core_Lock($lockName); if (!$lock->isAcquired()) { continue; } // Re-fetch the job status in case things // changed between the first query and now // to avoid race conditions $job->status = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Job', $job->id, 'status'); if ($job->status != 'Scheduled') { $lock->release(); continue; } $job->split_job($offset); // update the status of the parent job $transaction = new CRM_Core_Transaction(); $saveJob = new CRM_Mailing_DAO_Job(); $saveJob->id = $job->id; $saveJob->start_date = date('YmdHis'); $saveJob->status = 'Running'; $saveJob->save(); $transaction->commit(); // Release the job lock $lock->release(); } }
/** * Initiate all pending/ready jobs * * @return void * @access public * @static */ public static function runJobs($testParams = null) { $job =& new CRM_Mailing_BAO_Job(); $mailing =& new CRM_Mailing_DAO_Mailing(); $config =& CRM_Core_Config::singleton(); $jobTable = CRM_Mailing_DAO_Job::getTableName(); $mailingTable = CRM_Mailing_DAO_Mailing::getTableName(); if (!empty($testParams)) { $query = "\nSELECT *\n FROM {$jobTable}\n WHERE id = {$testParams['job_id']}"; $job->query($query); } else { $currentTime = date('YmdHis'); /* FIXME: we might want to go to a progress table.. */ $query = "\nSELECT j.*\n FROM {$jobTable} j,\n {$mailingTable} m\n WHERE m.id = j.mailing_id\n AND j.is_test = 0\n AND ( ( j.start_date IS null\n AND j.scheduled_date <= {$currentTime}\n AND j.status = 'Scheduled' )\n OR ( j.status = 'Running'\n AND j.end_date IS null ) )\nORDER BY j.scheduled_date,\n j.start_date"; $job->query($query); } require_once 'CRM/Core/Lock.php'; /* TODO We should parallelize or prioritize this */ while ($job->fetch()) { // fix for cancel job at run time which is in queue, CRM-4246 if (CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Job', $job->id, 'status') == 'Canceled') { continue; } $lockName = "civimail.job.{$job->id}"; // get a lock on this job id $lock = new CRM_Core_Lock($lockName); if (!$lock->isAcquired()) { continue; } /* Queue up recipients for all jobs being launched */ if ($job->status != 'Running') { require_once 'CRM/Core/Transaction.php'; $transaction = new CRM_Core_Transaction(); $job->queue($testParams); /* Start the job */ // use a seperate DAO object to protect the loop // integrity. I think transactions messes it up // check CRM-2469 $saveJob = new CRM_Mailing_DAO_Job(); $saveJob->id = $job->id; $saveJob->start_date = date('YmdHis'); $saveJob->status = 'Running'; $saveJob->save(); $transaction->commit(); } $mailer =& $config->getMailer(); /* Compose and deliver */ $isComplete = $job->deliver($mailer, $testParams); require_once 'CRM/Utils/Hook.php'; CRM_Utils_Hook::post('create', 'CRM_Mailing_DAO_Spool', $job->id, $isComplete); if ($isComplete) { /* Finish the job */ require_once 'CRM/Core/Transaction.php'; $transaction = new CRM_Core_Transaction(); // use a seperate DAO object to protect the loop // integrity. I think transactions messes it up // check CRM-2469 $saveJob = new CRM_Mailing_DAO_Job(); $saveJob->id = $job->id; $saveJob->end_date = date('YmdHis'); $saveJob->status = 'Complete'; $saveJob->save(); $mailing->reset(); $mailing->id = $job->mailing_id; $mailing->is_completed = true; $mailing->save(); $transaction->commit(); } $lock->release(); if ($testParams) { return $isComplete; } } }
public static function runJobs_pre($offset = 200) { $job = new CRM_Mailing_BAO_Job(); $config = CRM_Core_Config::singleton(); $jobTable = CRM_Mailing_DAO_Job::getTableName(); $mailingTable = CRM_Mailing_DAO_Mailing::getTableName(); $currentTime = date('YmdHis'); $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL('m'); // add an additional check and only process // jobs that are approved $workflowClause = null; require_once 'CRM/Mailing/Info.php'; if (CRM_Mailing_Info::workflowEnabled()) { require_once 'CRM/Core/OptionGroup.php'; $approveOptionID = CRM_Core_OptionGroup::getValue('mail_approval_status', 'Approved', 'name'); if ($approveOptionID) { $workflowClause = " AND m.approval_status_id = {$approveOptionID} "; } } // Select all the mailing jobs that are created from // when the mailing is submitted or scheduled. $query = "\n\t\tSELECT j.*\n\t\t FROM {$jobTable} j,\n\t\t\t\t {$mailingTable} m\n\t\t WHERE m.id = j.mailing_id\n {$workflowClause}\n\t\t AND j.is_test = 0\n\t\t AND ( ( j.start_date IS null\n\t\t AND j.scheduled_date <= {$currentTime}\n\t\t AND j.status = 'Scheduled'\n\t\t AND j.end_date IS null ) )\n\t\t AND ((j.job_type is NULL) OR (j.job_type <> 'child'))\n\t\tORDER BY j.scheduled_date,\n\t\t\t\t j.start_date"; $job->query($query); require_once 'CRM/Core/Lock.php'; // For reach of the "Parent Jobs" we find, we split them into // X Number of child jobs while ($job->fetch()) { // still use job level lock for each child job $lockName = "civimail.job.{$job->id}"; $lock = new CRM_Core_Lock($lockName); if (!$lock->isAcquired()) { continue; } // refetch the job status in case things // changed between the first query and now // avoid race conditions $job->status = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Job', $job->id, 'status'); if ($job->status != 'Scheduled') { $lock->release(); continue; } $job->split_job($offset); // update the status of the parent job require_once 'CRM/Core/Transaction.php'; $transaction = new CRM_Core_Transaction(); $saveJob = new CRM_Mailing_DAO_Job(); $saveJob->id = $job->id; $saveJob->start_date = date('YmdHis'); $saveJob->status = 'Running'; $saveJob->save(); $transaction->commit(); // Release the job lock $lock->release(); } }