Search code examples
phptask-queuegearmanworker

PHP Gearman Duplicate tasks


I got:

  • Debian 6
  • nginx 1.2.6
  • PHP 5.3.20-1~dotdeb.0 (fpm-fcgi) (built: Dec 24 2012 11:53:16)
  • gearmand 1.1.4
  • pecl gearman ext 1.1.1

And I got two scripts (Probably I copied them from php.). Client:

<?php
$gmc= new GearmanClient();
$gmc->addServer();

$gmc->setCompleteCallback("reverse_complete");
$gmc->setStatusCallback("reverse_status");

$gmc->addTask("reverse", "!dlroW olleH", null, "2");

if (!$gmc->runTasks())
{
    echo "Error " . $gmc->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 "Done: " . $task->unique() . ", " . $task->data() . "\n";
}

?>

And worker:

<?php
$gmworker= new GearmanWorker();
$gmworker->addServer('127.0.0.1');
$gmworker->addOptions(GEARMAN_WORKER_GRAB_UNIQ);
$gmworker->addFunction("reverse", "reverse_fn");

print "Waiting for tasks...\n";
while($gmworker->work())
{

  if ($gmworker->returnCode() != GEARMAN_SUCCESS)
  {
    echo "return code: " . $gmworker->returnCode() . "\n";
    break;
  }
}

function reverse_fn($job)
{

  echo "uniq: " . $job->unique() . "\n";
  echo "I got job: " . $job->handle() . "\n";

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

  echo "Workload: $workload ($workload_size)\n";

  for ($x= 0; $x < $workload_size; $x++)
  {
    echo "Sending status: " . ($x + 1) . "/$workload_size is done\n";
    $job->sendStatus($x+1, $workload_size);
    $job->sendData(substr($workload, $x, 1));
    sleep(1);
  }

  $result= strrev($workload);
  echo "The result: $result\n";

  return $result;
}

?>

I run gearmand via "gearmand -d". I start worker via "php worker.php".

I open up my client script from my browser, the task goes to server, everything's ok. But when I open up the same script from 2+ browser tabs gearmand receives 2+ similar tasks with the same unique IDs.

That doesn't happen when I add background tasks or use doBackground method or in CLI mode. That also doesn't happen from 2+ different browsers.

I tried different client and server versions. The same thing happens.

I'm totally stuck. Thanks for the help.


Solution

  • old, but i guess i figured that out. browsers don't allow to send 2+ identical queries at a time. the second one waits on the first one to finish first.