| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Beanie; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Beanie\Server\Pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Beanie\Server\PoolFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Beanie\Server\Server; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class Beanie | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     const DEFAULT_TUBE = 'default'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     const DEFAULT_PRIORITY = 1024; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     const DEFAULT_DELAY = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     const DEFAULT_TIME_TO_RUN = 60; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     const DEFAULT_MAX_TO_KICK = 50; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** @var Pool  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param Pool $pool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 7 |  |     public function __construct(Pool $pool) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 7 |  |         $this->pool = $pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 7 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param null|string $serverName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @return Worker | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @throws Exception\InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 2 |  |     public function worker($serverName = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 2 |  |         $server = ($serverName === null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 2 |  |             ? $this->pool->getRandomServer() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 2 |  |             : $this->pool->getServer($serverName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 2 |  |         return new Worker($server); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @return Worker[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |     public function workers() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return array_map(function (Server $server) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |             return new Worker($server); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |         }, $this->pool->getServers()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @return Producer | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 1 |  |     public function producer() | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 1 |  |         return new Producer($this->pool); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param string $serverName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @return Manager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @throws Exception\InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |     public function manager($serverName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |         return $this->pool->getServer($serverName)->getManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return Manager[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public function managers() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |         return array_map(function (Server $server) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |             return $server->getManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |         }, $this->pool->getServers()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param string[] $servers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param PoolFactory|null $poolFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @return static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |     public static function pool($servers, PoolFactory $poolFactory = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |         $poolFactory = $poolFactory ?: PoolFactory::instance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |         return new static($poolFactory->create($servers)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  |