Development

/plugins/sfGearmanPlugin/trunk/lib/sfGearmanWorkerDoctrine.class.php

You must first sign up to be able to contribute.

root/plugins/sfGearmanPlugin/trunk/lib/sfGearmanWorkerDoctrine.class.php

Revision 29482, 3.7 kB (checked in by bicou, 3 years ago)

[sfGearmanPlugin] initial import

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Rev URL
Line 
1 <?php
2
3 /**
4  * Gearman worker for doctrine jobs
5  *
6  * @uses sfGearmanWorker
7  * @package   sfGearmanPlugin
8  * @author    Benjamin VIELLARD <bicou@bicou.com>
9  * @license   The MIT License
10  * @version   SVN: $Id$
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     // configure the doctrine worker
23     if (($config = $this->getParameter('config')) !== null)
24     {
25       // use a config key from gearman.yml worker section
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       // target a specific model
35       if (($methods = $this->getParameters('methods')) !== null)
36       {
37         // --methods=insert,update,delete,...
38         $this->addFunctions($model, explode(',', $methods));
39       }
40       else
41       {
42         // use template methods defined in schema.yml
43         $this->addFunctions($model);
44       }
45     }
46     else
47     {
48       // load all models and add all methods
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       // loop
71       foreach ($method as $f)
72       {
73         $this->addFunctions($model, $f);
74       }
75     }
76     elseif ($method === null)
77     {
78       // install every event defined in schema.yml
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       // convert special event to trigger%Event%
91       if (in_array(strtolower($method), array('insert', 'update', 'delete')))
92       {
93         $method = 'trigger'.ucfirst(strtolower($method));
94       }
95
96       // attach to functions named "Model.Method"
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     // notify job event
110     $this->notifyEventJob($job);
111
112     // extract model and method from gearman function name
113     list($model, $method) = explode('.', $job->functionName());
114
115     // unserialize workload
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     // restore things
125     $object    = isset($workload['record']) ? $workload['record'] : Doctrine::getTable($model);
126     $arguments = isset($workload['arguments']) ? $workload['arguments'] : array();
127     $result    = null;
128
129     // attach gearman job to object
130     $object->setGearmanJob($job);
131
132     // call workMethod
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     // free record
145     if ($object instanceof Doctrine_Record)
146     {
147       $object->free(true);
148     }
149
150     // gearman accepts strings only
151     return sfGearman::serialize($result);
152   }
153 }
154
155
Note: See TracBrowser for help on using the browser.