Procházet zdrojové kódy

Added README, fixed some formatting & a couple of bugs

Lacey Sanderson před 10 roky
rodič
revize
d7d2479c78

+ 64 - 0
tripal_daemon/README.txt

@@ -0,0 +1,64 @@
+
+CONTENTS OF THIS FILE
+---------------------
+ * Introduction
+ * Features
+ * Requirements
+ * Installation
+ * Configuration
+ * Daemon Usage
+
+INTRODUCTION
+------------
+This module is meant to provide a simple means of creating a robust
+command-line-driven, fully bootstrapped PHP Daemon. It uses the PHP-Daemon
+(https://github.com/shaneharter/PHP-Daemon) Library to create the Daemon (via
+the Libraries API) in order to not re-invent the wheel ;-).
+
+
+FEATURES
+--------
+ * Provides a Drush interface to start/stop your Daemon.
+ * Your daemon starts in the background and is detached from the current
+   terminal.
+ * Daemon will run all Tripal Jobs submitted within 20 seconds.
+ * A log of the number of jobs executed including their identifier and the result.
+ * Lock Files, Automatic restart (8hrs default) and Built-in Signal Handling &
+   Event Logging are only a few of the features provided by the Daemon API
+   making this a fully featured & robust Daemon.
+
+REQUIREMENTS
+------------
+ * Libraries API (https://www.drupal.org/project/libraries)
+ * PHP-Daemon Library version 2.0 (https://github.com/shaneharter/PHP-Daemon)
+ * Drush 5.x (https://github.com/drush-ops/drush)
+ * Daemon API (https://www.drupal.org/sandbox/laceysanderson/2311987)
+
+INSTALLATION
+------------
+ * Install all required modules as per their instructions.
+ * Install this module as you would normally install a contributed drupal
+   module. See:https://drupal.org/documentation/install/modules-themes/modules-7
+   for further information.
+ * Download the PHP-Daemon Library and extract it in your sites/all/libraries
+   directory. The folder must be named "PHP-Daemon".
+
+CONFIGURATION
+-------------
+The module has no menu or modifiable settings.  There is no configuration.  When
+enabled, the module will provide a number of drush commands for control of the
+Tripal Daemon from the command-line.
+
+TRIPAL DAEMON USAGE
+-------------------
+* Start Daemon
+    drush trpjob-daemon start
+* Stop Daemon
+    drush trpjob-daemon stop
+* Check the Status
+    drush trpjob-daemon status
+* Show the Log
+   * List the last 10 lines of the log file:
+      drush trpjob-daemon show-log
+   * List the last N lines of the log file:
+      drush trpjob-daemon show-log --num_lines=N

+ 81 - 0
tripal_daemon/TripalDaemon.inc

@@ -0,0 +1,81 @@
+<?php
+
+/**
+ * @file
+ * Implements the Tripal Daemon functionality by using the Daemon API
+ */
+
+/**
+ * This is the main class for the Tripal Daemon.
+ *
+ * It extends the DaemonAPIDaemon class provided by the Daemon API in order
+ * to implement tripal job checking and execution functionality.
+ */
+class TripalDaemon extends DaemonAPIDaemon {
+
+  // OPTIONAL: Set how often in seconds your executeTask() should be called.
+  // Keep in mind that this time does not include the amount of time spent
+  // executing your tasks. For example, if you set this to 5 seconds and you
+  // have 2 tasks in your execute_tasks() function, each of which take 15
+  // seconds, then your loop will iterate (and thus your execute_task()
+  // function will be called again) before your tasks finish.
+  // CODING STANDARDS: Can't change this variable to lowerCamel since it
+  // inherits from a library class.
+  protected $loop_interval = 20;
+
+  /**
+   * Implements DaemonAPIDaemon::executeTask() function.
+   *
+   * This gets executed once per loop iteration & does the following:
+   *   1. Checks to see if there are any Tripal Jobs waiting to be executed.
+   *   2. If there are then they are run (jobs with a higher priority and higher
+   *      job_id are run first.
+   *
+   * This function will log how many jobs have been found and when each one was
+   * started/completed, as well as, it's status upon completion.
+   *
+   * @param int $iteration_number
+   *   This is an integer stating the current iteration of the loop you are on.
+   */
+  protected function executeTask($iteration_number) {
+
+    // First check to see if there are any tripal jobs to be run.
+    $waiting_jobs = db_query(
+      "SELECT
+        count(*) as count,
+        array_to_string(array_agg(j.job_id ORDER BY j.priority ASC, j.job_id ASC),'|') as jobs
+      FROM {tripal_jobs} j
+      WHERE j.pid IS NULL AND j.end_time IS NULL"
+    )->fetchObject();
+
+    $num_waiting_jobs = $waiting_jobs->count;
+    $job_ids = explode('|', $waiting_jobs->jobs);
+
+    // If there are then run them and log the output.
+    if ($num_waiting_jobs > 0) {
+      $this->log($num_waiting_jobs . ' Waiting Tripal Jobs... '
+        . 'Running waiting job(s) now.');
+
+      // Launch all tripal jobs :) Yay for bootstrapping!!
+      foreach ($job_ids as $id) {
+        $this->log('Starting Job (ID=' . $id . ')', '', 1);
+        tripal_launch_job(FALSE, $id);
+
+        // Report job details
+        $job = db_query(
+          "SELECT j.*
+          FROM {tripal_jobs} j
+          WHERE j.job_id = :jid",
+          array(':jid' => $id)
+        )->fetchObject();
+        $this->log("Job completed at "
+        . date('d M Y H:i:s', $job->end_time) . " with a status of '"
+        . $job->status . "'", "", 1);
+      }
+    }
+    else {
+      $this->log('There are no Tripal Jobs to run');
+    }
+
+  }
+}

+ 0 - 40
tripal_daemon/TripalJobDaemon.inc

@@ -1,40 +0,0 @@
-<?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
-    $waiting_jobs = db_query("SELECT count(*) as count, array_to_string(array_agg(j.job_id ORDER BY j.priority ASC, j.job_id ASC),'|') as jobs FROM {tripal_jobs} j WHERE j.pid IS NULL AND j.end_time IS NULL")->fetchObject();
-    $num_waiting_jobs = $waiting_jobs->count;
-    $job_ids = explode('|', $waiting_jobs->jobs);
-
-    // If there are then run them and log the output
-    if ($num_waiting_jobs > 0) {
-      $this->log("$num_waiting_jobs Waiting Tripal Jobs... Running waiting job(s) now.");
-
-      // Launch all tripal jobs :) Yay for bootstrapping!!
-      foreach ($job_ids as $id) {
-        $this->log("Starting Job (ID=$id)","",1);
-        tripal_launch_job(FALSE, $id);
-
-        // Report job details
-        $job = db_query("SELECT j.* FROM {tripal_jobs} j WHERE j.job_id = :jid", array(':jid' => $id))->fetchObject();
-        $this->log("Job completed at ". date($job->end_time, 'd M Y H:i:s') . " with a status of '" . $job->status . "'","",1);
-      }
-    }
-    else {
-      $this->log('There are no Tripal Jobs to run');
-    }
-
-	}
-}

+ 13 - 22
tripal_daemon/tripal_daemon.drush.inc

@@ -24,34 +24,25 @@ 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.',
+      'show-log' => 'Show the log file.',
     ),
     'options' => array(
       'num_lines' => 'The number of lines of the log file to show.',
-      'deamonize' => 'Pass this option to push the daemon into the background'
+      'child' => array(
+        'hidden' => TRUE,
+        'description' => 'This option should only be passed via '
+        . 'drush_invoke_process and essentially just allows my command '
+        . 'to not fork bomb',
+      ),
     ),
     '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 start' => 'Start 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 --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 --watch' => 'Watch the log file.',
-      //'drush trpjob-daemon show-log --tail' => 'Show just the tail of the log file, to see recent messages.',
+      'drush trpjob-daemon show-log' => 'Show the last 10 lines of the log file.',
+      'drush trpjob-daemon show-log --num_lines=50' => 'Show the last 10 lines of the log file.',
     ),
     'aliases' => array('trpjob-daemon'),
   );
@@ -69,5 +60,5 @@ function tripal_daemon_drush_command() {
  *   the daemon to do.
  */
 function drush_tripal_daemon_tripal_jobs_daemon($action) {
-  drush_daemon_api_jobs_daemon($action, 'tripal_daemon');
+  drush_daemon_api_daemon($action, 'tripal_daemon');
 }

+ 3 - 7
tripal_daemon/tripal_daemon.module

@@ -2,26 +2,22 @@
 
 /**
  * @file
- * Non-Drush Tripal Daemon functionality
+ * 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'
+    'class' => 'TripalDaemon'
   );
 
   return $daemon;