Permalink
Browse files

Fixed a bug in the client checking for instances, added code to handl…

…e if already connected, updated client aware to take the interface, added worker (not tested) and worker aware interface
  • Loading branch information...
1 parent 95ceef4 commit 7773895afb75d0461ddd686dd5ca4d7dbd567b09 @mwillbanks committed Feb 16, 2012
@@ -21,6 +21,12 @@
*/
class Pecl implements Client
{
+
+ /**
+ * @var bool
+ */
+ protected $isConnected = false;
+
/**
* @var GearmanClient
*/
@@ -108,6 +114,9 @@ public function addServer($host, $port = 4730)
}
$this->servers[$host . ':' . $port] = array($host, $port);
+ if ($this->isConnected) {
+ $this->getGearmanClient()->addServer($host, $port);
+ }
return $this;
}
@@ -121,6 +130,9 @@ public function addServer($host, $port = 4730)
public function clearServers()
{
$this->servers = array();
+ if ($this->isConnected) {
+ $this->close();
+ }
return $this;
}
@@ -173,6 +185,7 @@ public function connect()
$client = $this->getGearmanClient();
$client->addServers(implode(',', array_keys($this->servers)));
+ $this->isConnected = true;
}
/**
@@ -185,9 +198,10 @@ public function connect()
*/
public function close()
{
- if ($this->client instanceof GearmanClient) {
+ if ($this->client instanceof \GearmanClient) {
$this->client = null;
}
+ $this->isConnected = false;
return $this;
}
@@ -211,6 +225,9 @@ public function addTask(Task $task)
public function doTask(Task $task)
{
$client = $this->getGearmanClient();
+ if (!$this->isConnected) {
+ $this->connect();
+ }
$method = 'do';
$method .= ucwords($task->getPriority());
@@ -247,6 +264,9 @@ public function doTask(Task $task)
*/
public function runTasks()
{
+ if (!$this->isConnected) {
+ $this->connect();
+ }
$handles = array();
foreach ($this->tasks as $task)
{
@@ -261,17 +281,17 @@ public function runTasks()
$context = ($task->hasContext()) ? $task->getContext() : $this->getContext();
- $task = $this->client->$method(
+ $task = $this->getGearmanClient()->$method(
$task->getFunction(),
$task->getWorkload(),
$context,
$task->getUnique()
);
- if ($task instanceof GearmanTask) {
+ if ($task instanceof \GearmanTask) {
$handles[] = $task->jobHandle();
}
}
- $this->client->runTasks();
+ $this->getGearmanClient()->runTasks();
return $handles;
}
@@ -297,6 +317,9 @@ public function setContext($context)
throw new \InvalidArgumentException('Context must be a string');
}
$this->context = $context;
+ if ($this->isConnected) {
+ $this->getGearmanClient()->setContext($context);
+ }
}
/**
@@ -322,6 +345,9 @@ public function setTimeout($timeout)
throw new \InvalidArgumentException('Timeout must be an integer');
}
$this->timeout = $timeout;
+ if ($this->isConnected) {
+ $this->getGearmanClient()->setTimeout($timeout);
+ }
return $this;
}
@@ -334,6 +360,9 @@ public function setTimeout($timeout)
public function ping($workload)
{
$client = $this->getGearmanClient();
+ if (!$this->isConnected) {
+ $this->connect();
+ }
if (method_exists($client, 'ping')) {
return $client->ping($workload);
}
@@ -1,10 +1,11 @@
<?php
namespace mwGearman;
-use mwGearman\Client\Pecl;
+
+use mwGearman\Client;
interface GearmanClientAware
{
- public function setGearmanClient(Pecl $client);
+ public function setGearmanClient(Client $client);
public function getGearmanClient();
}
@@ -0,0 +1,10 @@
+<?php
+
+namespace mwGearman;
+use mwGearman\Worker;
+
+interface GearmanWorkerAware
+{
+ public function setGearmanWorker(Worker $worker);
+ public function getGearmanWorker();
+}
View
@@ -0,0 +1,26 @@
+<?php
+
+namespace mwGearman;
+
+interface Worker
+{
+ // connection related
+ // @todo move connection related items into their own connection class
+ // connection related
+ public function addServer($host, $port);
+ public function clearServers();
+ public function getServers();
+ public function setServers(array $servers);
+ public function connect();
+ public function close();
+
+ public function getTimeout();
+ public function setTimeout($timeout);
+
+ public function register($func);
+ public function unregister($func);
+ public function write($workload);
+ public function wait();
+ public function work();
+ public function getError();
+}
Oops, something went wrong.

0 comments on commit 7773895

Please sign in to comment.