Skip to content

Instantly share code, notes, and snippets.

@GendelfLugansk
Created March 4, 2015 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GendelfLugansk/9670be7563054226af3b to your computer and use it in GitHub Desktop.
Save GendelfLugansk/9670be7563054226af3b to your computer and use it in GitHub Desktop.
Controller_Cron_Gearman_Worker
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Cron_Gearman_Worker extends Controller_Cron {
public function action_run()
{
$worker = new GearmanWorker();
$worker->addServer(Kohana::$config->load('gearman.host'), Kohana::$config->load('gearman.port'));
$methods = get_class_methods($this);
if (is_array($methods))
{
$class = get_class($this);
$prefix = strtolower(substr($class, strrpos($class, '_') + 1).'_');
foreach ($methods as $method)
{
if (substr($method, 0, 8) == 'handler_')
{
echo $prefix.substr($method, 8)."\n";
$worker->addFunction($prefix.substr($method, 8), array($this, $method));
}
}
}
while ($worker->work()) {};
}
}
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Cron_Gearman_Worker_Emailer extends Controller_Cron_Gearman_Worker {
/**
* @param GearmanJob $job
*/
public function handler_send_many($job)
{
try
{
$mails = unserialize($job->workload());
foreach ($mails as $mail)
{
Email::send(
Arr::get($mail, 'to', Kohana::$config->load('email.bcc')),
Arr::get($mail, 'cc'),
Arr::get($mail, 'bcc'),
Arr::get($mail, 'from', Kohana::$config->load('email.send_from')),
Arr::get($mail, 'reply_to'),
Arr::get($mail, 'subject', ''),
Arr::get($mail, 'message', ''),
Arr::get($mail, 'html', FALSE),
Arr::get($mail, 'encoding'),
Arr::get($mail, 'attach_files', array())
);
}
} catch (Exception $e)
{
Kohana::$log->add(Log::DEBUG, 'catched exception in worker');
// Add this exception to the log
Kohana::$log->add(Log::ERROR, $e->getMessage());
$strace = Kohana_Exception::text($e)."\n--\n".$e->getTraceAsString();
Kohana::$log->add(Log::STRACE, $strace);
// Make sure the logs are written
Kohana::$log->write();
exit(255);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment