Skip to content

Commit 905fbcd

Browse files
authored
Merge pull request #51 from brianlmoon/status_update_improvement
Improvements from DealNews upstream
2 parents 0f61d29 + 38fec5d commit 905fbcd

File tree

7 files changed

+109
-59
lines changed

7 files changed

+109
-59
lines changed

Net/Gearman/Client.php

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

Net/Gearman/Worker.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
* exit;
4949
* }
5050
*
51-
* ?>
51+
*
5252
* </code>
5353
*
5454
* @category Net
@@ -275,6 +275,7 @@ private function connect($server) {
275275

276276
if (isset($this->retryConn[$server])) {
277277
unset($this->retryConn[$server]);
278+
$this->status("Removing server from the retry list.", $server);
278279
}
279280

280281
$this->status("Connected to $server", $server);
@@ -707,6 +708,9 @@ protected function doWork($conn)
707708
break;
708709
}
709710
}
711+
712+
$this->sleepConnection($server);
713+
710714
$this->status(
711715
"No job was returned from the server",
712716
$server
@@ -736,6 +740,11 @@ protected function doWork($conn)
736740
}
737741

738742
try {
743+
744+
if (empty($this->initParams[$name])) {
745+
$this->initParams[$name] = [];
746+
}
747+
739748
$job = Net_Gearman_Job::factory(
740749
$name, $conn, $handle, $this->initParams[$name]
741750
);
@@ -861,7 +870,7 @@ protected function status($message, $server = null)
861870

862871
if (!empty($server)) {
863872
$failed_conns = isset($this->failedConn[$server]) ? $this->failedConn[$server] : 0;
864-
$connected = isset($this->retryConn[$server]);
873+
$connected = isset($this->conn[$server]) && $this->conn[$server]->isConnected();
865874
} else {
866875
$failed_conns = null;
867876
$connected = null;

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@
2929
"."
3030
],
3131
"require-dev": {
32-
"phpunit/phpunit": "^7.5|^8.0"
32+
"phpunit/phpunit": "^9.6"
3333
}
3434
}

phpunit.xml.dist

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="./tests/bootstrap.php"
3-
colors="true">
4-
<testsuites>
5-
<testsuite name="Main">
6-
<directory suffix="Test.php">./tests</directory>
7-
</testsuite>
8-
</testsuites>
9-
<groups>
10-
<exclude>
11-
<group>functional</group>
12-
</exclude>
13-
</groups>
14-
<filter>
15-
<whitelist>
16-
<directory>./Net</directory>
17-
</whitelist>
18-
</filter>
19-
</phpunit>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory>./Net</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Main">
10+
<directory suffix="Test.php">./tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
<groups>
14+
<exclude>
15+
<group>functional</group>
16+
</exclude>
17+
</groups>
18+
</phpunit>

phpunit.xml.functional-dist

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="./tests/bootstrap.php"
3-
colors="true">
4-
<php>
5-
<const name="NET_GEARMAN_TEST_SERVER" value="localhost:4730"/>
6-
</php>
7-
<testsuites>
8-
<testsuite name="Main">
9-
<directory suffix="Test.php">./tests</directory>
10-
</testsuite>
11-
</testsuites>
12-
<groups>
13-
<include>
14-
<group>functional</group>
15-
</include>
16-
</groups>
17-
<filter>
18-
<whitelist>
19-
<directory>./Net</directory>
20-
</whitelist>
21-
</filter>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory>./Net</directory>
6+
</include>
7+
</coverage>
8+
<php>
9+
<const name="NET_GEARMAN_TEST_SERVER" value="localhost:4730"/>
10+
</php>
11+
<testsuites>
12+
<testsuite name="Main">
13+
<directory suffix="Test.php">./tests</directory>
14+
</testsuite>
15+
</testsuites>
16+
<groups>
17+
<include>
18+
<group>functional</group>
19+
</include>
20+
</groups>
2221
</phpunit>

tests/Net/Gearman/TaskTest.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ class Net_Gearman_TaskTest extends \PHPUnit\Framework\TestCase
88
* Unknown job type.
99
*
1010
* @return void
11-
* @expectedException \Net_Gearman_Exception
1211
*/
1312
public function testExceptionFromConstruct()
1413
{
14+
$this->expectException(\Net_Gearman_Exception::class);
1515
new Net_Gearman_Task('foo', [], null, 8);
1616
}
1717

@@ -30,20 +30,16 @@ public function testParameters()
3030
$this->assertEquals($uniq, $task->uniq);
3131
}
3232

33-
/**
34-
* @expectedException \Net_Gearman_Exception
35-
*/
3633
public function testAttachInvalidCallback()
3734
{
35+
$this->expectException(\Net_Gearman_Exception::class);
3836
$task = new Net_Gearman_Task('foo', []);
3937
$task->attachCallback('func_bar');
4038
}
4139

42-
/**
43-
* @expectedException \Net_Gearman_Exception
44-
*/
4540
public function testAttachInvalidCallbackType()
4641
{
42+
$this->expectException(\Net_Gearman_Exception::class);
4743
$task = new Net_Gearman_Task('foo', []);
4844
$this->assertInstanceOf('Net_Gearman_Task', $task->attachCallback('strlen', 666));
4945
}

tests/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ From the project root:
99

1010
## Run the functional tests
1111
1. Start up your gearman job server
12-
1. Update the `NET_GEARMAN_TEST_SERVER` constant in `phpunit.xml.functional-dist` (if necessary)
12+
1. For local testing, this docker command can be used: ` docker run --name gearmand --rm -d -p 4730:4730 artefactual/gearmand:latest`
13+
1. Update the `NET_GEARMAN_TEST_SERVER` constant in `phpunit.xml.functional-dist` (if not on localhost and/or port 4730)
1314
1. vendor/bin/phpunit -c phpunit.xml.functional-dist

0 commit comments

Comments
 (0)