/** * Notify the Client * * @param int $type one of NOTIFY_* * @param string $data * @return bool */ public function notify($type, $data = null) { if ($type == JobInterface::NOTIFY_COMPLETE) { return $this->job->sendComplete($data); } if ($type == JobInterface::NOTIFY_DATA) { return $this->job->sendData($data); } if ($type == JobInterface::NOTIFY_EXCEPTION) { return $this->job->sendException($data); } if ($type == JobInterface::NOTIFY_FAIL) { return $this->job->sendFail(); } if ($type == JobInterface::NOTIFY_WARNING) { return $this->job->notifyWarning($data); } return false; }
/** * Functions registered with the worker * * @param GearmanJob $job * @return boolean */ function send_email($job) { //Get the info of the job $workload = unserialize($job->workload()); //Ensure the minimum info if (!array_key_exists('text', $workload) && !array_key_exists('html', $workload)) { echo sprintf("%s: To send an email we need at least the text or html\n", date('r')); $job->sendFail(); return FALSE; } if (!array_key_exists('to', $workload) || array_key_exists('to', $workload) && empty($workload['to'])) { echo sprintf("%s: To send an email we need the recipient address\n", date('r')); $job->sendFail(); return FALSE; } if (!array_key_exists('subject', $workload)) { echo sprintf("%s: To send an email we need the subject of the email\n", date('r')); $job->sendFail(); return FALSE; } echo sprintf("%s: Received a task to send email to %s\n", date('r'), implode(', ', is_array($workload['to']) ? $workload['to'] : array($workload['to']))); $config = getConfig(); $mail = new Zend_Mail('utf-8'); if ($config->system->email_system->send_by_amazon_ses) { $transport = new App_Mail_Transport_AmazonSES(array('accessKey' => $config->amazon->aws_access_key, 'privateKey' => $config->amazon->aws_private_key)); } if (array_key_exists('text', $workload)) { $mail->setBodyText($workload['text']); } if (array_key_exists('html', $workload)) { $mail->setBodyHtml($workload['html']); } if (array_key_exists('reply', $workload) && !empty($workload['reply'])) { $mail->setReplyTo($workload['reply']); } $mail->setFrom($config->amazon->ses->from_address, $config->amazon->ses->from_name); $mail->addTo($workload['to']); $mail->setSubject($workload['subject']); //Prepare gearman client $config = getConfig(); $gearmanClient = new GearmanClient(); if (!empty($config->gearman->servers)) { $gearmanClient->addServers($config->gearman->servers->toArray()); } else { $gearmanClient->addServer(); } //Add the callbacks $gearmanClient->setCompleteCallback('taskCompleted'); $gearmanClient->setFailCallback('taskFailed'); try { if (isset($transport) && $transport instanceof App_Mail_Transport_AmazonSES) { $mail->send($transport); } else { $mail->send(); } //Some status info echo sprintf("%s: Email (%s) sent to %s\n", date('r'), $workload['subject'], implode(', ', is_array($workload['to']) ? $workload['to'] : array($workload['to']))); echo sprintf("%s: Task finished successfully\n\n", date('r')); $job->sendComplete(TRUE); return TRUE; } catch (Exception $e) { logError(sprintf("Error while sending an email to %s.\n\nError: %s\n", $workload['to'], $e->getMessage())); $job->sendFail(); return FALSE; } }