/** * 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'); } }
/** * adds $value['foo_display'] for each $value['foo'] enum from civicrm_mailing_job * * @param array $values (reference) the array up for enhancing * @return void */ static function addDisplayEnums(&$values) { $enumFields =& CRM_Mailing_DAO_Job::getEnums(); foreach ($enumFields as $enum) { if (isset($values[$enum])) { $values[$enum . '_display'] = CRM_Mailing_DAO_Job::tsEnum($enum, $values[$enum]); } } }
public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments) { // get the return properties $returnProperties = $mailing->getReturnProperties(); $params = array(); $targetParams = array(); foreach ($fields as $key => $field) { $params[] = $field['contact_id']; } $details = $mailing->getDetails($params, $returnProperties); foreach ($fields as $key => $field) { $contactID = $field['contact_id']; /* Compose the mailing */ $recipient = null; $message =& $mailing->compose($this->id, $field['id'], $field['hash'], $field['contact_id'], $field['email'], $recipient, false, $details[0][$contactID], $attachments); /* Send the mailing */ $body =& $message->get(); $headers =& $message->headers(); // make $recipient actually be the *encoded* header, so as not to baffle Mail_RFC822, CRM-5743 $recipient = $headers['To']; $result = null; /* TODO: when we separate the content generator from the delivery * engine, maybe we should dump the messages into a table */ // disable error reporting on real mailings (but leave error reporting for tests), CRM-5744 if ($job_date) { CRM_Core_Error::ignoreException(); } if (is_object($mailer)) { // hack to stop mailing job at run time, CRM-4246. $mailingJob = new CRM_Mailing_DAO_Job(); $mailingJob->mailing_id = $mailing->id; if ($mailingJob->find(true)) { // mailing have been canceled at run time. if ($mailingJob->status == 'Canceled') { return false; } } else { // mailing have been deleted at run time. return false; } $mailingJob->free(); $result = $mailer->send($recipient, $headers, $body, $this->id); CRM_Core_Error::setCallback(); } $params = array('event_queue_id' => $field['id'], 'job_id' => $this->id, 'hash' => $field['hash']); if (is_a($result, 'PEAR_Error')) { /* Register the bounce event */ require_once 'CRM/Mailing/BAO/BouncePattern.php'; require_once 'CRM/Mailing/Event/BAO/Bounce.php'; $params = array_merge($params, CRM_Mailing_BAO_BouncePattern::match($result->getMessage())); CRM_Mailing_Event_BAO_Bounce::create($params); } else { /* Register the delivery event */ CRM_Mailing_Event_BAO_Delivered::create($params); } $targetParams[] = $field['contact_id']; unset($result); } if (!empty($targetParams)) { // add activity record for every mail that is send $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'Bulk Email', 'name'); $activity = array('source_contact_id' => $mailing->scheduled_id, 'target_contact_id' => $targetParams, 'activity_type_id' => $activityTypeID, 'source_record_id' => $this->mailing_id, 'activity_date_time' => $job_date, 'subject' => $mailing->subject, 'status_id' => 2, 'deleteActivityTarget' => false); //check whether activity is already created for this mailing. //if yes then create only target contact record. $query = "\nSELECT id \nFROM civicrm_activity\nWHERE civicrm_activity.activity_type_id = %1\nAND civicrm_activity.source_record_id = %2"; $queryParams = array(1 => array($activityTypeID, 'Integer'), 2 => array($this->mailing_id, 'Integer')); $activityID = CRM_Core_DAO::singleValueQuery($query, $queryParams); if ($activityID) { $activity['id'] = $activityID; } require_once 'api/v2/Activity.php'; if (is_a(civicrm_activity_create($activity, 'Email'), 'CRM_Core_Error')) { return false; } } return true; }
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); } } }