Gearman PHP Extension: Dead Job Server = Slow Response from all Workers

笑着哭i 提交于 2019-12-06 05:33:19

So ultimately it appears that I'm not the only one with this issue. No one on the Google groups for Gearman could point to a solution either. So ultimately I wrote my own code (taking pieces from Gearman Monitor) to determine which job servers were up and running and which weren't.

try {
            $cxn = @fsockopen($ip, $gHosts->ports[$host], $errCode, $errMsg, $timeout);

            /* Using the new \Net_Gearman_Manager on a dead job server kept leading to
             *  fatal error which was uncaught. Thus crashing the script and leading
             *  no update of the server status
            */
            //$gearmanManager = new \Net_Gearman_Manager($ip . ':' . $gHosts->ports[$host], 1);

            if ($cxn === FALSE) {
                write_log($fLog, 'Connection FAILED');
                $output[$host] = FAILURE;
            } else {
                write_log($fLog, 'Connection Succeeded');
                $output[$host] = SUCCESS;
            }
        } catch (Net_Gearman_Exception $e) {
            write_log($fLog, $e->getMessage());
            $output[$host] = FAILURE;
        } catch (Exception $e) {
            write_log($fLog, $e->getMessage());
            $output[$host] = FAILURE;
        } // if (@$wrkr->addServer($ip, $gHosts->ports[$host]))

The $gHosts class is a configuration class that holds the IPs and Ports for each of my potential Gearman job servers. I spin through each potential job server in $gHosts and test it.

I then write the output from this to memcache and a text file. The memcache alone worked fine until I started really trying to load the machine. Then the memcache connection would repeatedly fail. Now I use the text file as a backup and the problems have disappeared.

I store the last attempt to connect to each Gearman Job Server in an array where the key is the server's name and the value is the time stamp of the last attempt. If the attempt was successful the time stamp is positive. If the attempt failed the time stamp is negative. The time stamps allow me to determine if the data is stale or fresh.

Then in the scripts that use Gearman I have a Client and Worker wrapper class around the PHP extension classes. They handle updating the connections on the time frame I want automatically. That way Gearman Job Servers that stop responding stop being used and the script, while potentially slow for a short period of time, typically runs quite fast.

Hope this helps someone out there.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!