Command for creating a worker

@param string $workerCmd
@param integer $workerCount
@return string Command result
512480
protected function _workerCmd($workerCmd, $workerCount)
    {
        // Shell command builder
        $cmd = "{$workerCmd}/{$workerCount}";
        $cmd = ($this->logPath) ? "{$cmd} >> {$this->logPath}" : $cmd;

        // Process handler
        $process = proc_open("{$cmd} &", self::$_procDescriptorspec, $pipe);
        // Find out worker command's PID
        $status = proc_get_status($process);
        $pid = $status['pid'] + 1;
        // Stack workers
        $this->_pidStack[$workerCount] = $pid;
        // Close
        proc_close($process);

        // Log
        $time = date("Y-m-d H:i:s");
        $this->_log("Queue Listener - Dispatch Worker #{$workerCount} (PID: {$pid})");

        return true;
    }
58357
public static function run($cmd)
    {
        self::initEvn();

        $param = is_array($cmd) && count($cmd) == 2 ? $cmd[1] : $cmd;
        switch ($param) {
            case 'start':
                self::start();
                break;
            case 'stop':
                self::stop();
                break;
            case 'reload':
                self::reload();
                break;
            case 'status':
                self::getStatus(true);
                break;
            case 'add-task':
                if (func_num_args() < 1) {
                    self::message('please input task name');
                    break;
                }
                $args = func_get_args();
                $frequency = isset($args[2]) ? intval($args[2]) : 60;
                self::addTask($args[1], $frequency);
                break;
            case 'rm-task':
                if (func_num_args() < 1) {
                    self::message('please input task name');
                    break;
                }
                $args = func_get_args();
                self::rmTask($args[1]);
                break;
            default:
                self::message('Usage: xxx.php cml.cmd DaemonWorker::run {start|stop|status|addtask|rmtask}');
                break;
        }
    }
368756
@SuppressWarnings("rawtypes")
    public static WorkerShutdown mk_worker(Map conf, IContext context, String topologyId, String supervisorId,
                                           int port, String workerId, String jarPath) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("topologyId:").append(topologyId).append(", ");
        sb.append("port:").append(port).append(", ");
        sb.append("workerId:").append(workerId).append(", ");
        sb.append("jarPath:").append(jarPath).append("\n");

        LOG.info("Begin to run worker:" + sb.toString());
        Worker w = new Worker(conf, context, topologyId, supervisorId, port, workerId, jarPath);
        //   w.redirectOutput();

        return w.execute();
    }
89966
public function onWorkerStart($server, $workid) {

        try {

            //init yii2 application
            $this->yiiConfig = require_once($this->yiiConfigFile);

            //register service
            $dir = ROOT_DIR . $this->config['service']['path'];
            $namespace = $this->config['service']['namespace'];

            $yiiConfig = $this->yiiConfig;
            $dbConfig = require DB_CONFIG_FILE;
            $this->__set('onBeforeInvoke',function() use ($yiiConfig,$dbConfig) {
                $application = new Application($yiiConfig);
                $application->run();
            });
            $this->__set('onAfterInvoke',function() use ($dbConfig) {
                \Yii::$app->release($dbConfig);
                \Yii::$app = null;
            });

            $this->registerService($dir, $namespace, $server);
            $asyncDir = $dir . 'async' . DIRECTORY_SEPARATOR;
            $this->registerService($asyncDir, $namespace, $server, '_async');

            $worker_num = $this->config['setting']['worker_num'];

            if (!function_exists('cli_set_process_title')) {
                if ($workid >= $worker_num) {
                    swoole_set_process_name($this->config['server']['base.process.name'] . 'Task');
                } else {
                    swoole_set_process_name($this->config['server']['base.process.name'] . 'Worker');
                }
            } else {
                if ($workid >= $worker_num) {
                    cli_set_process_title($this->config['server']['base.process.name'] . 'Task');
                } else {
                    cli_set_process_title($this->config['server']['base.process.name'] . 'Worker');
                }
            }
        } catch(\Exception $ex) {
            $this->access_log('server exit.Exception = ' . $ex->getMessage());
        }
    }
286200
protected function createProcess()
    {
        $class = config('horizon.fast_termination')
                    ? BackgroundProcess::class
                    : Process::class;

        return new WorkerProcess($class::fromShellCommandline(
            $this->options->toWorkerCommand(), $this->options->directory
        )->setTimeout(null)->disableOutput());
    }
