PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/resque.php

https://github.com/hfcorriez/php-resque
PHP | 79 lines | 67 code | 10 blank | 2 comment | 19 complexity | 994ce6b7720823f0b3eb40d93ff7b86e MD5 | raw file
  1. <?php
  2. $QUEUE = getenv('QUEUE');
  3. if(empty($QUEUE)) {
  4. die("Set QUEUE env var containing the list of queues to work.\n");
  5. }
  6. require_once 'lib/Resque.php';
  7. require_once 'lib/Resque/Worker.php';
  8. $REDIS_BACKEND = getenv('REDIS_BACKEND');
  9. if(!empty($REDIS_BACKEND)) {
  10. Resque::setBackend($REDIS_BACKEND);
  11. }
  12. $logLevel = 0;
  13. $LOGGING = getenv('LOGGING');
  14. $VERBOSE = getenv('VERBOSE');
  15. $VVERBOSE = getenv('VVERBOSE');
  16. if(!empty($LOGGING) || !empty($VERBOSE)) {
  17. $logLevel = Resque_Worker::LOG_NORMAL;
  18. }
  19. else if(!empty($VVERBOSE)) {
  20. $logLevel = Resque_Worker::LOG_VERBOSE;
  21. }
  22. $APP_INCLUDE = getenv('APP_INCLUDE');
  23. if($APP_INCLUDE) {
  24. if(!file_exists($APP_INCLUDE)) {
  25. die('APP_INCLUDE ('.$APP_INCLUDE.") does not exist.\n");
  26. }
  27. require_once $APP_INCLUDE;
  28. }
  29. $interval = 5;
  30. $INTERVAL = getenv('INTERVAL');
  31. if(!empty($INTERVAL)) {
  32. $interval = $INTERVAL;
  33. }
  34. $count = 1;
  35. $COUNT = getenv('COUNT');
  36. if(!empty($COUNT) && $COUNT > 1) {
  37. $count = $COUNT;
  38. }
  39. if($count > 1) {
  40. for($i = 0; $i < $count; ++$i) {
  41. $pid = pcntl_fork();
  42. if($pid == -1) {
  43. die("Could not fork worker ".$i."\n");
  44. }
  45. // Child, start the worker
  46. else if(!$pid) {
  47. $queues = explode(',', $QUEUE);
  48. $worker = new Resque_Worker($queues);
  49. $worker->logLevel = $logLevel;
  50. fwrite(STDOUT, '*** Starting worker '.$worker."\n");
  51. $worker->work($interval);
  52. break;
  53. }
  54. }
  55. }
  56. // Start a single worker
  57. else {
  58. $queues = explode(',', $QUEUE);
  59. $worker = new Resque_Worker($queues);
  60. $worker->logLevel = $logLevel;
  61. $PIDFILE = getenv('PIDFILE');
  62. if ($PIDFILE) {
  63. file_put_contents($PIDFILE, getmypid()) or
  64. die('Could not write PID information to ' . $PIDFILE);
  65. }
  66. fwrite(STDOUT, '*** Starting worker '.$worker."\n");
  67. $worker->work($interval);
  68. }
  69. ?>