Skip to content

Instantly share code, notes, and snippets.

@harikt
Created July 22, 2014 16:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save harikt/a0b1fbbd14389f17a555 to your computer and use it in GitHub Desktop.
Where does PDO object fits best when the worker is a long running process. It is having the problem with MySQL server has gone away http://dev.mysql.com/doc/refman/5.1/en/gone-away.html
<?php
// Reverse Client Code
$client= new GearmanClient();
$client->addServer("127.0.0.1", "4730");
$data = array(
'something' => rand(1, 1000),
);
$client->setCompleteCallback(function ($task) {
print "COMPLETE: " . $task->unique() . ", " . $task->data() . "\n";
});
$client->setFailCallback(function ($task) {
print "Failed: " . $task->unique() . ", " . $task->data() . "\n";
});
$client->addTaskBackground('reverse', json_encode($data));
$client->addTask('reverse', json_encode(array()));
$client->addTaskBackground('reverse', json_encode($data));
$client->runTasks();
<?php
// Reverse worker Code
$worker= new GearmanWorker();
$worker->addServer("127.0.0.1", "4730");
$worker->addFunction("reverse", function ($job) {
// use pdo here ? I feel no
$pdo = new PDO();
$payload = json_decode($job->workload());
echo " Processing $payload->something" . PHP_EOL;
if (! isset($payload->something)) {
throw new Exception("Test whether things works! ");
}
return strrev($payload->something);
});
while ($worker->work());
<?php
// Reverse worker Code
$worker= new GearmanWorker();
$worker->addServer("127.0.0.1", "4730");
$pdo = new PDO()
$worker->addFunction("reverse", function ($job) use ($pdo) {
$payload = json_decode($job->workload());
echo " Processing $payload->something" . PHP_EOL;
if (! isset($payload->something)) {
throw new Exception("Test whether things works! ");
}
return strrev($payload->something);
});
while ($worker->work());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment