TripalJobDaemon.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. *
  4. */
  5. class TripalJobDaemon {
  6. // Maximum Memory Usage Allowed
  7. // During Health checks, if the memory usage is over this % usage for the system
  8. // then the child process will kill re-fork and kill itself
  9. protected $memory_threshold = 0.85;
  10. public function get_memory_threshold() { return $this->memory_threshold; }
  11. // How often to check for more Tripal Jobs (in seconds)
  12. // Default is 1 minute (60 seconds)
  13. protected $wait_time = 60;
  14. public function get_wait_time() { return $this->wait_time; }
  15. // The daemon command, such as start, stop, restart
  16. protected $action = '';
  17. public function get_action() { return $this->action; }
  18. // The filepath to store the log and status files to
  19. // This should be either the sites/default/files directory for your Drupal site
  20. // or the /tmp directory and should be set upon creation of the Tripal Daemon
  21. protected $file_path;
  22. // The filename & path of the log file
  23. public $log_filename;
  24. // The filename & path of the status file
  25. public $status_filename;
  26. // Whether or not the current process is a Daemon (ie: child) or not (ie: parent)
  27. protected $is_daemon = FALSE;
  28. /**
  29. * Creates a new TripalDaemon object
  30. */
  31. public function __construct($args = array()) {
  32. // Set the log/status files
  33. $this->file_path = (isset($args['file_path'])) ? $args['file_path'] : '/tmp';
  34. $this->log_filename = (isset($args['log_filename'])) ? $args['log_filename'] : 'tripaljobs_daemon.log';
  35. $this->log_filename = $this->file_path . '/' . $this->log_filename;
  36. $this->status_filename = (isset($args['status_filename'])) ? $args['status_filename'] : 'tripaljobs_daemon.status.json';
  37. $this->status_filename = $this->file_path . '/' . $this->status_filename;
  38. // If the wait time is provided then set it to the provided value
  39. if (isset($args['wait_time'])) {
  40. $this->wait_time = $args['wait_time'];
  41. }
  42. // If the memory threshold is provided then use it instead of our default
  43. if (isset($args['memory_threshold'])) {
  44. $this->memory_threshold = $args['memory_threshold'];
  45. }
  46. // Whether or not the current process is a Daemon (ie: child) or not (ie: parent)
  47. // There are VERY FEW cases where it's a good idea to pass this option in yourself
  48. if (isset($args['is_daemon'])) {
  49. $this->is_daemon = $args['is_daemon'];
  50. }
  51. }
  52. /**
  53. * Start the Daemon
  54. */
  55. public function start() {
  56. // If we are not currently dealing with the daemon/child process then we need to first
  57. // create it via forking
  58. if (!$this->is_daemon) {
  59. print "Starting the Tripal Daemon...\n";
  60. $this->fork();
  61. }
  62. // If we are a Daemon, we need to implement the never-ending daemonizing loop
  63. if ($this->is_daemon) {
  64. //Now, we detach from the terminal window, so that we stay alive when it is closed.
  65. if ( posix_setsid() == -1 ) {
  66. echo "\n Error: Unable to detach from the terminal window. \n";
  67. }
  68. for ($i=0; $i<=10; $i++) {
  69. print posix_getpid() . " Mua" . str_repeat("ha",rand(1,15)) . "\n";
  70. }
  71. }
  72. // Otherwise, we are going to be an irresponsible parent and let our child run amok
  73. else {
  74. print posix_getpid() . " Letting my child run amok :)\n";
  75. exit;
  76. }
  77. }
  78. /**
  79. * Stop the Daemon
  80. */
  81. public function stop() {
  82. print "Stopping the Tripal Daemon...\n";
  83. }
  84. /**
  85. * Restart the Daemon
  86. */
  87. public function restart() {
  88. print "Re-Starting the Tripal Daemon...\n";
  89. }
  90. /**
  91. * Show the Status of the Daemon
  92. */
  93. public function status() {
  94. print "Checking the Status of the Tripal Daemon...\n";
  95. }
  96. /**
  97. * Show the Log of the Daemon
  98. */
  99. public function log() {
  100. print "Showing the Tripal Daemon Log...\n";
  101. }
  102. /**
  103. * Fork off a new process to be the daemon since nobody wants to be the daemon themselves ;-)
  104. */
  105. protected function fork() {
  106. $pid = pcntl_fork();
  107. pcntl_signal(SIGCHLD, SIG_IGN);
  108. // Problem launching the job
  109. if ($pid == -1){
  110. error_log('Could not launch new job, exiting');
  111. return FALSE;
  112. }
  113. // Parent Process since children are not self-aware
  114. else if ($pid) {
  115. print "Daemon Process ID: $pid.\n";
  116. }
  117. // Child Process (we should never get here b/c of the is_daemon logic in start)
  118. else {
  119. $this->is_daemon = TRUE;
  120. }
  121. }
  122. }