0

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.

ns3777k
  • 41
  • 1
  • 5

2 Answers2

0

$gmc->addTask("reverse", "!dlroW olleH", null, "2"); the last option is your unique key. so of course it is always the same.

nihunde
  • 76
  • 4
  • Yes. But as far as I understand if the first task with unique key "2"(for example) was enqueued, the second one that goes right after it with the same unique key shouldn't be added to queue 'cause another task(first one) with the same unique key is already in queue. – ns3777k Jan 27 '13 at 11:10
  • Having had a chance to look at the code i used for gearman: i dont supply the unique, but rather let gearman take care of that and it works just fine. Beware that Gearman is just a simple Workerserver not a fully fledged MessagingQueue. – nihunde Jan 29 '13 at 13:12
  • And gearman puts only one job to the queue even from different tabs of the same browser? Can you put a piece of code here and write the server and client versions please? – ns3777k Jan 29 '13 at 19:16
  • No, of course it starts the job twice. A Job Server ist not a Messaging Queue. you should consider using a real Message Queue or implementing some locks on your Jobs. – nihunde Jan 30 '13 at 10:00
0

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.

ns3777k
  • 41
  • 1
  • 5