Why isn't there a result on failed jobs?

Asked by Khai Do on 2013-02-08

Hello. I was wondering why I don't get a result when I set a GearmanJobResult to failed?

Here's my gearman worker function..

    public GearmanJobResult executeFunction() {

        boolean jobResult = false;
        boolean jobResultMsg = "job failed!!"

        GearmanJobResult gjr = new GearmanJobResultImpl(this.jobHandle, jobResult,
                jobResultMsg.getBytes(), new byte[0], new byte[0], 0, 0);
        return gjr;

When I run this job the jobResultMsg is an empty string. So on the client side I don't get a message telling me why the job failed. I do get a message when jobResult=true.

Is there any reason why no message is returned for failed jobs?

Question information

English Edit question
Gearman Java Edit question
No assignee Edit question
Solved by:
Khai Do
Last query:
Last reply:
Eric Lambert (elambert) said : #1

Hi Khai:

Per the Gearman Protocol, failed jobs dont return a result. Which, when you think about it, is the right thing since the job has failed the job should not have a result. There is a means of communicating of this kind of information and that can be done by setting the exception "field" of the GearmanJobResult. So in your case if you were to do something like

GearmanJobResult gjr = new GearmanJobResultImpl(this.jobHandle, jobResult,
                new byte[0], jobResultMsg.getBytes(), new byte[0], 0, 0);

You should see the result sent to the client should have the exceptions (GearmanJobResult.getExceptions()) set to bytes that make up "job failed!".

Let me know if you have questions


Khai Do (zaro0508) said : #2

Thanks Eric Lambert, that solved my question.

Khai Do (zaro0508) said : #3


Khai Do (zaro0508) said : #4

I tried using a modified ReverseFunction example to test but I can't seem to get the exception back from the worker. It doesn't matter what I set the jobResult, only the result is returned to the client. Am I doing something wrong? I'm using with the gearman C server, version 0.27

Here's the worker:

public class ReverseFunction extends AbstractGearmanFunction {
    public GearmanJobResult executeFunction() {
        boolean jobResult = true;
        RuntimeException re = new RuntimeException("job Failed!");

        StringBuffer sb = new StringBuffer(ByteUtils.fromUTF8Bytes((byte[]) this.data));

        GearmanJobResult gjr = new GearmanJobResultImpl(this.jobHandle,
                jobResult, sb.reverse().toString().getBytes(),
                new byte[0], re.toString().getBytes(), 0, 0);
        return gjr;

here's the client:

    public String reverse(String input) {
        String function = ReverseFunction.class.getCanonicalName();
        String uniqueId = null;
        byte[] data = ByteUtils.toUTF8Bytes(input);
        GearmanJobResult res = null;
        GearmanJob job = GearmanJobImpl.createJob(function, data, uniqueId);
        String value = "";
        try {
            res = job.get();
            value = ByteUtils.fromUTF8Bytes(res.getResults());
        } catch (Exception e) {
            value = ByteUtils.fromUTF8Bytes(res.getExceptions());
            e.printStackTrace(); //NOPMD
        return value;

Khai Do (zaro0508) said : #5

This has been fixed. I've verified and tested.