| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SfCod\QueueBundle\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SfCod\QueueBundle\Options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SfCod\QueueBundle\Worker; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Class WorkCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Job queue worker. Use pm2 (http://pm2.keymetrics.io/) for fork command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @author Alexey Orlov <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @package SfCod\QueueBundle\Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class WorkCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var Worker | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $worker; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * WorkCommand constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param Worker $worker | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function __construct(Worker $worker) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->worker = $worker; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Configure command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->setName('job-queue:work') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             ->addOption('delay', null, InputArgument::OPTIONAL, 'Delay before getting jobs.', 3) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             ->addOption('memory', null, InputArgument::OPTIONAL, 'Maximum memory usage limit.', 128) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             ->addOption('sleep', null, InputArgument::OPTIONAL, 'Sleep time before getting new job.', 3) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             ->addOption('maxTries', null, InputArgument::OPTIONAL, 'Max tries to run job.', 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ->addOption('timeout', null, InputArgument::OPTIONAL, 'Daemon timeout.', 60) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ->addOption('connection', null, InputArgument::OPTIONAL, 'The name of the connection.', 'default') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ->addOption('queue', null, InputArgument::OPTIONAL, 'The name of the queue.', null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             ->setDescription('Run worker.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Execute command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param InputInterface $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @return int|null|void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |     public function execute(InputInterface $input, OutputInterface $output) | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |         $workerOptions = new Options( | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |             $input->getOption('delay'), | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |             $input->getOption('memory'), | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |             $input->getOption('timeout'), | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |             $input->getOption('sleep'), | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |             $input->getOption('maxTries') | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |         ); | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |         $connection = $input->getOption('connection'); | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |         $queue = $input->getOption('queue'); | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |         $this->worker->daemon($connection, $queue, $workerOptions); | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  |