PHP ext. 0.5 setting client timeout

7 views
Skip to first unread message

nejc.jelovcan

unread,
Aug 14, 2009, 3:05:33 AM8/14/09
to gearman
I've been playing with Gearman PHP extension for two weeks now. I'm
struggling to guess the meanings of all methods on provided classes
(no docs, what a joy).

So, here's the thing. When using a client, if gearmand is down, I get
a "could not connect" error. That's what I want. But when gearmand is
up and there is no worker registered for the job i'm sending, client
waits forever (or until PHP executing time limit is reached). I've
been looking at constants defined by the extension and trying with
setOptions on client and I couldn't do what I want. Is there any way
to set a timeout for GearmanClient?

One option of course is doing a job in background and then checking
it's state with GearmanClient::jobStatus continuosly, giving up after
certain time limit is reached (but how do i get a result from worker
this way if i need it?).

It would be great if at least the constants were listed somewhere with
their meanings described. Some, are actual values, some are
enumerators (0,1,2,3...), flags (2,4,8,16...). I'm having a hard time
guessing which are relevant for my usage.

Jasper van Wanrooy - Royalfish eSolutions

unread,
Aug 14, 2009, 4:08:42 AM8/14/09
to gea...@googlegroups.com
Hi,

>
> I've been playing with Gearman PHP extension for two weeks now. I'm
> struggling to guess the meanings of all methods on provided classes
> (no docs, what a joy).

I share your opinion completely. I started with creating some
documentation, but it was to tough.

> So, here's the thing. When using a client, if gearmand is down, I get
> a "could not connect" error. That's what I want. But when gearmand is
> up and there is no worker registered for the job i'm sending, client
> waits forever (or until PHP executing time limit is reached). I've
> been looking at constants defined by the extension and trying with
> setOptions on client and I couldn't do what I want. Is there any way
> to set a timeout for GearmanClient?
>
> One option of course is doing a job in background and then checking
> it's state with GearmanClient::jobStatus continuosly, giving up after
> certain time limit is reached (but how do i get a result from worker
> this way if i need it?).

How do you check the status of a background job? Do you have a snippet
of code for that?

nejc.jelovcan

unread,
Aug 14, 2009, 5:13:32 AM8/14/09
to gearman
On Aug 14, 10:08 am, Jasper van Wanrooy - Royalfish eSolutions
<jas...@royalfish.nl> wrote:
>
> How do you check the status of a background job? Do you have a snippet
> of code for that?
>
You trigger the job via GearmanClient::doBackground and get a job
handle, then you use the handle when calling GearmanClient::jobStatus

$handle = $client->doBackground('test', serialize($workload));
while(true)
{
$status = $client->jobStatus($handle);
var_dump($status);
sleep(1);
}

$status is an indexed array of 4 values, first two being booleans and
second two integers.
Here's a couple of different scenarios:

// when working
array(4) {
[0]=> bool(true)
[1]=> bool(true)
[2]=> int(0)
[3]=> int(0)
}

// when done
array(4) {
[0]=> bool(false)
[1]=> bool(false)
[2]=> int(0)
[3]=> int(0)
}

// when all workers for the task are busy (or if there are no workers
registered for the task)
array(4) {
[0]=> bool(true)
[1]=> bool(false)
[2]=> int(0)
[3]=> int(0)
}

It would be great if there was at least a way to get a list of
registered tasks from the job server. That, and an optional timeout
for jobs, waiting for available workers.

James M. Luedke

unread,
Aug 14, 2009, 12:26:17 PM8/14/09
to gea...@googlegroups.com
There are plans to add status calls to libgearman, which I will also add
to the php ext. You could use native php sockets to connect to gearmand
and issue Administrative Protocol commands for now.
http://gearman.org/index.php?id=protocol

Ex. this shows that I have two reverse task queued. Then I started up
the worker which drained the task.

% telnet localhost 4730
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
status
reverse 2 0 0
.
status
reverse 0 0 1
.


-James

Eric Day

unread,
Aug 14, 2009, 1:36:52 PM8/14/09
to gea...@googlegroups.com
Hi!

On Fri, Aug 14, 2009 at 12:05:33AM -0700, nejc.jelovcan wrote:
> I've been playing with Gearman PHP extension for two weeks now. I'm
> struggling to guess the meanings of all methods on provided classes
> (no docs, what a joy).

Yeah, all I can say is they're coming, along with generic Gearman
docs as well. Unfortunately there is only reflection output, a few
examples, and this mailing list. We need more help if anyone is
interested in contributing!

> So, here's the thing. When using a client, if gearmand is down, I get
> a "could not connect" error. That's what I want. But when gearmand is
> up and there is no worker registered for the job i'm sending, client
> waits forever (or until PHP executing time limit is reached). I've
> been looking at constants defined by the extension and trying with
> setOptions on client and I couldn't do what I want. Is there any way
> to set a timeout for GearmanClient?