1081926
private function createCommand(Server $server)
    {
        $cmd = [
            'aggregate' => isset($this->collectionName) ? $this->collectionName : 1,
            'pipeline' => $this->pipeline,
        ];
        $cmdOptions = [];

        $cmd['allowDiskUse'] = $this->options['allowDiskUse'];

        if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) {
            $cmd['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
        }

        foreach (['comment', 'explain', 'maxTimeMS'] as $option) {
            if (isset($this->options[$option])) {
                $cmd[$option] = $this->options[$option];
            }
        }

        if (isset($this->options['collation'])) {
            $cmd['collation'] = (object) $this->options['collation'];
        }

        if (isset($this->options['hint'])) {
            $cmd['hint'] = is_array($this->options['hint']) ? (object) $this->options['hint'] : $this->options['hint'];
        }

        if (isset($this->options['maxAwaitTimeMS'])) {
            $cmdOptions['maxAwaitTimeMS'] = $this->options['maxAwaitTimeMS'];
        }

        if ($this->options['useCursor']) {
            $cmd['cursor'] = isset($this->options["batchSize"])
                ? ['batchSize' => $this->options["batchSize"]]
                : new stdClass;
        }

        return new Command($cmd, $cmdOptions);
    }
880027
public function createWorker($serverId, $siteId, array $data, $wait = true)
    {
        $worker = $this->post("servers/$serverId/sites/$siteId/workers", $data)['worker'];

        if ($wait) {
            return $this->retry($this->getTimeout(), function () use ($serverId, $siteId, $worker) {
                $worker = $this->worker($serverId, $siteId, $worker['id']);

                return $worker->status == 'installed' ? $worker : null;
            });
        }

        return new Worker($worker + ['server_id' => $serverId, 'site_id' => $siteId], $this);
    }
155425
protected function exec($cmd, $task, $args = null)
    {
        $script = __DIR__.'/../../exec/'.$cmd.'/'.$task.'.sh';
        if (!file_exists($script)) {
            die('Producer >  INTERNAL ERROR MISSING SCRIPT');
        }

        $params = '';

        if ($args && count($args) > 0) {
            foreach ($args as &$value) {
                $value = $this->escapeParam($value);
            }

            $params = implode(' ', $args);
        }

        $rawCommand = $script.' '
            .$this->escapeParam($this->cwd).' '
            .$this->escapeParam($this->projectsDir).' '.$params;

        $output = shell_exec($rawCommand);

        if (!$this->silent) {
            echo $output;
        }
    }
1056783
static function make($dataCenterID = 0, $workerID = 0)
    {
        // 41bit timestamp + 5bit dataCenter + 5bit worker + 12bit
        $timestamp = self::timeGen();
        if (self::$lastTimestamp == $timestamp) {
            self::$lastSequence = (self::$lastSequence + 1) & self::$sequenceMask;
            if (self::$lastSequence == 0) $timestamp = self::tilNextMillis(self::$lastTimestamp);
        } else {
            self::$lastSequence = 0;
        }
        self::$lastTimestamp = $timestamp;
        $snowFlakeId = (($timestamp - self::$twepoch) << 22) | ($dataCenterID << 17) | ($workerID << 12) | self::$lastSequence;
        return $snowFlakeId;
    }
642538
public void launchWorker(Map conf, IContext sharedContext, String topologyId,
                             String supervisorId, Integer port, String workerId,
                             ConcurrentHashMap<String, String> workerThreadPidsAtom) throws Exception {
        String pid = UUID.randomUUID().toString();
        WorkerShutdown worker = Worker.mk_worker(conf, sharedContext, topologyId, supervisorId, port, workerId, null);
        ProcessSimulator.registerProcess(pid, worker);
        workerThreadPidsAtom.put(workerId, pid);
    }
41252
public synchronized List<alluxio.grpc.JobCommand> pollAllPendingCommands(long workerId) {
    if (!mWorkerIdToPendingCommands.containsKey(workerId)) {
      return Lists.newArrayList();
    }
    List<JobCommand> commands =
        Lists.newArrayList(mWorkerIdToPendingCommands.get(workerId));
    mWorkerIdToPendingCommands.get(workerId).clear();
    return commands;
  }
1299858
def create_process_worker(self, cmd_list, environ=None):
        """"""
        worker = ProcessWorker(cmd_list, environ=environ)
        self._create_worker(worker)
        return worker
