Gearman with multiple servers and php workers

后端 未结 5 1916
误落风尘
误落风尘 2021-02-02 01:57

I\'m having a problem with gearman workers running on multiple servers which i can\'t seem to solve.

The problem occurs when a worker server is taken offline, rather tha

5条回答
  •  南笙
    南笙 (楼主)
    2021-02-02 02:47

    I use this class, which keep track of which jobs work on which servers. It hasn't been thoroughly tested, just wrote it now. I've pasted an edited version, so there might be a typo or somesuch, but otherwise appears to solve the issue.

    setTimeout(self::$timeout);
    
                    # add the default job server
                    self::$gmclient->addServer($server = self::getServer($job));
    
                    return $server;
            }
    
            static function Destroy() {
                    self::$gmclient = false;
            }
    
            static function Client($name, $vars, $timeout = false) {
                    if (is_int($timeout)) {
                            self::$timeout = $timeout;
                    } else {
                            self::$timeout = self::$defaultTimeout;
                    }
    
    
                    do {
                            $server = self::Connect(false, $name);
                            $value = self::$gmclient->do($name, $vars);
                            $return_code = self::$gmclient->returnCode();
                            if (!$value) {
                                    $error_message = self::$gmclient->error();
                                    if ($return_code == 47) {
                                            self::serverFailed($server, $name);
                                            if (count(self::$server_array) > 1) {
                                                 // ADDED SINGLE SERVER LOOP AVOIDANCE // echo "Timeout on server $server, trying another server...\n";
                                                 continue;
                                            } else {
                                                 return false;
                                            }
                                    }
                                    echo "ERR: $error_message ($return_code)\n";
                            }
                            # printf("Worker has returned\n");
                            $short_value = substr($value, 0, 80);
                            switch ($return_code)
                            {
                            case GEARMAN_WORK_DATA:
                                    echo "DATA: $short_value\n";
                                    break;
                            case GEARMAN_SUCCESS:
                                    self::serverWorked($server, $name);
                                    break;
                            case GEARMAN_WORK_STATUS:
                                    list($numerator, $denominator)= self::$gmclient->doStatus();
                                    echo "Status: $numerator/$denominator\n";
                                    break;
                            case GEARMAN_TIMEOUT:
                                    // self::Connect();
                                    // Fall through
                            default:
                                    echo "ERR: $error_message " . self::$gmclient->error() . " ($return_code)\n";
                                    break;
                            }
                    }
                    while($return_code != GEARMAN_SUCCESS);
    
                    $rv = unserialize($value);
                    return $rv["rv"];
            }
    }
    
    # Example usage:
    #    $rv = MyGearmanClient::Client("Function", $args);
    
    ?>
    

提交回复
热议问题