Skip to content

Instantly share code, notes, and snippets.

@hjr3
Created September 7, 2011 22:27
Show Gist options
  • Save hjr3/1201989 to your computer and use it in GitHub Desktop.
Save hjr3/1201989 to your computer and use it in GitHub Desktop.
gearman-manager-callbacks
<?php
/**
* Manage responses from gearman workers and pass data along to the report
*/
class Status
{
protected $id;
protected $status;
protected $failures = array();
protected $successes = array();
public function __construct($id, $status)
{
$this->status = $status;
$this->id = $id;
}
/**
* Record a failed status
*
* @param GearmanTask $task
*/
public function fail($task)
{
$this->failures[$task->unique()] = $task->data();
}
/**
* Record a successful status
*
* @param GearmanTask $task
*/
public function success($task)
{
$this->successes[$task->unique()] = $task->data();
}
public function report($email)
{
$report = new Reports_Status($this->id, $this->status, $this->successes, $this->failures);
$report->report($email);
}
}
/**
* Batch create for a given id and then send a report
*
* @param mixed $job instance of GearmanJob or array
*/
function generate_status_batch($job) {
$s = new Status;
foreach ($statuses as $status) {
$workload = array(
'status' => $status,
'site_env' => SITE_ENV
);
$id = $status['id'];
$g->addTask("status", serialize($workload), null, $id);
}
$g->setCompleteCallback(array($s, 'success'));
$g->setFailCallback(array($s, 'fail'));
$g->runTasks();
$log->info("Finished status returns for {$event_id}");
$s->report($email);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment