Forráskód Böngészése

Merge branch '7.x-1.x' of ../tripal_daemon into 103-tripal-daemon

Lacey Sanderson 7 éve
szülő
commit
62b71e81ae

+ 4 - 0
tripal_daemon/.gitattributes

@@ -0,0 +1,4 @@
+*.inc linguist-language=PHP
+*.info linguist-language=PHP
+*.install linguist-language=PHP
+*.module linguist-language=PHP

+ 44 - 0
tripal_daemon/README.md

@@ -0,0 +1,44 @@
+# Tripal Daemon
+
+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 including the number of jobs executed, their identifiers and results.
+* 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)
+* Drush Daemon API (https://www.drupal.org/project/drushd)
+
+## 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".
+
+## 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

+ 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 including the number of jobs executed, their identifiers and results.
+ * 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)
+ * Drush Daemon API (https://www.drupal.org/project/drushd)
+
+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

+ 115 - 0
tripal_daemon/TripalDaemon.inc

@@ -0,0 +1,115 @@
+<?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 DrushDaemon {
+
+  // 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) {
+
+    // When sorting the job list we want to use version specific SQL and thus
+    // need to know the postgreSQL version to determine what SQL to execute.
+    $version_string = db_query('SELECT version()')->fetchField();
+    if (preg_match('/PostgreSQL (\d+)\.(\d+)/', $version_string, $matches)) {
+      $version = array('major' => $matches[1], 'minor' => $matches[2]);
+    }
+    // If we can't determine the version then use the deprecated method.
+    else {
+      $version = array('major' => 8, 'minor' => 4);
+    }
+
+    // First check to see if there are any tripal jobs to be run.
+    if ($version['major'] >= 9 ) {
+      $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();
+    }
+    else {
+     $waiting_jobs = db_query(
+        "SELECT
+          count(*) as count,
+          array_to_string(array_agg(j.job_id),'|') as jobs
+        FROM (SELECT * FROM {tripal_jobs} WHERE pid IS NULL AND end_time IS NULL ORDER BY priority ASC, job_id ASC) as j"
+      )->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);
+
+        // We would like to log the output from the job.
+        // However, most tripal jobs simply print to the screen :-(
+        // Thus we have to use output buffering to capture the output.
+        // Start Buffering.
+        ob_start();
+
+        // Launch Tripal Job.
+        tripal_launch_job(FALSE, $id);
+
+        // Save the buffer to the log and stop buffering.
+        $this->log(str_repeat('=', 80));
+        $this->log(ob_get_clean());
+        $this->log(str_repeat('=', 80));
+
+        // 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');
+    }
+
+  }
+}

+ 65 - 0
tripal_daemon/tripal_daemon.drush.inc

@@ -0,0 +1,65 @@
+<?php
+
+/**
+ * @file
+ * Implementation of the Tripal Daemon Drush commands.
+ */
+
+/**
+ * Implements hook_drush_help().
+ */
+function tripal_daemon_drush_help($command) {
+  switch ($command) {
+    case 'drush:tripal-jobs-daemon':
+      return dt('Use Tripal Jobs Deamon to manage Tripal Job execution.');
+  }
+}
+
+/**
+ * Implements hook_drush_command().
+ */
+function tripal_daemon_drush_command() {
+  $items = array();
+  $items['tripal-jobs-daemon'] = array(
+    'description' => dt('Use Tripal Jobs Deamon to manage Tripal Job execution.'),
+    'arguments' => array(
+      'start'    => 'Start the daemon.',
+      'status'   => 'Display status information about the daemon.',
+      'stop'     => 'Stop the daemon.',
+      'show-log' => 'Show the log file.',
+    ),
+    'options' => array(
+      'num_lines' => 'The number of lines of the log file to show.',
+      '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 status' => 'Show the current status of the daemon.',
+      'drush trpjob-daemon stop'              => 'Stop the daemon.',
+      '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'),
+  );
+
+  return $items;
+}
+
+/**
+ * Drush Command for Daemonized management of Tripal Jobs.
+ *
+ * Simply plugs into the Daemon API for easier running. This is equivalent to
+ *   drush jobs-daemon $action tripal_daemon.
+ *
+ * @param string $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) {
+  drush_drushd_daemon($action, 'tripal_daemon');
+}

+ 9 - 0
tripal_daemon/tripal_daemon.info

@@ -0,0 +1,9 @@
+name = Tripal Jobs Daemon
+description = Creates a Daemon to run Tripal Jobs as they are submitted.
+core = 7.x
+package = Tripal Extensions
+
+dependencies[] = drushd
+dependencies[] = tripal_core
+
+files[] = TripalDaemon.inc

+ 24 - 0
tripal_daemon/tripal_daemon.module

@@ -0,0 +1,24 @@
+<?php
+
+/**
+ * @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(
+    'machine_name' => 'tripal_daemon',
+    'name' => 'Tripal Job Daemon',
+    'module' => 'tripal_daemon',
+    'class' => 'TripalDaemon',
+  );
+
+  return $daemon;
+}