reverse_worker.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /*
  3. * Gearman PHP Extension
  4. *
  5. * Copyright (C) 2008 James M. Luedke <contact@jamesluedke.com>,
  6. * Eric Day <eday@oddments.org>
  7. * All rights reserved.
  8. *
  9. * Use and distribution licensed under the PHP license. See
  10. * the LICENSE file in this directory for full text.
  11. */
  12. echo "Starting\n";
  13. # Create our worker object.
  14. $gmworker= new GearmanWorker();
  15. # Add default server (localhost).
  16. $gmworker->addServer();
  17. # Register function "reverse" with the server. Change the worker function to
  18. # "reverse_fn_fast" for a faster worker with no output.
  19. $gmworker->addFunction("reverse", "reverse_fn");
  20. print "Waiting for job...\n";
  21. while($gmworker->work())
  22. {
  23. if ($gmworker->returnCode() != GEARMAN_SUCCESS)
  24. {
  25. echo "return_code: " . $gmworker->returnCode() . "\n";
  26. break;
  27. }
  28. }
  29. function reverse_fn($job)
  30. {
  31. echo "Received job: " . $job->handle() . "\n";
  32. $workload= $job->workload();
  33. $workload_size= $job->workloadSize();
  34. echo "Workload: $workload ($workload_size)\n";
  35. # This status loop is not needed, just showing how it works
  36. for ($x= 0; $x < $workload_size; $x++)
  37. {
  38. echo "Sending status: $x/$workload_size complete\n";
  39. /*
  40. $job->sendStatus($x, $workload_size);
  41. sleep(1);
  42. */
  43. }
  44. $result= strrev($workload);
  45. echo "Result: $result\n";
  46. # Return what we want to send back to the client.
  47. return $result;
  48. }
  49. # A much simpler and less verbose version of the above function would be:
  50. function reverse_fn_fast($job)
  51. {
  52. return strrev($job->workload());
  53. }
  54. ?>