| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
class sfGearmanWorkerDoctrine extends sfGearmanWorker |
|---|
| 13 |
{ |
|---|
| 14 |
|
|---|
| 15 |
* Configure doctrine worker |
|---|
| 16 |
* |
|---|
| 17 |
* @access public |
|---|
| 18 |
* @return void |
|---|
| 19 |
*/ |
|---|
| 20 |
public function configure() |
|---|
| 21 |
{ |
|---|
| 22 |
|
|---|
| 23 |
if (($config = $this->getParameter('config')) !== null) |
|---|
| 24 |
{ |
|---|
| 25 |
|
|---|
| 26 |
$configuration = sfGearman::getDoctrine($config); |
|---|
| 27 |
foreach ($configuration as $model => $methods) |
|---|
| 28 |
{ |
|---|
| 29 |
$this->addFunctions($model, $methods); |
|---|
| 30 |
} |
|---|
| 31 |
} |
|---|
| 32 |
elseif (($model = $this->getParameter('model')) !== null) |
|---|
| 33 |
{ |
|---|
| 34 |
|
|---|
| 35 |
if (($methods = $this->getParameters('methods')) !== null) |
|---|
| 36 |
{ |
|---|
| 37 |
|
|---|
| 38 |
$this->addFunctions($model, explode(',', $methods)); |
|---|
| 39 |
} |
|---|
| 40 |
else |
|---|
| 41 |
{ |
|---|
| 42 |
|
|---|
| 43 |
$this->addFunctions($model); |
|---|
| 44 |
} |
|---|
| 45 |
} |
|---|
| 46 |
else |
|---|
| 47 |
{ |
|---|
| 48 |
|
|---|
| 49 |
Doctrine::loadModels(sfConfig::get('sf_lib_dir').'/model'); |
|---|
| 50 |
foreach (array_keys(Doctrine::getLoadedModelFiles()) as $model) |
|---|
| 51 |
{ |
|---|
| 52 |
$this->addFunctions($model); |
|---|
| 53 |
} |
|---|
| 54 |
} |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
* recursive add functions to worker |
|---|
| 59 |
* loop through Gearmanable template for events if needed |
|---|
| 60 |
* |
|---|
| 61 |
* @param string $model Doctrine model name |
|---|
| 62 |
* @param mixed $method Doctrine methods |
|---|
| 63 |
* |
|---|
| 64 |
* @return void |
|---|
| 65 |
*/ |
|---|
| 66 |
public function addFunctions($model, $method = null) |
|---|
| 67 |
{ |
|---|
| 68 |
if (is_array($method)) |
|---|
| 69 |
{ |
|---|
| 70 |
|
|---|
| 71 |
foreach ($method as $f) |
|---|
| 72 |
{ |
|---|
| 73 |
$this->addFunctions($model, $f); |
|---|
| 74 |
} |
|---|
| 75 |
} |
|---|
| 76 |
elseif ($method === null) |
|---|
| 77 |
{ |
|---|
| 78 |
|
|---|
| 79 |
$table = Doctrine::getTable($model); |
|---|
| 80 |
if ($table->hasTemplate('Doctrine_Template_Gearmanable')) |
|---|
| 81 |
{ |
|---|
| 82 |
$template = $table->getTemplate('Doctrine_Template_Gearmanable'); |
|---|
| 83 |
$this->addFunctions($model, $template->getOption('events')); |
|---|
| 84 |
} |
|---|
| 85 |
} |
|---|
| 86 |
else |
|---|
| 87 |
{ |
|---|
| 88 |
$method = trim($method); |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
if (in_array(strtolower($method), array('insert', 'update', 'delete'))) |
|---|
| 92 |
{ |
|---|
| 93 |
$method = 'trigger'.ucfirst(strtolower($method)); |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
$this->addFunction($model.'.'.$method, array($this, 'handler')); |
|---|
| 98 |
} |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
* Gearman work handler |
|---|
| 103 |
* |
|---|
| 104 |
* @param GearmanJob $job The gearman job |
|---|
| 105 |
* @return string |
|---|
| 106 |
*/ |
|---|
| 107 |
public function handler($job) |
|---|
| 108 |
{ |
|---|
| 109 |
|
|---|
| 110 |
$this->notifyEventJob($job); |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
list($model, $method) = explode('.', $job->functionName()); |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
$workload = unserialize($job->workload()); |
|---|
| 117 |
if ($workload === false) |
|---|
| 118 |
{ |
|---|
| 119 |
$job->sendWarning('Unable to unserialize workload'); |
|---|
| 120 |
$job->sendFail(); |
|---|
| 121 |
return; |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
$object = isset($workload['record']) ? $workload['record'] : Doctrine::getTable($model); |
|---|
| 126 |
$arguments = isset($workload['arguments']) ? $workload['arguments'] : array(); |
|---|
| 127 |
$result = null; |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
$object->setGearmanJob($job); |
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
try |
|---|
| 134 |
{ |
|---|
| 135 |
$result = call_user_func_array(array($object, $method), $arguments); |
|---|
| 136 |
} |
|---|
| 137 |
catch(Exception $e) |
|---|
| 138 |
{ |
|---|
| 139 |
$job->sendWarning($e->getMessage()); |
|---|
| 140 |
$job->sendFail(); |
|---|
| 141 |
throw $e; |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
if ($object instanceof Doctrine_Record) |
|---|
| 146 |
{ |
|---|
| 147 |
$object->free(true); |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
return sfGearman::serialize($result); |
|---|
| 152 |
} |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
|
|---|