Browse Source

Fully functional and plugged into the Daemon API :):):)

Lacey Sanderson 10 years ago
parent
commit
f32070d71c

+ 31 - 0
tripal_daemon/TripalJobDaemon.inc

@@ -0,0 +1,31 @@
+<?php
+
+/**
+ * This is the main class for a Job Daemon.
+ *
+ * A Daemon is created by running run.php where an instance of this class is created
+ */
+class TripalJobDaemon extends JobDaemon {
+
+  /**
+   * Implements JobDaemon::job() function
+   * This gets executed once per loop iteration
+   */
+  protected function job() {
+
+    // First check to see if there are any tripal jobs to be run
+    $num_waiting_jobs = db_query('SELECT count(*) as count FROM {tripal_jobs} j WHERE j.pid IS NULL AND j.end_time IS NULL')->fetchField();
+
+    // If there are then run them and log the output
+    if ($num_waiting_jobs > 0) {
+      $this->log("$num_waiting_jobs Waiting Tripal Jobs... Running them now.");
+
+      // Launch all tripal jobs :) Yay for bootstrapping!!
+      tripal_launch_job();
+    }
+    else {
+      $this->log('There are no Tripal Jobs to run');
+    }
+
+	}
+}

+ 0 - 149
tripal_daemon/classes/TripalJobDaemon.inc

@@ -1,149 +0,0 @@
-<?php
-
-/**
- *
- */
-class TripalJobDaemon {
-
-  // Maximum Memory Usage Allowed
-  // During Health checks, if the memory usage is over this % usage for the system
-  // then the child process will kill re-fork and kill itself
-  protected $memory_threshold = 0.85;
-  public function get_memory_threshold() { return $this->memory_threshold; }
-
-  // How often to check for more Tripal Jobs (in seconds)
-  // Default is 1 minute (60 seconds)
-  protected $wait_time = 60;
-  public function get_wait_time() { return $this->wait_time; }
-
-  // The daemon command, such as start, stop, restart
-  protected $action = '';
-  public function get_action() { return $this->action; }
-
-  // The filepath to store the log and status files to
-  // This should be either the sites/default/files directory for your Drupal site
-  // or the /tmp directory and should be set upon creation of the Tripal Daemon
-  protected $file_path;
-
-  // The filename & path of the log file
-  public $log_filename;
-
-  // The filename & path of the status file
-  public $status_filename;
-
-  // Whether or not the current process is a Daemon (ie: child) or not (ie: parent)
-  protected $is_daemon = FALSE;
-
-  /**
-   * Creates a new TripalDaemon object
-   */
-  public function __construct($args = array()) {
-
-    // Set the log/status files
-    $this->file_path = (isset($args['file_path'])) ? $args['file_path'] : '/tmp';
-
-    $this->log_filename = (isset($args['log_filename'])) ? $args['log_filename'] : 'tripaljobs_daemon.log';
-    $this->log_filename = $this->file_path . '/' . $this->log_filename;
-
-    $this->status_filename = (isset($args['status_filename'])) ? $args['status_filename'] : 'tripaljobs_daemon.status.json';
-    $this->status_filename = $this->file_path . '/' . $this->status_filename;
-
-    // If the wait time is provided then set it to the provided value
-    if (isset($args['wait_time'])) {
-      $this->wait_time = $args['wait_time'];
-    }
-
-    // If the memory threshold is provided then use it instead of our default
-    if (isset($args['memory_threshold'])) {
-      $this->memory_threshold = $args['memory_threshold'];
-    }
-
-    // Whether or not the current process is a Daemon (ie: child) or not (ie: parent)
-    // There are VERY FEW cases where it's a good idea to pass this option in yourself
-    if (isset($args['is_daemon'])) {
-      $this->is_daemon = $args['is_daemon'];
-    }
-  }
-
-  /**
-   * Start the Daemon
-   */
-  public function start() {
-
-    // If we are not currently dealing with the daemon/child process then we need to first
-    // create it via forking
-    if (!$this->is_daemon) {
-      print "Starting the Tripal Daemon...\n";
-      $this->fork();
-    }
-
-
-    // If we are a Daemon, we need to implement the never-ending daemonizing loop
-    if ($this->is_daemon) {
-
-      //Now, we detach from the terminal window, so that we stay alive when it is closed.
-      if ( posix_setsid() == -1 ) {
-	      echo "\n Error: Unable to detach from the terminal window. \n";
-      }
-
-      for ($i=0; $i<=10; $i++) {
-        print posix_getpid() . " Mua" . str_repeat("ha",rand(1,15)) . "\n";
-      }
-    }
-    // Otherwise, we are going to be an irresponsible parent and let our child run amok
-    else {
-      print posix_getpid() . " Letting my child run amok :)\n";
-      exit;
-    }
-  }
-
-  /**
-   * Stop the Daemon
-   */
-  public function stop() {
-    print "Stopping the Tripal Daemon...\n";
-  }
-
-  /**
-   * Restart the Daemon
-   */
-  public function restart() {
-    print "Re-Starting the Tripal Daemon...\n";
-  }
-
-  /**
-   * Show the Status of the Daemon
-   */
-  public function status() {
-    print "Checking the Status of the Tripal Daemon...\n";
-  }
-
-  /**
-   * Show the Log of the Daemon
-   */
-  public function log() {
-    print "Showing the Tripal Daemon Log...\n";
-  }
-
-  /**
-   * Fork off a new process to be the daemon since nobody wants to be the daemon themselves ;-)
-   */
-  protected function fork() {
-    $pid = pcntl_fork();
-    pcntl_signal(SIGCHLD, SIG_IGN);
-
-    // Problem launching the job
-    if ($pid == -1){
-      error_log('Could not launch new job, exiting');
-      return FALSE;
-    }
-    // Parent Process since children are not self-aware
-    else if ($pid) {
-      print "Daemon Process ID: $pid.\n";
-    }
-    // Child Process (we should never get here b/c of the is_daemon logic in start)
-    else {
-      $this->is_daemon = TRUE;
-    }
-  }
-}

