public function initCallbacks() { $this->client->setStatusCallback([$this, 'handleStatus']); $this->client->setDataCallback([$this, 'handleData']); $this->client->setCompleteCallback([$this, 'handleComplete']); $this->client->setFailCallback([$this, 'handleFail']); $this->client->setWarningCallback([$this, 'handleWarning']); return null; }
/** * Assign all GearmanClient callbacks as Symfony2 events * * @param \GearmanClient $gearmanClient Gearman client * * @return GearmanCallbacksDispatcher self Object */ public function assignTaskCallbacks(\GearmanClient $gearmanClient) { $gearmanClient->setCompleteCallback(array($this, 'assignCompleteCallback')); $gearmanClient->setFailCallback(array($this, 'assignFailCallback')); $gearmanClient->setDataCallback(array($this, 'assignDataCallback')); $gearmanClient->setCreatedCallback(array($this, 'assignCreatedCallback')); $gearmanClient->setExceptionCallback(array($this, 'assignExceptionCallback')); $gearmanClient->setStatusCallback(array($this, 'assignStatusCallback')); $gearmanClient->setWarningCallback(array($this, 'assignWarningCallback')); $gearmanClient->setWorkloadCallback(array($this, 'assignWorkloadCallback')); }
* * Copyright (C) 2008 James M. Luedke (jluedke@jamesluedke.com) * Eric Day (eday@oddments.org) * All rights reserved. * * Use and distribution licensed under the PHP license. See * the LICENSE file in this directory for full text. */ /* create our object */ $gmc = new GearmanClient(); /* add the default server */ $gmc->addServer(); /* set a few callbacks */ $gmc->setCreatedCallback("thumb_created"); $gmc->setCompleteCallback("thumb_complete"); $gmc->setFailCallback("thumb_fail"); for ($x = 0; $x < 20; $x++) { $data[$x]['src'] = $_SERVER['argv'][1]; $data[$x]['dest'] = "{$x}.jpg"; $data[$x]['x'] = (80 + 1) * ($x + 1); $data[$x]['y'] = NULL; } /* fire off each job */ foreach ($data as $img) { /* NOTE: if you want to asynchronously queue jobs use ** $task= $gmc->add_task_background("shrink_image", serialize($img)); ** however keep in mind that your complete callback will not get called */ if (!$gmc->addTask("shrink_image", serialize($img))) { echo "ERROR RET: " . $gmc->error() . "\n"; exit; }
/** * 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; } }
<?php $gmc = new GearmanClient(); $gmc->addServer(); $gmc->setCreatedCallback("reverse_created"); $gmc->setStatusCallback("reverse_status"); $gmc->setCompleteCallback("reverse_complete"); $gmc->setFailCallback("reverse_fail"); $task = $gmc->addTask("reverse", "this is a test", NULL); if (!$gmc->runTasks()) { echo "ERROR " . $gmc->error() . "\n"; exit; } echo "DONE\n"; function reverse_created($task) { echo "CREATED: " . $task->jobHandle() . "\n"; } function reverse_status($task) { echo "STATUS: " . $task->jobHandle() . " - " . $task->taskNumerator() . "/" . $task->taskDenominator() . "\n"; } function reverse_complete($task) { echo "COMPLETE: " . $task->jobHandle() . "\n"; } function reverse_fail($task) { echo "FAILED: " . $task->jobHandle() . "\n"; }
/** * Constructor method checks for gearman * * @throws Kohana_Request_Exception */ public function __construct() { if (!extension_loaded('gearman')) { throw new Kohana_Request_Exception('Unable to load PHP Gearman. Check your local environment.'); } // Create a new Gearman client and add a server $this->_gearman_client = new GearmanClient(); /** * @todo support multiple Gearman servers * @todo support customisable servers */ if (!$this->_gearman_client->addServer()) { throw new Kohana_Request_Exception('Unable to attach to Gearman server'); } // Setup callback functions for Gearman tasks $this->_gearman_client->setDataCallback(array($this, '_task_data')); $this->_gearman_client->setFailCallback(array($this, '_task_failed')); $this->_gearman_client->setCompleteCallback(array($this, '_task_complete')); $this->_gearman_client->setExceptionCallback(array($this, '_task_exception')); }