Not currently, but this will be added in the near future. This will
most likely be an option (like CLIENT_TIMEOUT or WORKER_TIMEOUT)
to wait inside of any client/worker function before returning. It
actually won't be difficult to do since everything is non-blocking
and this will just translate to a poll() timeout request.

> One option of course is doing a job in background and then checking
> it's state with GearmanClient::jobStatus continuosly, giving up after
> certain time limit is reached (but how do i get a result from worker
> this way if i need it?).

Like you said, hard to get your result back if you need it immediately,
foreground job is what you want.

> It would be great if at least the constants were listed somewhere with
> their meanings described. Some, are actual values, some are
> enumerators (0,1,2,3...), flags (2,4,8,16...). I'm having a hard time
> guessing which are relevant for my usage.

Probably not many. Again, something that will come with
documentation. The only flags you'll be concerned with are
gearman_client_options_t/gearman_worker_options_t (as defined in the
C header) options for now. There are not any yet that would help with
your timeout issue.

Thanks for your input, we're getting there. :)

-Eric

Denis Arh

unread,
Sep 27, 2009, 11:58:08 PM9/27/09
to gearman
Hi,

Is there any progress or ETA info for the timeout option on the client
side?

Regards,
Densi

Eric Day

unread,
Sep 28, 2009, 5:10:53 AM9/28/09
to gea...@googlegroups.com
Hi Denis,

The timeouts, for both client and worker, are done in my development
branch. I need to check a couple more things, but this should be merged
into trunk tomorrow and released in 0.10 (C library) this week. The
PHP extension will also have the new functionality added and released
this week. C-library-based Perl and Python soon to follow as well,
I hope. :)

-Eric

Denis Arh

unread,
Oct 15, 2009, 12:45:15 PM10/15/09
to gearman
Hi Eric,

I've finally got to the new version and it works great. I had to write
a couple of wrappers so I can work with exceptions instead of notices
and warnings.
You should look into PDO error handling (http://docs.php.net/manual/en/
pdo.error-handling.php) -- it would be nice to have something similar
in gearman's php extension.

Regards,
Denis

On Sep 28, 11:10 am, Eric Day <e...@oddments.org> wrote:
> Hi Denis,
>
> The timeouts, for both client and worker, are done in my development
> branch. I need to check a couple more things, but this should be merged
> into trunk tomorrow and released in 0.10 (C library) this week. The
> PHP extension will also have the new functionality added and released
> this week. C-library-based Perl and Python soon to follow as well,
> I hope. :)
>
> -Eric
>
> On Sun, Sep 27, 2009 at 08:58:08PM -0700, Denis Arh wrote:
>
> > Hi,
>
> > Is there any progress or ETA info for the timeout option on the client
> > side?
>
> > Regards,
> > Densi
>
> > On Aug 14, 7:36 pm, Eric Day <e...@oddments.org> wrote:
> > > Hi!
>
> > > On Fri, Aug 14, 2009 at 12:05:33AM -0700, nejc.jelovcan wrote:
> > > > I've been playing withGearmanPHP extension for two weeks now. I'm

Eric Day

unread,
Oct 16, 2009, 12:48:51 PM10/16/09
to gea...@googlegroups.com
Hi Denis,

If you want to share any code (or pseudo code) of how you think things
could work, please do. While the PHP module is starting to stablize,
we're still lookings for better ways of doing things. I know James
was still planning to do more with exception handling.

-Eric

Denis Arh

unread,
Oct 17, 2009, 11:41:51 PM10/17/09
to gearman
On Oct 16, 6:48 pm, Eric Day <e...@oddments.org> wrote:
> Hi Denis,
>
> If you want to share any code (or pseudo code) of how you think things
> could work, please do. While the PHP module is starting to stablize,
> we're still lookings for better ways of doing things. I know James
> was still planning to do more with exception handling.
>
> -Eric
>

Currently, my code looks like this (and I have another one, similar
for worker):

class DoZF_Gearman_Client {
protected $_client;

public function getClient() {
if (!$this->_client) {
$this->_client = new GearmanClient;
}

return $this->_client;
}

public function setClient(GearmanClient $client = null) {
$this->_client = $client;
return $this;
}

public function __call($method, $args) {
$client = $this->getClient();

if (!method_exists($client, $method)) {
throw new DoZF_Gearman_Exception('Unknown method '. get_class
($client) .'::'. $method .'()');
}

$rval = @call_user_func_array(array($client, $method), $args);

if ($client->error()) {
throw new DoZF_Gearman_Exception($client->error());
}

return $rval;
}
}

The code for the worker's wrapper is very much the same. This is not
perfect but it does work for now.
This just wraps return value of error() inside some specific exception
class and throws that exception.

If Gearman extension would throw exception I would still have to keep
this wrapper class for unit-test mocking
but I could get rid of non-bullet-proof error-to-exception converter.


Regards,
Denis
Reply all
Reply to author
Forward
0 new messages