123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace Mmoreramerino\GearmanBundle\Module;
- use Mmoreramerino\GearmanBundle\Driver\Gearman\Job;
- use Mmoreramerino\GearmanBundle\Driver\Gearman\Work;
- use Symfony\Component\DependencyInjection\ContainerAware;
- use Mmoreramerino\GearmanBundle\Exceptions\SettingValueMissingException;
- use Mmoreramerino\GearmanBundle\Exceptions\SettingValueBadFormatException;
- /**
- * Job class
- *
- * @author Marc Morera <marc@ulabox.com>
- */
- class JobClass extends ContainerAware
- {
- /**
- * Callable name for this job
- * If is setted on annotations, this value will be used
- * otherwise, natural method name will be used.
- *
- * @var string
- */
- private $callableName;
- /**
- * Description of Job
- *
- * @var string
- */
- private $description;
- /**
- * 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, Work $classAnnotation, $callableNameClass, array $settings)
- {
- $this->callableName = (null !== $methodAnnotation->name) ?
- $methodAnnotation->name :
- $method->getName();
- $this->methodName = $method->getName();
- $this->realCallableName = str_replace('\\', '', $callableNameClass.'~'.$this->callableName);
- $this->description = (null !== $methodAnnotation->description) ?
- $methodAnnotation->description :
- 'No description is defined';
- if (null !== $settings['defaults']['iterations']) {
- $iter = (int) ($settings['defaults']['iterations']);
- if (null !== $classAnnotation->iterations) {
- $iter = (int) ($classAnnotation->iterations);
- }
- if (null !== $methodAnnotation->iterations) {
- $iter = (int) ($methodAnnotation->iterations);
- }
- } else {
- throw new SettingValueMissingException('defaults/iterations');
- }
- $this->iterations = $iter;
- if (null !== $settings['defaults']['method']) {
- $defaultMethod = ($settings['defaults']['method']);
- if (null !== $classAnnotation->defaultMethod) {
- $defaultMethod = ($classAnnotation->defaultMethod);
- }
- if (null !== $methodAnnotation->defaultMethod) {
- $defaultMethod = ($methodAnnotation->defaultMethod);
- }
- } else {
- throw new SettingValueMissingException('defaults/method');
- }
- $this->defaultMethod = $defaultMethod;
- /**
- * Servers definition for job
- */
- $servers = array();
- if (null !== $settings['defaults']['servers']) {
- if (is_array($settings['defaults']['servers'])) {
- foreach ($settings['defaults']['servers'] as $name => $server) {
- $servername = $server['hostname'].':'.(int) ($server['port']);
- $servers[$name] = $servername;
- }
- } else {
- throw new SettingValueBadFormatException('servers');
- }
- if (null !== $classAnnotation->servers) {
- if (is_array($classAnnotation->servers)) {
- $servers = $classAnnotation->servers;
- } else {
- $servers = array($classAnnotation->servers);
- }
- }
- if (null !== $methodAnnotation->servers) {
- if (is_array($methodAnnotation->servers)) {
- $servers = $methodAnnotation->servers;
- } else {
- $servers = array($methodAnnotation->servers);
- }
- }
- } else {
- throw new SettingValueMissingException('defaults/servers');
- }
- $this->servers = $servers;
- }
- /**
- * Retrieve all Job data in cache format
- *
- * @return array
- */
- public function __toCache()
- {
- return array(
- 'callableName' => $this->callableName,
- 'methodName' => $this->methodName,
- 'realCallableName' => $this->realCallableName,
- 'description' => $this->description,
- 'iterations' => $this->iterations,
- 'servers' => $this->servers,
- 'defaultMethod' => $this->defaultMethod,
- );
- }
- }
|