Discovers workers
@throws \InvalidArgumentException
489669
private function discoverNotifiables()
    {
        /** @var ClassMetadata[] $entities */
        $entities = $this->em->getMetadataFactory()->getAllMetadata();
        foreach ($entities as $entity) {
            $class = $entity->name;
            $annotation = $this->annotationReader->getClassAnnotation(new \ReflectionClass($class), 'Mgilet\NotificationBundle\Annotation\Notifiable');
            if ($annotation) {
                $this->notifiables[$annotation->getName()] = [
                    'class' => $entity->name,
                    'annotation' => $annotation,
                    'identifiers' => $entity->getIdentifier()
                ];
            }
        }
    }
605747
protected function load_workers() {

        $this->functions = array();

        $dirs = is_array($this->worker_dir) ? $this->worker_dir : explode(",", $this->worker_dir);

        foreach ($dirs as $dir) {

            $this->log("Loading workers in ".$dir);

            $worker_files = glob($dir."/*.php");

            if (!empty($worker_files)) {

                foreach ($worker_files as $file) {

                    $function = substr(basename($file), 0, -4);

                    /**
                     * include workers
                     */
                    if (!empty($this->config['include'])) {
                        if (!in_array($function, $this->config['include'])) {
                            continue;
                        }
                    }

                    /**
                     * exclude workers
                     */
                    if (in_array($function, $this->config['exclude'])) {
                        continue;
                    }

                    if (!isset($this->functions[$function])) {
                        $this->functions[$function] = array('name' => $function);
                    }

                    if (!empty($this->config['functions'][$function]['dedicated_only'])) {

                        if (empty($this->config['functions'][$function]['dedicated_count'])) {
                            $this->log("Invalid configuration for dedicated_count for function $function.", GearmanManager::LOG_LEVEL_PROC_INFO);
                            exit();
                        }

                        $this->functions[$function]['dedicated_only'] = true;
                        $this->functions[$function]["count"] = $this->config['functions'][$function]['dedicated_count'];

                    } else {

                        $min_count = max($this->do_all_count, 1);
                        if (!empty($this->config['functions'][$function]['count'])) {
                            $min_count = max($this->config['functions'][$function]['count'], $this->do_all_count);
                        }

                        if (!empty($this->config['functions'][$function]['dedicated_count'])) {
                            $ded_count = $this->do_all_count + $this->config['functions'][$function]['dedicated_count'];
                        } elseif (!empty($this->config["dedicated_count"])) {
                            $ded_count = $this->do_all_count + $this->config["dedicated_count"];
                        } else {
                            $ded_count = $min_count;
                        }

                        $this->functions[$function]["count"] = max($min_count, $ded_count);

                    }

                    $this->functions[$function]['path'] = $file;

                    /**
                     * Note about priority. This exploits an undocumented feature
                     * of the gearman daemon. This will only work as long as the
                     * current behavior of the daemon remains the same. It is not
                     * a defined part fo the protocol.
                     */
                    if (!empty($this->config['functions'][$function]['priority'])) {
                        $priority = max(min(
                            $this->config['functions'][$function]['priority'],
                            self::MAX_PRIORITY), self::MIN_PRIORITY);
                    } else {
                        $priority = 0;
                    }

                    $this->functions[$function]['priority'] = $priority;

                }
            }
        }
    }
1251291
private void startWorkers() {
        m_logger.info("Starting {} workers", m_config.workers);
        for (int workerNo = 1; workerNo <= m_config.workers; workerNo++) {
            LoadWorker loadWorker = new LoadWorker(workerNo);
            loadWorker.start();
            m_workerList.add(loadWorker);
        }
    }
