@@ -57,6 +57,13 @@ class Net_Gearman_Client
5757 */
5858 protected $ timeout = 1000 ;
5959
60+ /**
61+ * Callbacks array for receiving connection status
62+ *
63+ * @var array $callback
64+ */
65+ protected $ callback = array ();
66+
6067 /**
6168 * Constructor
6269 *
@@ -71,7 +78,7 @@ class Net_Gearman_Client
7178 */
7279 public function __construct ($ servers , $ timeout = 1000 )
7380 {
74- if (!is_array ($ servers ) && strlen ($ servers )) {
81+ if (!is_array ($ servers ) && strlen ($ servers ) > 0 ) {
7582 $ servers = array ($ servers );
7683 } elseif (is_array ($ servers ) && !count ($ servers )) {
7784 throw new Net_Gearman_Exception ('Invalid servers specified ' );
@@ -108,7 +115,7 @@ protected function getConnection($uniq=null, $servers=null)
108115 */
109116 $ tried_servers = array ();
110117
111- while ($ conn === null && count ($ servers )) {
118+ while ($ conn === null && count ($ servers ) > 0 ) {
112119 if (count ($ servers ) === 1 ) {
113120 $ key = key ($ servers );
114121 } elseif ($ uniq === null ) {
@@ -121,10 +128,12 @@ protected function getConnection($uniq=null, $servers=null)
121128
122129 $ tried_servers [] = $ server ;
123130
124- if (empty ($ this ->conn [$ server ])) {
125- $ conn = null ;
131+ if (empty ($ this ->conn [$ server ]) || !$ this ->conn [$ server ]->isConnected ()) {
126132
133+ $ conn = null ;
127134 $ start = microtime (true );
135+ $ e = null ;
136+
128137 try {
129138 $ conn = new Net_Gearman_Connection ($ server , $ this ->timeout );
130139 } catch (Net_Gearman_Exception $ e ) {
@@ -141,6 +150,17 @@ protected function getConnection($uniq=null, $servers=null)
141150 break ;
142151 }
143152
153+ foreach ($ this ->callback as $ callback ) {
154+ call_user_func (
155+ $ callback ,
156+ $ server ,
157+ $ conn !== null ,
158+ $ this ->timeout ,
159+ microtime (true ) - $ start ,
160+ $ e
161+ );
162+ }
163+
144164 } else {
145165 $ conn = $ this ->conn [$ server ];
146166 }
@@ -160,6 +180,22 @@ protected function getConnection($uniq=null, $servers=null)
160180 return $ conn ;
161181 }
162182
183+ /**
184+ * Attach a callback for connection status
185+ *
186+ * @param callback $callback A valid PHP callback
187+ *
188+ * @return void
189+ * @throws Net_Gearman_Exception When an invalid callback is specified.
190+ */
191+ public function attachCallback ($ callback )
192+ {
193+ if (!is_callable ($ callback )) {
194+ throw new Net_Gearman_Exception ('Invalid callback specified ' );
195+ }
196+ $ this ->callback [] = $ callback ;
197+ }
198+
163199 /**
164200 * Fire off a background task with the given arguments
165201 *
@@ -297,22 +333,26 @@ public function runSet(Net_Gearman_Set $set, $timeout = null)
297333 $ t ++;
298334 }
299335
300- $ write = null ;
301- $ except = null ;
336+ $ write = null ;
337+ $ except = null ;
302338 $ read_cons = array ();
339+
303340 foreach ($ this ->conn as $ conn ) {
304341 $ read_conns [] = $ conn ->socket ;
305342 }
343+
306344 @socket_select ($ read_conns , $ write , $ except , $ socket_timeout );
307- foreach ($ this ->conn as $ conn ) {
345+
346+ $ error_messages = [];
347+
348+ foreach ($ this ->conn as $ server => $ conn ) {
308349 $ err = socket_last_error ($ conn ->socket );
309350 // Error 11 is EAGAIN and is normal in non-blocking mode
310351 // Error 35 happens on macOS often enough to be annoying
311352 if ($ err && $ err != 11 && $ err != 35 ) {
312353 $ msg = socket_strerror ($ err );
313- socket_getpeername ($ conn ->socket , $ remote_address , $ remote_port );
314- socket_getsockname ($ conn ->socket , $ local_address , $ local_port );
315- trigger_error ("socket_select failed: ( $ err) $ msg; server: $ remote_address: $ remote_port " , E_USER_WARNING );
354+ list ($ remote_address , $ remote_port ) = explode (": " , $ server );
355+ $ error_messages [] = "socket_select failed: ( $ err) $ msg; server: $ remote_address: $ remote_port " ;
316356 }
317357 socket_clear_error ($ conn ->socket );
318358 $ resp = $ conn ->read ();
@@ -321,6 +361,10 @@ public function runSet(Net_Gearman_Set $set, $timeout = null)
321361 }
322362 }
323363
364+ // if all connections threw errors, throw an exception
365+ if (count ($ error_messages ) == count ($ this ->conn )) {
366+ throw new Net_Gearman_Exception (implode ("; " , $ error_messages ));
367+ }
324368 }
325369 }
326370
@@ -388,6 +432,8 @@ public function disconnect()
388432 $ conn ->close ();
389433 }
390434 }
435+
436+ $ this ->conn = [];
391437 }
392438
393439 /**
0 commit comments