PHP encapsulation Class Based on gearman

Source: Internet
Author: User

When there are multiple jobs servers, PHP's gearman extension will automatically detect when the jobs server port is disconnected, so that it can automatically switch to another one; but when the IP address is unavailable, an error occurs.

This encapsulation solves several problems:

1. If the IP address of the jobs server is suddenly inaccessible (for example, if the server is shut down), the worker automatically adds the jobs server again (an error is reported by default and the worker ends );

2. Add only valid servers when adding the jobs server (if the server with an IP address not available is added, an error will be reported later );

PS: The following function is used to detect valid jobs server. The crude method is P.

/*** Worker class */class GW {private $ enabled = false; private $ worker = NULL; private $ Config = array (); Private $ task = array (); // registered task information/*** create a worker object, add a job server * @ Param array $ config job server config * @ return void */public function _ construct ($ config) {$ this-> Config = $ config; $ this-> initworker ();} /*** register the task and processing function * @ Param string $ task_name the name of the processing function corresponding to the task to be registered * @ Param string $ fun_name * @ Return Boolean */Public Function regtask ($ task_name, $ fun_name) {If (! $ This-> enabled) {$ ret = false;} else {// register and add callback function $ ret = $ this-> worker-> addfunction ($ task_name, $ fun_name); if ($ RET) {$ this-> task [$ task_name] = $ fun_name;} return $ ret ;} /*** run worker * @ return void/Boolean */public function run () {If (! $ This-> enabled) {return false;} while (@ $ this-> worker-> Work () | $ this-> worker-> returncode ()! = Gearman_success) {// An error occurred while running // echo "error :". $ this-> worker-> error (). "\ n"; // echo "return_code :". $ this-> worker-> returncode (). "\ n"; // automatically restart the worker and re-register the previous task if ($ this-> worker-> returncode ()! = Gearman_success) {$ this-> enabled = false; $ this-> initworker (); $ ret = $ this-> reloadtask (); if ($ RET) {// restart successful $ this-> Run ();} else {// restart failed }}}/*** Add a valid jobs server to worker */private function initworker () {$ this-> worker = new gearmanworker (); // Add job server foreach ($ this-> config as $ value) {$ host = trim (strval ($ value ['host']); $ Port = array_key_exists ('Port', $ value )? Intval ($ value ['Port']): 4730; If (! Check_conn ($ host, $ port) {continue;} else {$ this-> worker-> addserver ($ host, $ port ); $ this-> enabled = true ;}}/ *** re-register the previous Task */private function reloadtask () {$ ret = false; foreach ($ this-> task as $ task_name => $ fun_name) {$ ret = $ this-> regtask ($ task_name, $ fun_name);} return $ ret ;} /*** whether a valid jobs server */Public Function isenabled () {return $ this-> enabled;}/*** client class */class GC {P has been added Rivate $ enabled = false; private $ client = NULL;/*** create a client object, add a job server * @ Param array $ config job server config * @ Param int $ timeout (MS) * @ return void */public function _ construct ($ config, $ timeout = 5000) {$ this-> client = new gearmanclient (); // Add job server foreach ($ config as $ value) {$ host = trim (strval ($ value ['host']); $ Port = array_key_exists ('Port', $ value )? Intval ($ value ['Port']): 4730; If (! Check_conn ($ host, $ port) {continue;} else {$ this-> client-> addserver ($ host, $ port ); $ this-> enabled = true; }}$ this-> enabled & $ this-> client-> setTimeout ($ timeout);}/*** sends a message, wait for the response * @ Param string task name * @ Param string data of the task * @ return result returned by the mixed job server */Public Function send ($ task_name, $ task_data) {If (! $ This-> enabled) {$ ret = false;} else {$ ret = $ this-> client-> do ($ task_name, strval ($ task_data ));} return $ ret;}/*** whether a valid jobs server */Public Function isenabled () {return $ this-> enabled;} has been added ;}} /*** network detection ** @ Param string machine IP Address * @ Param string machine port * @ return bool */function check_conn ($ IP, $ Port = 4730) {// socket connection test, 0.2 ms timeout @ $ fp = fsockopen ($ IP, $ port, $ errno, $ errstr,); if ($ FP) {$ FP & fclose ($ FP); Return true ;}else {return false ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.