414404
public function listen()
    {
        // Env check
        if (!$this->_isLinux()) {
            die("Error environment: Queue Listener requires Linux OS, you could use `work` or `single` instead.");
        }
        
        // Pre-work check
        if (!method_exists($this, 'handleListen'))
            throw new Exception("You need to declare `handleListen()` method in your worker controller.", 500);
        if (!method_exists($this, 'handleWork'))
            throw new Exception("You need to declare `handleWork()` method in your worker controller.", 500);
        if ($this->logPath && !file_exists($this->logPath)) {
            // Try to access or create log file
            if ($this->_log('')) {
                throw new Exception("Log file doesn't exist: `{$this->logPath}`.", 500);
            }
        }

        // INI setting
        if ($this->debug) {
            error_reporting(-1);
            ini_set('display_errors', 1);
        }
        set_time_limit(0);

        // Worker command builder
        // Be careful to avoid infinite loop by opening listener itself
        $workerAction = 'work';
        $route = $this->router->fetch_directory() . $this->router->fetch_class() . "/{$workerAction}";
        $workerCmd = "{$this->phpCommand} " . FCPATH . "index.php {$route}";

        // Static variables
        $startTime = 0;
        $workerCount = 0;
        $workingFlag = false;

        // Setting check
        $this->workerMaxNum = ($this->workerMaxNum >= 1) ? floor($this->workerMaxNum) : 1;
        $this->workerStartNum = ($this->workerStartNum <= $this->workerMaxNum) ? floor($this->workerStartNum) : $this->workerMaxNum;
        $this->workerWaitSeconds = ($this->workerWaitSeconds >= 1) ? $this->workerWaitSeconds : 10;

        while (true) {

            // Loading insurance
            sleep(0.1);
            
            // Call customized listener process, assigns works while catching true by callback return
        	$hasEvent = ($this->handleListen($this->_staticListen)) ? true : false;

            // Start works if exists
            if ($hasEvent) {  

                // First time to assign works
                if (!$workingFlag) {
                    $workingFlag = true;
                    $startTime = microtime(true);
                    $this->_log("Queue Listener - Job detect");
                    $this->_log("Queue Listener - Start dispatch");

                    if ($this->workerStartNum > 1) {
                        // Execute extra worker numbers
                        for ($i=1; $i < $this->workerStartNum ; $i++) { 
                            $workerCount ++;
                            $r = $this->_workerCmd($workerCmd, $workerCount);
                        }
                    }
                }

                // Max running worker numbers check, otherwise keeps dispatching more workers
                if ($this->workerMaxNum <= $workerCount) {

                    // Worker heath check
                    if ($this->workerHeathCheck) {
                        foreach ($this->_pidStack as $id => $pid) {
                            $isAlive = $this->_isPidAlive($pid);
                            if (!$isAlive) {
                                $this->_log("Queue Listener - Worker health check: Missing #{$id} (PID: {$pid})");
                                $r = $this->_workerCmd($workerCmd, $id);
                            }
                        }
                    }

                    sleep($this->workerWaitSeconds);
                    continue;
                }

                // Assign works
                $workerCount ++;
                // Create a worker
                $r = $this->_workerCmd($workerCmd, $workerCount);

                sleep($this->workerWaitSeconds);
                continue;
            }

            // The end of assignment (No more work), close the assignment
            if ($workingFlag) {
                $workingFlag = false;
                $workerCount = 0;
                // Clear worker stack
                $this->_pidStack = [];
                $costSeconds = number_format(microtime(true) - $startTime, 2, '.', '');
                $this->_log("Queue Listener - Job empty");
                $this->_log("Queue Listener - Stop dispatch, total cost: {$costSeconds}s");
            }
            
            // Idle
            if ($this->listenerSleep) {
                sleep($this->listenerSleep);
            }
        }
    }
325096
def ConfigureLazyWorkers(self):
    
    lazy_worker_instances = self.__GetMissingWorkers()
    if not lazy_worker_instances:
      return
    reachable_states = self.__AreInstancesReachable(lazy_worker_instances)
    reachable_instances = [t[0] for t in zip(lazy_worker_instances, 
                                             reachable_states) if t[1]]
    print 'reachable_instances: %s' % reachable_instances 
    self.__ConfigureWorkers(reachable_instances)
    return
654407
public void checkNewWorkers(Map conf) throws IOException, InterruptedException {
        Set<String> workers = new HashSet<>();
        for (Entry<String, Pair<Integer, Integer>> entry : workerIdToStartTimeAndPort.entrySet()) {
            String workerId = entry.getKey();
            int startTime = entry.getValue().getFirst();
            LocalState ls = StormConfig.worker_state(conf, workerId);
            WorkerHeartbeat whb = (WorkerHeartbeat) ls.get(Common.LS_WORKER_HEARTBEAT);
            if (whb == null) {
                if ((TimeUtils.current_time_secs() - startTime) <
                        JStormUtils.parseInt(conf.get(Config.SUPERVISOR_WORKER_START_TIMEOUT_SECS))) {
                    LOG.info(workerId + " still hasn't started");
                } else {
                    LOG.error("Failed to start Worker " + workerId);
                    workers.add(workerId);
                }
            } else {
                LOG.info("Successfully started worker " + workerId);
                workers.add(workerId);
            }
        }
        for (String workerId : workers) {
            Integer port = this.workerIdToStartTimeAndPort.get(workerId).getSecond();
            this.workerIdToStartTimeAndPort.remove(workerId);
            this.portToWorkerId.remove(port);
        }
    }
35110
public function discover()
    {
        if (!$this->connectionFactory) {
            throw new ClientException('Discovery requires a connection factory');
        }

        RETRY_FETCH: {
            try {
                if ($connection = $this->getMaster()) {
                    $this->discoverFromMaster($connection, $this->connectionFactory);
                } elseif ($connection = $this->pickSlave()) {
                    $this->discoverFromSlave($connection, $this->connectionFactory);
                } else {
                    throw new ClientException('No connection available for discovery');
                }
            } catch (ConnectionException $exception) {
                $this->remove($connection);
                goto RETRY_FETCH;
            }
        }
    }
