@@ -107,6 +107,13 @@ class Net_Gearman_Connection
107107
108108 public $ socket ;
109109
110+ /**
111+ * Gearmand Server Version
112+ *
113+ * @var string
114+ */
115+ protected $ serverVersion ;
116+
110117 public function __construct ($ host =null , $ timeout =250 ) {
111118 if ($ host ) {
112119 $ this ->connect ($ host , $ timeout );
@@ -123,8 +130,8 @@ public function __destruct() {
123130 * Opens the socket to the Gearman Job server. It throws an exception if
124131 * a socket error occurs. Also populates Net_Gearman_Connection::$magic.
125132 *
126- * @param string $host e.g. 127.0.0.1 or 127.0.0.1:7003
127- * @param int $timeout Timeout in milliseconds
133+ * @param string $host e.g. 127.0.0.1 or 127.0.0.1:7003
134+ * @param int $timeout Timeout in milliseconds
128135 *
129136 * @return resource A connection to a Gearman server
130137 * @throws Net_Gearman_Exception when it can't connect to server
@@ -203,6 +210,8 @@ public function connect($host, $timeout = 250)
203210
204211 // socket_set_option($this->socket, SOL_TCP, SO_DEBUG, 1); // Debug
205212
213+ $ this ->setServerVersion ($ host );
214+
206215 } else {
207216
208217 $ errno = @socket_last_error ($ this ->socket );
@@ -259,6 +268,10 @@ public function send($command, array $params = array())
259268 }
260269 }
261270
271+ if ($ command === 'can_do_timeout ' ) {
272+ $ params = $ this ->fixTimeout ($ params );
273+ }
274+
262275 $ d = implode ("\x00" , $ data );
263276
264277 $ cmd = "\0REQ " . pack ("NN " ,
@@ -548,4 +561,30 @@ public static function subString($str, $start, $length)
548561 return substr ($ str , $ start , $ length );
549562 }
550563 }
564+
565+ /**
566+ * Sets the server version.
567+ *
568+ * @param string $host The host
569+ * @param Net_Gearman_Manager $manager Optional manager object
570+ */
571+ protected function setServerVersion ($ host , $ manager = null )
572+ {
573+ if (empty ($ manager )) {
574+ $ manager = new \Net_Gearman_Manager ($ host );
575+ }
576+ $ this ->serverVersion = $ manager ->version ();
577+ unset($ manager );
578+ }
579+
580+ protected function fixTimeout ($ params ) {
581+ // In gearmand version 1.1.19 and greater, the timeout is
582+ // expected to be in milliseconds. Before that version, it
583+ // is expected to be in seconds.
584+ // https://github.com/gearman/gearmand/issues/196
585+ if (version_compare ('1.1.18 ' , $ this ->serverVersion )) {
586+ $ params ['timeout ' ] *= 1000 ;
587+ }
588+ return $ params ;
589+ }
551590}
0 commit comments