Gearman receive data of processed task

1.3k Views Asked by At

How do we receive data of a gearman task already ran.

The idea is when the script dies due to certain reason want to get the processed task from the job handle. (say mysql gone, php segmentation fault due to memory)

This is the example code I am using.

<?php

# The client script

# create our gearman client
$gmclient = new GearmanClient();

# add the default job server
$gmclient->addServer();

# set a couple of callbacks so we can track progress
$gmclient->setCompleteCallback("reverse_complete");
$gmclient->setStatusCallback("reverse_status");
$gmclient->setCreatedCallback(function ($task) {
    echo 'Job Handle server returned :  ' . $task->jobHandle() . PHP_EOL;
});

# add another task, but this one to run in the background
$job_handle = $gmclient->doBackground("reverse", "!dlroW olleH");
echo " Job handle : " . $job_handle . PHP_EOL;

// add a task
// $gmclient->addTask("reverse", "!dlroW olleH");
// this will work, how to get data of bg task

if (! $gmclient->runTasks()) {
    echo "ERROR " . $gmclient->error() . "\n";
    exit;
}

echo "DONE\n";

function reverse_status($task) {
    echo "STATUS: " . $task->unique() . ", " . $task->jobHandle() . " - " . $task->taskNumerator() . 
         "/" . $task->taskDenominator() . "\n";
}

function reverse_complete($task) {
    echo "COMPLETE: " . $task->unique() . ", " . $task->data() . "\n";
}

The worker

<?php

# The worker script

echo "Starting\n";

# Create our worker object.
$gmworker= new GearmanWorker();

# Add default server (localhost).
$gmworker->addServer();

# Register function "reverse" with the server.
$gmworker->addFunction("reverse", "reverse_fn");

print "Waiting for job...\n";
while($gmworker->work()) {
    if ($gmworker->returnCode() != GEARMAN_SUCCESS) {
        echo "return_code: " . $gmworker->returnCode() . "\n";
        break;
    }
}

function reverse_fn($job) {
    echo "Received job: " . $job->handle() . "\n";

    $workload = $job->workload();
    $workload_size = $job->workloadSize();

    echo "Workload: $workload ($workload_size)\n";
    $result= strrev($workload);
    echo "Result: $result\n";

    # Return what we want to send back to the client.
    return $result;
}

The status to get the data for the job handle.

<?php
# The client script

# create our gearman client
$gmclient = new GearmanClient();

# add the default job server
$gmclient->addServer();

/*
$gmclient->setClientCallback(function () {
    echo "setClientCallback ! " . PHP_EOL;
});
*/
$gmclient->setCompleteCallback(function () {
    echo "setCompleteCallback ! " . PHP_EOL;
});
$gmclient->setCreatedCallback(function () {
    echo "setCreatedCallback ! " . PHP_EOL;
});
$gmclient->setDataCallback(function () {
    echo "setDataCallback ! " . PHP_EOL;
});
$gmclient->setExceptionCallback(function () {
    echo "setExceptionCallback ! " . PHP_EOL;
});
$gmclient->setFailCallback(function () {
    echo "setFailCallback ! " . PHP_EOL;
});
$gmclient->setStatusCallback(function () {
    echo "setStatusCallback ! " . PHP_EOL;
    echo "Is known ? " . PHP_EOL;
    var_dump($task->returnCode());
    echo "Return code" . PHP_EOL;
    var_dump($task->returnCode());
    echo "Is task running ? " . PHP_EOL;
    var_dump($task->isRunning());
    if (!$task->isKnown())
      $done++;
});
$gmclient->setWarningCallback(function () {
    echo "setWarningCallback ! " . PHP_EOL;
});
$gmclient->setWorkloadCallback(function () {
    echo "setWorkloadCallback ! " . PHP_EOL;
});

// $job_handle = 'H:hari-Vostro-270s:21';
$job_handle = escapeshellarg($argv[1]);
// $gmtask = $gmclient->addTaskStatus($job_handle);
// $gmclient->runTasks();

if ($gmclient->returnCode() != GEARMAN_SUCCESS) {
  echo "bad return code\n";
  exit;
}

$done = false;
do {
    sleep(3);
    $stat = $gmclient->jobStatus($job_handle);
    if (!$stat[0]) {
       // the job is known so it is not done
        $done = true;
    }
    echo "Running: " . ($stat[1] ? "true" : "false") . ", numerator: " . $stat[2] . ", denomintor: " . $stat[3] . "\n";
} while(!$done);

echo "done!\n";
1

There are 1 best solutions below

0
On

from irc, and looking at some python answers (https://stackoverflow.com/a/8349166/487878) I noticed that you could not get the data of a processed task from gearmand.

Either we want to store the data once it is processed by gearman worker and get the data back on the next call.

But you could not store the task handle and quit the client and try again with another client after the task is finished at gearman worker.

Before the worker finished you may get the response, but not after that. Hope that helps someone.