+ 13 - 49
tripal_daemon/tripal_daemon.drush.inc

@@ -24,39 +24,30 @@ function tripal_daemon_drush_command() {
     'description' => dt('Use Tripal Jobs Deamon to manage Tripal Job execution.'),
     'arguments' => array(
       'start'    => 'Start the daemon.',
-      'status'   => 'Display status information about the daemon.',
+      //'status'   => 'Display status information about the daemon.',
       'stop'     => 'Stop the daemon.',
-      'restart'  => 'Restart the daemon',
-      'show-log' => 'Show the log file.',
+      //'restart'  => 'Restart the daemon',
+      //'show-log' => 'Show the log file.',
     ),
     'options' => array(
-      'memory_threshold' => 'The maximum memory usage allowed before the daemon should kill itself. Default: 0.85',
-      'wait_time' => 'The amount of seconds to wait before checking whether a Tripal Job has been submitted. Default: 60.',
-      'file_path' => 'The filepath to store the log and status files to. This should be
-        either the sites/default/files directory for your Drupal site or the /tmp directory. Default: /tmp.',
-      'log_filename' => 'The name of the logfile. Default: tripaljobs_daemon.log',
-      'status_filename' => array(
-        'description' => 'The name of the file containing a JSON array describing the status of the Daemon. Default: tripaljobs_daemon.status.json',
-        'hidden' => TRUE
-      )
     ),
     'examples' => array(
       'drush trpjob-daemon start'                            => 'Start the daemon.',
       //'drush trpjob-daemon start --feedback="100 items"'     => 'Log a status message every 100 nodes.',
       //'drush trpjob-daemon start --feedback="60 seconds"'    => 'Log a status message every 60 seconds.',
       //'drush trpjob-daemon start --verbose'                  => 'Log verbosely.',
-      ' '   => '',
-      'drush trpjob-daemon status' => 'Show the current status of the daemon.',
+      //' '   => '',
+      //'drush trpjob-daemon status' => 'Show the current status of the daemon.',
       '  '  => '',
       'drush trpjob-daemon stop'              => 'Stop the daemon.',
       //'drush trpjob-daemon stop --timeout=10' => 'Allow 10 seconds for processing.',
       //'drush trpjob-daemon stop --queue'      => 'Queue the node access rebuild daemon to stop.',
-      '   ' => '',
-      'drush trpjob-daemon restart'              => 'Restart the daemon.',
+      //'   ' => '',
+      //'drush trpjob-daemon restart'              => 'Restart the daemon.',
       //'drush trpjob-daemon restart --timeout=10' => 'Allow 10 seconds for processing.',
       //'drush trpjob-daemon restart --queue'      => 'Queue the node access rebuild daemon to restart.',
-      '    ' => '',
-      'drush trpjob-daemon show-log' => 'Show the log file, using less.',
+      //'    ' => '',
+      //'drush trpjob-daemon show-log' => 'Show the log file, using less.',
       //'drush trpjob-daemon show-log --watch' => 'Watch the log file.',
       //'drush trpjob-daemon show-log --tail' => 'Show just the tail of the log file, to see recent messages.',
     ),
@@ -67,41 +58,14 @@ function tripal_daemon_drush_command() {
 }
 
 /**
- * Drush Command for Daemonized management of Tripal Jobs
+ * Drush Command for Daemonized management of Tripal Jobs.
+ * Simply plugs into the Daemon API for easier running. This is the equivalent of
+ *   drush jobs-daemon $action tripal_daemon
  *
  * @param $action
  *   One of 'start','stop','restart',status','show-log'. Meant to indicate what you want
  *   the daemon to do.
  */
 function drush_tripal_daemon_tripal_jobs_daemon($action) {
-
-  $details = tripal_daemon_drush_command();
-  $supported_options = array_keys($details['tripal-jobs-daemon']['options']);
-
-  $script_path = drupal_get_path('module','tripal_daemon');
-  $module_path = DRUPAL_ROOT . '/' . drupal_get_path('module','tripal_daemon');
-
-  $output = NULL;
-  $return_value = NULL;
-  $options = array();
-  foreach ($supported_options as $opt) {
-    $val = drush_get_option($opt);
-    if ($val) {
-      $options[] = $opt . '=' . $val;
-    }
-  }
-  $command = "php $script_path/tripal_daemon_script.php $action module_path=$module_path " . implode(" ",$options) . " &";
-  exec($command); //, $output, $return_value);
-  /**
-  drush_print(implode("\n",$output));
-
-  switch($return_value) {
-    case 0:
-      drush_log("Daemon started successfully.",'ok');
-      break;
-    default:
-      drush_log("Unable to $action the Daemon.",'error');
-      break;
-  }
-  */
+  drush_daemon_api_jobs_daemon($action, 'tripal_daemon');
 }

+ 5 - 1
tripal_daemon/tripal_daemon.info

@@ -2,4 +2,8 @@ name = Tripal Jobs Daemon
 description = Creates a Daemon to run Tripal Jobs as they are submitted.
 core = 7.x
 package = Tripal Extensions
-dependencies[] = tripal_core
+
+dependencies[] = daemon_api
+dependencies[] = tripal_core
+
+files[] = TripalJobDaemon.inc

+ 22 - 0
tripal_daemon/tripal_daemon.module

@@ -4,3 +4,25 @@
  * @file
  * Non-Drush Tripal Daemon functionality
  */
+
+/**
+ * Implements hook_daemon_api_info().
+ * Registers our Daemon with the Daemon API
+ */
+function tripal_daemon_daemon_api_info() {
+  $daemon = array();
+
+  $daemon['tripal_daemon'] = array(
+    // the machine name of the daemon (same as key above)
+    'machine_name' => 'tripal_daemon',
+    // A human-readable name for your daemon
+    'name' => 'Tripal Job Daemon',
+    // this module (ie: the module implementing the daemon)
+    'module' => 'tripal_daemon',
+    // The class extending JobDaemon and implementing your daemon-specific functionality
+    // This class should be in a [classname].inc file in your modules base directory
+    'class' => 'TripalJobDaemon'
+  );
+
+  return $daemon;
+}

+ 0 - 32
tripal_daemon/tripal_daemon_script.php

@@ -1,32 +0,0 @@
-<?php
-
-/**
- * This is the script that is actually Daemonized.
- * Expected to be run via Drush tripal-jobs-daemon
- */
-
-// Get Command-line Variables
-parse_str(implode('&', array_slice($argv, 1)), $args);
-$action = $argv[1];
-if (!$action) {
-  die('You need to specify what you want the Daemon to do. This should be one of: start, stop, restart, status, show-log');
-}
-
-require_once $args['module_path'] . '/classes/TripalJobDaemon.inc';
-$Daemon = new TripalJobDaemon($args);
-
-print "\nTripal Jobs Daemon\n".str_repeat("=",60)."\n";
-print "Memory Threshold: " . ($Daemon->get_memory_threshold() * 100) . "%\n";
-print "Wait Time: ". $Daemon->get_wait_time() . " seconds\n";
-print "Log File: " . $Daemon->log_filename . "\n";
-print "\n";
-
-// Check that the action is valid and then execute it
-// Everything else is taken case of by the object :)
-$action = strtolower($action);
-if (method_exists($Daemon, $action)) {
-  $Daemon->{$action}();
-}
-else {
-  die("ERROR: Unable to $action the daemon. This action is not recognized; instead try one of 'start', 'stop', 'restart', 'status' or 'log'.\n\n");
-}