123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- /**
- * Gearman Bundle for Symfony2
- *
- * @author Marc Morera <yuhu@mmoreram.com>
- * @since 2013
- */
- namespace Mmoreram\GearmanBundle\Module;
- use Mmoreram\GearmanBundle\Driver\Gearman\Job;
- use Mmoreram\GearmanBundle\Driver\Gearman\Work;
- use Symfony\Component\DependencyInjection\ContainerAware;
- use ReflectionMethod;
- /**
- * Job class
- *
- * This class provide all worker definition.
- */
- class JobClass extends ContainerAware
- {
-
- /**
- * @var string
- *
- * Callable name for this job
- * If is setted on annotations, this value will be used
- * otherwise, natural method name will be used.
- */
- private $callableName;
-
- /**
- * @var string
- *
- * Method name
- */
- private $methodName;
- /**
- * @var string
- *
- * RealCallable name for this job
- * natural method name will be used.
- */
- private $realCallableName;
- /**
- * @var string
- *
- * Description of Job
- */
- private $description;
- /**
- * @var integer
- *
- * Number of iterations this job will be alive before die
- */
- private $iterations;
- /**
- * @var string
- *
- * Default method this job will be call into Gearman client
- */
- private $defaultMethod;
- /**
- * @var array
- *
- * Collection of servers to connect
- */
- private $servers;
- /**
- * Construct method
- *
- * @param Job $methodAnnotation MethodAnnotation class
- * @param ReflectionMethod $method ReflextionMethod class
- * @param Work $classAnnotation Work class
- * @param string $callableNameClass Callable name class
- * @param array $settings Settings structure
- */
- public function __construct( Job $methodAnnotation, ReflectionMethod $method, $callableNameClass, array $servers, array $defaultSettings)
- {
- $this->callableName = is_null($methodAnnotation->name)
- ? $method->getName()
- : $methodAnnotation->name;
- $this->methodName = $method->getName();
- $this->realCallableName = str_replace('\\', '', $callableNameClass . '~' . $this->callableName);
- $this->description = is_null($methodAnnotation->description)
- ? 'No description is defined'
- : $methodAnnotation->description;
- $this->iterations = is_null($methodAnnotation->iterations)
- ? (int) $defaultSettings['iterations']
- : $methodAnnotation->iterations;
- $this->defaultMethod = is_null($methodAnnotation->defaultMethod)
- ? $defaultSettings['method']
- : $methodAnnotation->defaultMethod;
-
- /**
- * By default, this worker takes default servers definition
- */
- $this->servers = $servers;
- /**
- * If is configured some servers definition in the worker, overwrites
- */
- if ($methodAnnotation->servers) {
- if (is_array($methodAnnotation->servers)) {
- $this->servers = $methodAnnotation->servers;
- } else {
- $this->servers = array($methodAnnotation->servers);
- }
- }
- }
- /**
- * Retrieve all Job data in cache format
- *
- * @return array
- */
- public function toArray()
- {
- return array(
- 'callableName' => $this->callableName,
- 'methodName' => $this->methodName,
- 'realCallableName' => $this->realCallableName,
- 'description' => $this->description,
- 'iterations' => $this->iterations,
- 'servers' => $this->servers,
- 'defaultMethod' => $this->defaultMethod,
- );
- }
- }
|