117025
public function actionStart($taskCommand = null )
    {
        /**
         * @var $cron Crontab
         */
        $cron = $this->module->get($this->module->nameComponent);
        $cron->eraseJobs();
        $common_params = $this->module->params;
        if(!empty($this->module->tasks)) {
            if($taskCommand && isset($this->module->tasks[$taskCommand])){

                    $task = $this->module->tasks[$taskCommand];
                    $params = ArrayHelper::merge( ArrayHelper::getValue($task, 'params',[]), $common_params );

                    $cron->addApplicationJob($this->module->phpPath.' '.$this->getYiiPath(), $task['command'],
                        $params,
                        ArrayHelper::getValue($task, 'min'),
                        ArrayHelper::getValue($task, 'hour'),
                        ArrayHelper::getValue($task, 'day'),
                        ArrayHelper::getValue($task, 'month'),
                        ArrayHelper::getValue($task, 'dayofweek'),
                        $this->module->cronGroup //mast be unique for any app on server
                    );

            } else {
                    foreach ($this->module->tasks as $commandName => $task) {

                        $params = ArrayHelper::merge( ArrayHelper::getValue($task, 'params',[]), $common_params );

                        $cron->addApplicationJob($this->module->phpPath.' '.$this->getYiiPath(), $task['command'],
                            $params,
                            ArrayHelper::getValue($task, 'min'),
                            ArrayHelper::getValue($task, 'hour'),
                            ArrayHelper::getValue($task, 'day'),
                            ArrayHelper::getValue($task, 'month'),
                            ArrayHelper::getValue($task, 'dayofweek'),
                            $this->module->cronGroup //mast be unique for any app on server
                        );
                    }
            }

            $cron->saveCronFile(); // save to my_crontab cronfile
            $cron->saveToCrontab(); // adds all my_crontab jobs to system (replacing previous my_crontab jobs)

            echo $this->ansiFormat('Cron Tasks started.' . PHP_EOL, Console::FG_GREEN);
        } else {
            echo $this->ansiFormat('Cron do not have Tasks.' . PHP_EOL, Console::FG_GREEN);
        }
    }
1219765
protected function listenToWorker(Worker $worker)
    {
        $worker->getDispatcher()
            ->listen('onWorkerBeforeJobRun', function (Event $event) {
                /**
                 * @var JobInterface $job
                 * @var QueueMessage $message
                 */
                $job     = $event['job'];
                $message = $event['message'];

                $this->console->addMessage(sprintf(
                    'Run Job: <info>%s</info> - Message ID: <info>%s</info>',
                    $job->getName(),
                    $message->getId()
                ));
            })
            ->listen('onWorkerJobFailure', function (Event $event) {
                /**
                 * @var JobInterface $job
                 * @var \Exception   $e
                 * @var QueueMessage $message
                 */
                $job     = $event['job'];
                $e       = $event['exception'];
                $message = $event['message'];

                $this->console->addMessage(sprintf(
                    'Job %s failed: %s (%s)',
                    $job->getName(),
                    $e->getMessage(),
                    $message->getId()
                ), 'error');

                // If be deleted, send to failed table
                if ($message->isDeleted()) {
                    $this->console->container->get('queue.failer')->add(
                        $this->console->get('queue.connection', 'sync'),
                        $message->getQueueName(),
                        json_encode($message),
                        (string) $e
                    );
                }

                $this->console->handleException($e);
            })
            ->listen('onWorkerLoopCycleStart', function (Event $event) {
                /** @var Worker $worker */
                $worker = $event['worker'];

                switch ($worker->getState()) {
                    case $worker::STATE_ACTIVE:
                        if ($this->console->isOffline()) {
                            $worker->setState($worker::STATE_PAUSE);
                        }
                        break;

                    case $worker::STATE_PAUSE:
                        if ($this->console->isOffline()) {
                            $worker->setState($worker::STATE_ACTIVE);
                        }
                        break;
                }
            })
            ->listen('onWorkerLoopCycleFailure', function (Event $event) {
                /** @var \Exception $e */
                $e = $event['exception'];

                $this->console->addMessage(
                    sprintf(
                        '%s File: %s (%s)',
                        $e->getMessage(),
                        $e->getFile(),
                        $e->getLine()
                    ),
                    'error'
                );

                $this->console->handleException($e);

                if ($this->console->database instanceof AbstractDatabaseDriver) {
                    $this->console->database->disconnect();
                }
            })
            ->listen('onWorkerLoopCycleEnd', function (Event $event) {
                /** @var QueueManager $manager */
                $manager = $event['manager'];

                $driver = $manager->getDriver();

                if ($driver instanceof DatabaseQueueDriver) {
                    $driver->disconnect();
                }

                if ($this->console->database instanceof AbstractDatabaseDriver) {
                    $this->console->database->disconnect();
                }
            });
    }
795152
public static function onTask($serv, $task_id, $worker_id, $data) {
		$rs = Plugin::trigger('taskStart', [$task_id, $worker_id, $data]);
		if (is_string($rs)) {
			return $rs;
		}
	}
287992
public function onWorkerStop(SwServer $server, int $workerId)
    {
        $workerType = $server->taskworker ? 'task' : 'worker';

        // {{ log
        $this->get('logger')->info('结束 Worker 进程', [
            'id' => $workerId,
            'type' => ucfirst($workerType)
        ]);
        // }}

        if ($workerType === 'worker') {
            $this->shutdownWorkerStartItems();
            $this->callProtocolMethod('onStop', $server, $workerId);
        }

        unset($workerType);
    }
848615
public static SMTPClientTransportFactory createNio(int workerCount) {
        return new NettySMTPClientTransportFactory(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), workerCount), FACTORY);
    }