Skip to content

Commit 71407df

Browse files
committed
Merge branch 'master' of https://github.com/brianlmoon/net_gearman into timeout_close_refactor
2 parents 5f590d3 + 1af3d8f commit 71407df

File tree

11 files changed

+183
-1150
lines changed

11 files changed

+183
-1150
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Test files
12
tests/tests-config.php
23
tests/report*
3-
tests/run-tests.log
4+
tests/run-tests.log
5+
6+
# Dependencies
7+
vendor
8+
composer.lock

Net/Gearman/Manager.php

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,30 @@ class Net_Gearman_Manager
6060
/**
6161
* Constructor
6262
*
63-
* @param string $server Host and port (e.g. 'localhost:7003')
63+
* @param string $server Host and port (e.g. 'localhost:4730')
6464
* @param integer $timeout Connection timeout
6565
*
6666
* @throws Net_Gearman_Exception
6767
* @see Net_Gearman_Manager::$conn
6868
*/
69-
public function __construct($server, $timeout = 5)
69+
public function __construct($server = null, $timeout = 5)
7070
{
71-
if (strpos($server, ':')) {
72-
list($host, $port) = explode(':', $server);
73-
} else {
74-
$host = $server;
75-
$port = 4730;
76-
}
71+
if (func_num_args() > 0) {
72+
if (strpos($server, ':')) {
73+
list($host, $port) = explode(':', $server);
74+
} else {
75+
$host = $server;
76+
$port = 4730;
77+
}
7778

78-
$errCode = 0;
79-
$errMsg = '';
80-
$this->conn = @fsockopen($host, $port, $errCode, $errMsg, $timeout);
81-
if ($this->conn === false) {
82-
throw new Net_Gearman_Exception(
83-
'Could not connect to ' . $host . ':' . $port
84-
);
79+
$errCode = 0;
80+
$errMsg = '';
81+
$this->conn = @fsockopen($host, $port, $errCode, $errMsg, $timeout);
82+
if ($this->conn === false) {
83+
throw new Net_Gearman_Exception(
84+
'Could not connect to ' . $host . ':' . $port
85+
);
86+
}
8587
}
8688
}
8789

@@ -133,25 +135,35 @@ public function shutdown($graceful = false)
133135
public function workers()
134136
{
135137
$this->sendCommand('workers');
136-
$res = $this->recvCommand();
138+
$res = $this->recvCommand();
139+
return $this->parseWorkersResponse($res);
140+
}
141+
142+
/**
143+
* Parses a 'workers' response payload
144+
* @param string $res Response payload from a `workers` command
145+
* @return array
146+
*/
147+
public function parseWorkersResponse($res)
148+
{
137149
$workers = array();
138150
$tmp = explode("\n", $res);
139151
foreach ($tmp as $t) {
140152
if (!Net_Gearman_Connection::stringLength($t)) {
141153
continue;
142154
}
143155

144-
list($info, $abilities) = explode(':', $t);
145-
list($fd, $ip, $id) = explode(' ', trim($info));
156+
$t = trim($t);
146157

147-
$abilities = trim($abilities);
148-
149-
$workers[] = array(
150-
'fd' => $fd,
151-
'ip' => $ip,
152-
'id' => $id,
153-
'abilities' => empty($abilities) ? array() : explode(' ', $abilities)
154-
);
158+
if (preg_match("/^(.+?) (.+?) (.+?) :(.*)$/", $t, $matches)) {
159+
$abilities = trim($matches[4]);
160+
$workers[] = array(
161+
'fd' => $matches[1],
162+
'ip' => $matches[2],
163+
'id' => $matches[3],
164+
'abilities' => empty($abilities) ? [] : explode(' ', $abilities)
165+
);
166+
}
155167
}
156168

157169
return $workers;

Net/Gearman/Worker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ protected function doWork($conn)
751751
$this->complete($handle, $name, $res);
752752
} catch (Net_Gearman_Job_Exception $e) {
753753
// If the factory method call fails, we won't have a job.
754-
if (isset($job) && $job instanceof Net_Gearman_Job) {
754+
if (isset($job) && $job instanceof Net_Gearman_Job_Common) {
755755
$job->fail();
756756
}
757757

build.xml

Lines changed: 0 additions & 100 deletions
This file was deleted.

composer.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,20 @@
1515
"issues": "https://github.com/brianlmoon/net_gearman/issues"
1616
},
1717
"require": {
18-
"php": ">=5.6.0"
18+
"php": "^7.0.0"
1919
},
2020
"autoload": {
2121
"classmap": [ "Net/Gearman" ]
2222
},
23+
"autoload-dev": {
24+
"psr-4": {
25+
"Net\\Gearman\\Tests\\": "tests/"
26+
}
27+
},
2328
"include-path": [
2429
"."
25-
]
30+
],
31+
"require-dev": {
32+
"phpunit/phpunit": "^7.3"
33+
}
2634
}

0 commit comments

Comments
 (0)