1

I am using Gearman and Slim PHP to create restful API in which:

User will call a restful API and send a file url. This will then:

  1. Download the file - and send the user a unique file id as an http response
  2. As the response is sent, I want to start processing the file
  3. User can check the status of the process by GET www.example.com/api/status API call

I have used gearman to doNormal for the file download part but the status response is sent only after the processing is done as well. Also, how to get status of every client process? I need more help on how exactly could I structure the same and a few details on queing the processing as I am new to gearman.

1 Answer 1

1

You need to use jobstatus and doBackground(). First, you need to initialize the transfer. This is done by sending the task to the background and send the user a job handle. You call this via yourserver.com/api/file-transfer and must send a POST request with the fileurl set. The answer is a json object.

<?php
// use composer for slim
require_once "vendor/autoload.php"; 
$app = new \Slim\Slim();

// init file transfer
$app->post('/file-transfer', function () use ($app) {
    $resp = array();
    try {
        // get file url
        $fileurl = $app->request->post('fileurl');
        $data = json_encode(array("fileurl"=>$fileurl);

        // send to gearman
        $client = new GearmanClient();
        $client->addServer();

        // store the gearman handle and send the task to the background
        $jobHandle = $client->doBackground("fileUpload", $data);

        if ($client->returnCode() != GEARMAN_SUCCESS) throw new Exception("Could not add the job to the queue.", 1);

        $resp["msg"] = "File upload queued";
        $resp["handle"] = $jobHandle;
    } catch(Exception $e) {
        // some error handling
        $resp["msg"] = "There occured a strange error.";
        $resp["error"] = true;
    } finally {
        $response = $app->response();
        $response['Content-Type'] = 'application/json';
        $response->status(200);
        $response->body(json_encode($resp));    
    }
});
?>

In a second step the user needs to query the server with the job handle (which he received from the first call):

$app->post('/file-status', function () use ($app) {
    $jobHandle = $app->request->params('handle');
    $resp = array();
    try {
        // ask for job status
        $client = new GearmanClient();
        $client->addServer();
        $stat = $client->jobStatus($jobHandle);
        if (!$stat[0]) { // it is done
            $resp["msg"]    = "Transfer completed.";
        } else {
            $resp["msg"]    = "Transfer in progress.";
            $w = (float)$stat[2]; // calculate the percentage
            $g = (float)$stat[3];
            $p = ($g>0)?$w/g*100:0;
            $resp["percentage"] = $p;
        }
    } catch(Exception $e) {
            // some error handling
            $resp["msg"] = "There occured a strange error.";
            $resp["error"] = true;
    } finally {
        $response = $app->response();
        $response['Content-Type'] = 'application/json';
        $response->status(200);
        $response->body(json_encode($resp));    
    }
});

In the second request, you have the $stats array from $client->jobStatus(). $stats[0] tells you whether the job is known to the gearman server. The second lement checks if it is running and three and four (2/3) are used to calculate the percentage of the transfer (you need to set these values yourself!).

3
  • Yes some code would really be helpful. I am not able to send a response and then continue a process after it Aug 18, 2015 at 16:47
  • I get the point, however, I want to echo the response to the user once the file upload is done. Then start a new process (to process the uploaded file) for which I want to show the user the status. The status is not supposed to be for the fileupload API Aug 19, 2015 at 5:46
  • Unfortunately, callbacks don't work on background jobs in Gearman. So a possible solution would be to use jquery ajax and poll the server say every five to 10 seconds until the processing has been done.
    – Jan
    Aug 19, 2015 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.