156414
public function registerWorker()
    {
        Resque::redis()->sadd('workers', $this);
        Resque::redis()->set('worker:' . (string)$this . ':started', strftime('%a %b %d %H:%M:%S %Z %Y'));
    }
509593
public function worker($worker, $params = array(), $method_name = "", $data = array())
    {
        //Check if this worker has been included
        if (!is_array($worker)) {
            $worker = array($worker);
        }
        $helper = "";
        foreach ($worker as $helper) {

            $helper = ucfirst($helper);
            $file = configItem('site')['workerdir'] . $helper .'/'. $helper . '.php';

            $class = ucfirst(basename($file, '.php'));

            if (!file_exists($file)) {

                $this->logger->error("The worker $file was not found");

            }
            $this->loading[] = 'worker';
            $helper = $this->$class = $this->_createInstance('Eaglehorn\worker\\'.$helper.'\\', $class, $params, $method_name, $data);
        }

        return $helper;
    }
1225395
protected static function initId()
    {
        foreach (self::$_workers as $worker_id => $worker) {
            $new_id_map = array();
            for($key = 0; $key < $worker->count; $key++) {
                $new_id_map[$key] = isset(self::$_idMap[$worker_id][$key]) ? self::$_idMap[$worker_id][$key] : 0;
            }
            self::$_idMap[$worker_id] = $new_id_map;
        }
    }
676617
public function restart()
    {
        $workersArgs = $this->ResqueStatus->getWorkers();

        $this->params['all'] = true;
        $this->stop();

        $this->out('<info>' . __d('cake_resque', 'Restarting workers') . '</info>');
        if (!empty($workersArgs)) {
            $debug = $this->params['debug'];
            $this->debug(__d('cake_resque', 'Found ' . count($workersArgs) . ' workers to restart'));

            foreach ($workersArgs as $workerArgs) {
                $workerArgs['debug'] = $debug;
                if (isset($workerArgs['type']) && $workerArgs['type'] === 'scheduler') {
                    $this->startScheduler($workerArgs);
                } else {
                    $this->start($workerArgs);
                }
            }
            $this->out('');
        } else {
            $this->out('<warning>' . __d('cake_resque', 'No active workers found, will start brand new worker') . '</warning>');
            $this->start();
        }
    }
599696
def start_workers(self):
        """"""

        if self._started:
            raise InternalError("The method start() was called twice on a BaseRunnable object.")

        self._started = True

        for worker in self._workers:
            worker.start()
148549
protected function spawnInitialWorkers()
    {
        $this->logger->debug('spawn initial ['.$this->min_children.'] workers', [
            'category' => get_class($this),
        ]);

        if (self::PM_DYNAMIC === $this->pm || self::PM_STATIC === $this->pm) {
            for ($i = $this->count(); $i < $this->min_children; ++$i) {
                $this->spawnWorker();
            }
        }
    }
1197716
private function checkTask()
    {
        $settings  = App::getAppProperties()->get('server');
        $settings  = $settings['setting'];
        $collector = SwooleListenerCollector::getCollector();

        $isConfigTask  = isset($settings['task_worker_num']) && $settings['task_worker_num'] > 0;
        $isInstallTask = isset($collector[SwooleEvent::TYPE_SERVER][SwooleEvent::ON_TASK]) && isset($collector[SwooleEvent::TYPE_SERVER][SwooleEvent::ON_FINISH]);

        if ($isConfigTask && !$isInstallTask) {
            throw new InvalidArgumentException("Please 'composer require swoft/task' or set task_worker_num=0 !");
        }

        if (!$isConfigTask && $isInstallTask) {
            throw new InvalidArgumentException('Please set task_worker_num > 0 !');
        }
    }
1274897
protected function gatherWorkerOptions()
    {
        return new WorkerOptions(
            $this->option('delay'), $this->option('memory'),
            $this->option('timeout'), $this->option('sleep'),
            $this->option('tries'), $this->option('force'),
            $this->option('stop-when-empty')
        );
    }
557728
private function initTransports()
    {
        $this->transports = array();

        foreach ($this->loadTransports() as $transport) {
            if (!$transport instanceof TransportInterface) {
                throw new \InvalidArgumentException(sprintf('Transport %s should implements TransportInterface', get_class($transport)));
            }

            $this->transports[$transport->getName()] = $transport;
        }
    }
596631
protected static function initId()
    {
        foreach (static::$_workers as $worker_id => $worker) {
            $new_id_map = array();
            $worker->count = $worker->count <= 0 ? 1 : $worker->count;
            for($key = 0; $key < $worker->count; $key++) {
                $new_id_map[$key] = isset(static::$_idMap[$worker_id][$key]) ? static::$_idMap[$worker_id][$key] : 0;
            }
            static::$_idMap[$worker_id] = $new_id_map;
        }
    }