@@ -14,7 +14,7 @@ |
||
| 14 | 14 | { |
| 15 | 15 | public function load(array $configs, ContainerBuilder $container) |
| 16 | 16 | { |
| 17 | - $documentName = 'Dtc\QueueBundle\Documents\Job'; |
|
| 17 | + $documentName = 'Dtc\QueueBundle\Documents\Job'; |
|
| 18 | 18 | $processor = new Processor(); |
| 19 | 19 | $configuration = new Configuration(); |
| 20 | 20 | |
@@ -7,7 +7,7 @@ |
||
| 7 | 7 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 8 | 8 | |
| 9 | 9 | class DtcQueueBundle |
| 10 | - extends Bundle |
|
| 10 | + extends Bundle |
|
| 11 | 11 | { |
| 12 | 12 | public function build(ContainerBuilder $container) |
| 13 | 13 | { |
@@ -383,19 +383,19 @@ |
||
| 383 | 383 | { |
| 384 | 384 | $this->worker = $worker; |
| 385 | 385 | } |
| 386 | - /** |
|
| 387 | - * @return the $elapsed |
|
| 388 | - */ |
|
| 389 | - public function getElapsed() |
|
| 390 | - { |
|
| 391 | - return $this->elapsed; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - /** |
|
| 395 | - * @param field_type $elapsed |
|
| 396 | - */ |
|
| 397 | - public function setElapsed($elapsed) |
|
| 398 | - { |
|
| 399 | - $this->elapsed = $elapsed; |
|
| 400 | - } |
|
| 386 | + /** |
|
| 387 | + * @return the $elapsed |
|
| 388 | + */ |
|
| 389 | + public function getElapsed() |
|
| 390 | + { |
|
| 391 | + return $this->elapsed; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + /** |
|
| 395 | + * @param field_type $elapsed |
|
| 396 | + */ |
|
| 397 | + public function setElapsed($elapsed) |
|
| 398 | + { |
|
| 399 | + $this->elapsed = $elapsed; |
|
| 400 | + } |
|
| 401 | 401 | } |
@@ -7,94 +7,94 @@ |
||
| 7 | 7 | |
| 8 | 8 | class WorkerManager |
| 9 | 9 | { |
| 10 | - protected $workers; |
|
| 11 | - protected $jobManager; |
|
| 12 | - protected $logger; |
|
| 10 | + protected $workers; |
|
| 11 | + protected $jobManager; |
|
| 12 | + protected $logger; |
|
| 13 | 13 | protected $eventDispatcher; |
| 14 | 14 | |
| 15 | - public function __construct(JobManagerInterface $jobManager, EventDispatcher $eventDispatcher, Logger $logger = null) { |
|
| 16 | - $this->workers = array(); |
|
| 17 | - $this->jobManager = $jobManager; |
|
| 18 | - $this->logger = $logger; |
|
| 15 | + public function __construct(JobManagerInterface $jobManager, EventDispatcher $eventDispatcher, Logger $logger = null) { |
|
| 16 | + $this->workers = array(); |
|
| 17 | + $this->jobManager = $jobManager; |
|
| 18 | + $this->logger = $logger; |
|
| 19 | 19 | $this->eventDispatcher = $eventDispatcher; |
| 20 | - } |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - public function addWorker(Worker $worker) { |
|
| 23 | - if ($this->logger) { |
|
| 24 | - $this->logger->debug("Added worker: {$worker->getName()}"); |
|
| 25 | - } |
|
| 22 | + public function addWorker(Worker $worker) { |
|
| 23 | + if ($this->logger) { |
|
| 24 | + $this->logger->debug("Added worker: {$worker->getName()}"); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - if (isset($this->workers[$worker->getName()])) { |
|
| 28 | - throw new \Exception("{$worker->getName()} already exists in worker manager"); |
|
| 29 | - } |
|
| 27 | + if (isset($this->workers[$worker->getName()])) { |
|
| 28 | + throw new \Exception("{$worker->getName()} already exists in worker manager"); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - $this->workers[$worker->getName()] = $worker; |
|
| 32 | - } |
|
| 31 | + $this->workers[$worker->getName()] = $worker; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - public function getWorker($name) { |
|
| 35 | - if (isset($this->workers[$name])) { |
|
| 36 | - return $this->workers[$name]; |
|
| 37 | - } |
|
| 34 | + public function getWorker($name) { |
|
| 35 | + if (isset($this->workers[$name])) { |
|
| 36 | + return $this->workers[$name]; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - return null; |
|
| 40 | - } |
|
| 39 | + return null; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - public function getWorkers() { |
|
| 43 | - return $this->workers; |
|
| 44 | - } |
|
| 42 | + public function getWorkers() { |
|
| 43 | + return $this->workers; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - public function run($workerName = null, $methodName = null, $prioritize = true) |
|
| 47 | - { |
|
| 48 | - $job = $this->jobManager->getJob($workerName, $methodName, $prioritize); |
|
| 49 | - if (!$job) { |
|
| 50 | - if ($this->logger) { |
|
| 51 | - $this->logger->debug("No job to run"); |
|
| 52 | - } |
|
| 46 | + public function run($workerName = null, $methodName = null, $prioritize = true) |
|
| 47 | + { |
|
| 48 | + $job = $this->jobManager->getJob($workerName, $methodName, $prioritize); |
|
| 49 | + if (!$job) { |
|
| 50 | + if ($this->logger) { |
|
| 51 | + $this->logger->debug("No job to run"); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - return; // no job to run |
|
| 55 | - } |
|
| 54 | + return; // no job to run |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - return $this->runJob($job); |
|
| 58 | - } |
|
| 57 | + return $this->runJob($job); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - public function runJob(Job $job) { |
|
| 60 | + public function runJob(Job $job) { |
|
| 61 | 61 | $event = new Event($job); |
| 62 | 62 | $this->eventDispatcher->dispatch(Event::PRE_JOB, $event); |
| 63 | 63 | |
| 64 | - try { |
|
| 65 | - $start = microtime(true); |
|
| 66 | - $worker = $this->getWorker($job->getWorkerName()); |
|
| 67 | - if ($this->logger) { |
|
| 68 | - $this->logger->debug("Start: {$job->getClassName()}->{$job->getMethod()}", $job->getArgs()); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - call_user_func_array(array($worker, $job->getMethod()), $job->getArgs()); |
|
| 72 | - |
|
| 73 | - // Job finshed successfuly... do we remove the job from database? |
|
| 74 | - $job->setStatus(Job::STATUS_SUCCESS); |
|
| 75 | - $job->setMessage(null); |
|
| 76 | - } |
|
| 77 | - catch (\Exception $e) { |
|
| 78 | - if ($this->logger) { |
|
| 79 | - $this->logger->debug("Failed: {$job->getClassName()}->{$job->getMethod()}\n{$e->getMessage()}"); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - $job->setStatus(Job::STATUS_ERROR); |
|
| 83 | - $job->setMessage($e->getMessage() . "\n" .$e->getTraceAsString()); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - // save Job history |
|
| 87 | - $elapsed = microtime(true) - $start; |
|
| 88 | - $job->setElapsed($elapsed); |
|
| 89 | - |
|
| 90 | - if ($this->logger) { |
|
| 91 | - $this->logger->debug("Finished: {$job->getClassName()}->{$job->getMethod()} in {$elapsed} micro-seconds"); |
|
| 92 | - $this->logger->debug("Save job history: {$job->getId()}"); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - $this->jobManager->saveHistory($job); |
|
| 64 | + try { |
|
| 65 | + $start = microtime(true); |
|
| 66 | + $worker = $this->getWorker($job->getWorkerName()); |
|
| 67 | + if ($this->logger) { |
|
| 68 | + $this->logger->debug("Start: {$job->getClassName()}->{$job->getMethod()}", $job->getArgs()); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + call_user_func_array(array($worker, $job->getMethod()), $job->getArgs()); |
|
| 72 | + |
|
| 73 | + // Job finshed successfuly... do we remove the job from database? |
|
| 74 | + $job->setStatus(Job::STATUS_SUCCESS); |
|
| 75 | + $job->setMessage(null); |
|
| 76 | + } |
|
| 77 | + catch (\Exception $e) { |
|
| 78 | + if ($this->logger) { |
|
| 79 | + $this->logger->debug("Failed: {$job->getClassName()}->{$job->getMethod()}\n{$e->getMessage()}"); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + $job->setStatus(Job::STATUS_ERROR); |
|
| 83 | + $job->setMessage($e->getMessage() . "\n" .$e->getTraceAsString()); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + // save Job history |
|
| 87 | + $elapsed = microtime(true) - $start; |
|
| 88 | + $job->setElapsed($elapsed); |
|
| 89 | + |
|
| 90 | + if ($this->logger) { |
|
| 91 | + $this->logger->debug("Finished: {$job->getClassName()}->{$job->getMethod()} in {$elapsed} micro-seconds"); |
|
| 92 | + $this->logger->debug("Save job history: {$job->getId()}"); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + $this->jobManager->saveHistory($job); |
|
| 96 | 96 | $this->eventDispatcher->dispatch(Event::POST_JOB, $event); |
| 97 | 97 | |
| 98 | - return $job; |
|
| 99 | - } |
|
| 98 | + return $job; |
|
| 99 | + } |
|
| 100 | 100 | } |
@@ -19,7 +19,7 @@ |
||
| 19 | 19 | $this->jobManager = new StaticJobManager(); |
| 20 | 20 | $this->worker = new FibonacciWorker(); |
| 21 | 21 | $this->worker->setJobManager($this->jobManager); |
| 22 | - $this->eventDispatcher = new EventDispatcher(); |
|
| 22 | + $this->eventDispatcher = new EventDispatcher(); |
|
| 23 | 23 | $this->workerManager = new WorkerManager($this->jobManager, $this->eventDispatcher); |
| 24 | 24 | } |
| 25 | 25 | |