@@ -3,14 +3,9 @@ |
||
3 | 3 | |
4 | 4 | use Dtc\QueueBundle\RabbitMQ\JobManager; |
5 | 5 | use Dtc\QueueBundle\Model\Job; |
6 | -use Dtc\QueueBundle\Model\WorkerManager; |
|
7 | - |
|
8 | 6 | use Dtc\QueueBundle\Tests\FibonacciWorker; |
9 | -use Dtc\QueueBundle\Tests\StaticJobManager; |
|
10 | 7 | use Dtc\QueueBundle\Tests\Model\BaseJobManagerTest; |
11 | - |
|
12 | 8 | use PhpAmqpLib\Connection\AMQPConnection; |
13 | -use PhpAmqpLib\Message\AMQPMessage; |
|
14 | 9 | |
15 | 10 | /** |
16 | 11 | * @author David |
@@ -57,8 +57,7 @@ |
||
57 | 57 | if ($job->getStatus() === BaseJob::STATUS_SUCCESS) { |
58 | 58 | $this->beanstalkd |
59 | 59 | ->delete($job); |
60 | - } |
|
61 | - else { |
|
60 | + } else { |
|
62 | 61 | $this->beanstalkd |
63 | 62 | ->bury($job); |
64 | 63 | } |
@@ -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 | { |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | |
16 | 16 | public function hasListeners($eventName) |
17 | 17 | { |
18 | - if ( ! isset($this->listeners[$eventName])) { |
|
18 | + if (!isset($this->listeners[$eventName])) { |
|
19 | 19 | return false; |
20 | 20 | } |
21 | 21 | |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | |
25 | 25 | public function dispatch($eventName, Event $event) |
26 | 26 | { |
27 | - if ( ! isset($this->listeners[$eventName])) { |
|
27 | + if (!isset($this->listeners[$eventName])) { |
|
28 | 28 | return; |
29 | 29 | } |
30 | 30 |
@@ -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 | } |
@@ -259,8 +259,7 @@ |
||
259 | 259 | } |
260 | 260 | |
261 | 261 | return true; |
262 | - } |
|
263 | - else { |
|
262 | + } else { |
|
264 | 263 | return !is_object($args); |
265 | 264 | } |
266 | 265 | } |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | } |
128 | 128 | |
129 | 129 | /** |
130 | - * @return the $className |
|
130 | + * @return string $className |
|
131 | 131 | */ |
132 | 132 | public function getClassName() |
133 | 133 | { |
@@ -298,7 +298,7 @@ discard block |
||
298 | 298 | } |
299 | 299 | |
300 | 300 | /** |
301 | - * @param field_type $createdAt |
|
301 | + * @param \DateTime $createdAt |
|
302 | 302 | */ |
303 | 303 | public function setCreatedAt($createdAt) |
304 | 304 | { |
@@ -306,7 +306,7 @@ discard block |
||
306 | 306 | } |
307 | 307 | |
308 | 308 | /** |
309 | - * @param field_type $updatedAt |
|
309 | + * @param \DateTime $updatedAt |
|
310 | 310 | */ |
311 | 311 | public function setUpdatedAt($updatedAt) |
312 | 312 | { |
@@ -361,7 +361,7 @@ discard block |
||
361 | 361 | } |
362 | 362 | |
363 | 363 | /** |
364 | - * @return the $worker |
|
364 | + * @return null|Worker $worker |
|
365 | 365 | */ |
366 | 366 | public function getWorker() |
367 | 367 | { |
@@ -392,7 +392,7 @@ discard block |
||
392 | 392 | } |
393 | 393 | |
394 | 394 | /** |
395 | - * @param field_type $elapsed |
|
395 | + * @param double $elapsed |
|
396 | 396 | */ |
397 | 397 | public function setElapsed($elapsed) |
398 | 398 | { |
@@ -1,8 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | namespace Dtc\QueueBundle\Model; |
3 | 3 | |
4 | -use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; |
|
5 | - |
|
6 | 4 | class Job |
7 | 5 | { |
8 | 6 | const STATUS_SUCCESS = 'success'; |
@@ -45,8 +45,7 @@ |
||
45 | 45 | if ($time) { |
46 | 46 | $dateTime = new \DateTime(); |
47 | 47 | $dateTime->setTimestamp($time); |
48 | - } |
|
49 | - else { |
|
48 | + } else { |
|
50 | 49 | $dateTime = null; |
51 | 50 | } |
52 | 51 |
@@ -53,6 +53,9 @@ discard block |
||
53 | 53 | return new $this->jobClass($this, $batch, $priority, $dateTime); |
54 | 54 | } |
55 | 55 | |
56 | + /** |
|
57 | + * @param integer $priority |
|
58 | + */ |
|
56 | 59 | public function later($delay = 0, $priority = null) |
57 | 60 | { |
58 | 61 | $job = $this->at(time() + $delay, false, $priority); |
@@ -60,6 +63,9 @@ discard block |
||
60 | 63 | return $job; |
61 | 64 | } |
62 | 65 | |
66 | + /** |
|
67 | + * @param integer $priority |
|
68 | + */ |
|
63 | 69 | public function batchLater($delay = 0, $priority = null) |
64 | 70 | { |
65 | 71 | $job = $this->at($delay, true, $priority); |
@@ -67,6 +73,10 @@ discard block |
||
67 | 73 | return $job; |
68 | 74 | } |
69 | 75 | |
76 | + /** |
|
77 | + * @param integer $time |
|
78 | + * @param integer $priority |
|
79 | + */ |
|
70 | 80 | public function batchAt($time = null, $priority = null) |
71 | 81 | { |
72 | 82 | return $this->at($time, true, $priority); |
@@ -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 | } |
@@ -73,8 +73,7 @@ |
||
73 | 73 | // Job finshed successfuly... do we remove the job from database? |
74 | 74 | $job->setStatus(Job::STATUS_SUCCESS); |
75 | 75 | $job->setMessage(null); |
76 | - } |
|
77 | - catch (\Exception $e) { |
|
76 | + } catch (\Exception $e) { |
|
78 | 77 | if ($this->logger) { |
79 | 78 | $this->logger->debug("Failed: {$job->getClassName()}->{$job->getMethod()}\n{$e->getMessage()}"); |
80 | 79 | } |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | $this->logger->debug("No job to run"); |
52 | 52 | } |
53 | 53 | |
54 | - return; // no job to run |
|
54 | + return; // no job to run |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | return $this->runJob($job); |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | } |
81 | 81 | |
82 | 82 | $job->setStatus(Job::STATUS_ERROR); |
83 | - $job->setMessage($e->getMessage() . "\n" .$e->getTraceAsString()); |
|
83 | + $job->setMessage($e->getMessage()."\n".$e->getTraceAsString()); |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | // save Job history |
@@ -20,7 +20,7 @@ |
||
20 | 20 | |
21 | 21 | public function fibonacci($n) |
22 | 22 | { |
23 | - if($n == 0) |
|
23 | + if ($n == 0) |
|
24 | 24 | return 0; //F0 |
25 | 25 | elseif ($n == 1) |
26 | 26 | return 1; //F1 |
@@ -20,12 +20,17 @@ |
||
20 | 20 | |
21 | 21 | public function fibonacci($n) |
22 | 22 | { |
23 | - if($n == 0) |
|
24 | - return 0; //F0 |
|
25 | - elseif ($n == 1) |
|
26 | - return 1; //F1 |
|
27 | - else |
|
28 | - return $this->fibonacci($n - 1) + $this->fibonacci($n - 2); |
|
23 | + if($n == 0) { |
|
24 | + return 0; |
|
25 | + } |
|
26 | + //F0 |
|
27 | + elseif ($n == 1) { |
|
28 | + return 1; |
|
29 | + } |
|
30 | + //F1 |
|
31 | + else { |
|
32 | + return $this->fibonacci($n - 1) + $this->fibonacci($n - 2); |
|
33 | + } |
|
29 | 34 | } |
30 | 35 | |
31 | 36 | public function getName() { |