【问题标题】:Can't run more than 3 gearman workers on same server不能在同一台服务器上运行超过 3 个齿轮工
【发布时间】:2017-02-07 06:11:44
【问题描述】:

当我在单个服务器上运行 gearman 时,最多三个工作人员工作正常,但当我启动第四个时,一个 php 代码工作正常,但无法检测到新工作人员,也没有清除作业队列。

protected function createWorker()
{
    $this->worker = new \GearmanWorker();
    $config = $this->app->config->job_remote;
    $this->worker->addServer($config['host'], $config['port']);
    return $this->worker;
}
public function listen($eventType, $callback)
{
    if (!($this->worker instanceof \GearmanWorker)){
        $this->worker = $this->createWorker();
    }
    $this->worker->addFunction($eventType, $callback);
    return $this->worker;
}

public function doWork($worker)
{
    if (!($worker instanceof \GearmanWorker)){
        $worker = $this->createWorker();
    }
    $this->worker = $worker;
    while (1) {
        $this->worker->work();
        $this->app->log->debug($this->worker->returnCode());
        if ($this->worker->returnCode() != \GEARMAN_SUCCESS) {
            break;
        }
    }
}

首先我调用“listen”方法,然后调用“doWork”方法

客户端代码:

    protected function createClient()
    {
        $this->client = new \GearmanClient();
        $config = $this->app->config->job_remote;
        $this->client->addServer($config['host'], $config['port']);
        return $this->client;
    }

    public function addTask($eventType, array $params)
    {
    if (!($this->client instanceof \GearmanClient)){
        $this->client = $this->createClient();
    }
    // add single task in queue
    $this->client->addTaskBackground($eventType, serialize($params));
    // Run task
    $this->client->runTasks();
    }

【问题讨论】:

  • 你的代码在哪里......你尝试了什么
  • 我已经添加了代码现在检查
  • 您的客户代码在哪里???从你分配工作的地方。
  • 伙计,最多 3 名工人,一切正常。
  • 我正在使用 GearmanManager,目前正在从事该项目,我最多可以工作 50 名工人。即使我尝试了 100 名工人,它也工作得很好,只是它挂了我的电脑..

标签: php web-worker gearman


【解决方案1】:

使用 GearmanManager 您可以运行任何编号。工作人员,直到它降低您的计算机速度并使用 100% 的 cpu 和 100% 的内存。

Gearman.ini

[GearmanManager]
worker_dir=./workers
count=50
dedicated_count=1
max_worker_lifetime=3600
auto_update=1
log_file=./logs/WebAnalyzerWorker.log
max_runs_per_worker=3
timeout=600

运行 Gearman 工人

./vendor/brianlmoon/gearmanmanager/pecl-manager.php -c ./gearman.ini -vvvvv

工人阶级

<?php

include dirname(__DIR__)."/vendor/autoload.php";

use Services\Log;
use Services\ServiceInitialization;
use Lib\Technology\FindWhoisRecords;
use Illuminate\Database\Capsule\Manager as Capsule;

class DomainDetailFetchJob{

    public function beforeRun()
    {
        ServiceInitialization::loadConfig();
        ServiceInitialization::loadDatabaseConfiguration();
    }

    public function run($job, &$log)
    {
        $log[] = "Starting whois job";
        $collection = "whois";
        $this->beforeRun();
        ServiceInitialization::$config->all();        
        $workload = $job->workload();
        //workload
        $whois = new FindWhoisRecords($workload);
        if($whois->whois_result["err_code"]==0) {
            $log[] = "Whois Info fetch successful";
            //success save the details
            $whois->whois_result["result"]["workload"] = $workload;
            Capsule::table($collection)->where("workload", $workload)->update($whois->whois_result["result"], ['upsert' => true]);

        }
        else {
            $log[] = "Whois Info fetch failed";
            $logger = new Log();
            $logger->write($whois->whois_result["err_msg"], "error", "Whois Record Job");
            unset($logger);
        }
    }

}

Gearman 客户端

$client = new GearmanClient();
$client->addServer();
$client->doBackground("DomainDetailFetchJob", $url);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2021-11-05
    • 2016-04-14
    • 2011-07-24
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    相关资源
    最近更新 更多