/** * Initiate all pending/ready jobs * * @return void * @access public * @static */ function runJobs() { $job =& new CRM_Mailing_BAO_Job(); $mailing =& new CRM_Mailing_DAO_Mailing(); $jobTable = CRM_Mailing_DAO_Job::getTableName(); $config =& CRM_Core_Config::singleton(); $mailer =& $config->getMailer(); /* FIXME: we might want to go to a progress table.. */ $query = " SELECT *\n FROM {$jobTable}\n WHERE (start_date IS null\n AND scheduled_date <= NOW()\n AND status = 'Scheduled')\n OR (status = 'Running'\n AND end_date IS null)\n ORDER BY scheduled_date, start_date"; $job->query($query); /* TODO We should parallelize or prioritize this */ while ($job->fetch()) { /* Queue up recipients for all jobs being launched */ if ($job->status != 'Running') { CRM_Core_DAO::transaction('BEGIN'); $job->queue(); /* Start the job */ $job->start_date = date('YmdHis'); $job->status = 'Running'; // CRM-992 - MySQL can't eat its own dates $job->scheduled_date = CRM_Utils_Date::isoToMysql($job->scheduled_date); $job->save(); CRM_Core_DAO::transaction('COMMIT'); } /* Compose and deliver */ $job->deliver($mailer); /* Finish the job */ CRM_Core_DAO::transaction('BEGIN'); $job->end_date = date('YmdHis'); $job->status = 'Complete'; // CRM-992 - MySQL can't eat its own dates $job->scheduled_date = CRM_Utils_Date::isoToMysql($job->scheduled_date); $job->start_date = CRM_Utils_Date::isoToMysql($job->start_date); $job->save(); $mailing->reset(); $mailing->id = $job->mailing_id; $mailing->is_completed = true; $mailing->save(); CRM_Core_DAO::transaction('COMMIT'); } }
/** * 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 function split_job($offset = 200) { $recipient_count = CRM_Mailing_BAO_Recipients::mailingSize($this->mailing_id); $jobTable = CRM_Mailing_DAO_Job::getTableName(); $dao = new CRM_Core_DAO(); $sql = "\nINSERT INTO civicrm_mailing_job\n(`mailing_id`, `scheduled_date`, `status`, `job_type`, `parent_id`, `job_offset`, `job_limit`)\nVALUES (%1, %2, %3, %4, %5, %6, %7)\n"; $params = array(1 => array($this->mailing_id, 'Integer'), 2 => array($this->scheduled_date, 'String'), 3 => array('Scheduled', 'String'), 4 => array('child', 'String'), 5 => array($this->id, 'Integer'), 6 => array(0, 'Integer'), 7 => array($recipient_count, 'Integer')); // create one child job if the mailing size is less than the offset // probably use a CRM_Mailing_DAO_Job( ); if (empty($offset) || $recipient_count <= $offset) { CRM_Core_DAO::executeQuery($sql, $params); } else { // Creating 'child jobs' for ($i = 0; $i < $recipient_count; $i = $i + $offset) { $params[6][0] = $i; $params[7][0] = $offset; CRM_Core_DAO::executeQuery($sql, $params); } } }