Browse Source

Merge branch '103-tripal-daemon' into 7.x-3.x

Lacey Sanderson 7 years ago
parent
commit
2960082360

+ 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.status = 'Waiting'"
+      )->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 j.status = 'Waiting' 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');
+    }
+
+  }
+}

+ 82 - 0
tripal_daemon/includes/tripal_daemon.blocks.inc

@@ -0,0 +1,82 @@
+<?php
+/** 
+ * @file
+ * Contains functions related to administrative blocks for daemon monitoring.
+ */
+
+/**
+ * Implements hook_block_info().
+ */
+function tripal_daemon_block_info() {
+  $blocks = array();
+
+  $blocks['tripal_daemon_status'] = array(
+    'info' => t('Tripal Daemon Status'),
+  );
+  
+  return $blocks;
+}
+
+/**
+ * Implements hook_block_view().
+ */
+function tripal_daemon_block_view($delta='') {
+  $block = array();
+
+  switch($delta) {
+    case 'tripal_daemon_status':
+      $is_running = drushd_is_daemon_running('tripal_daemon');
+      $status_class = ($is_running) ? 'active' : 'inactive';
+      $block['subject'] = t('Job Daemon Status');
+      $block['content'] = theme_tripal_daemon_status_block_content();
+      break;
+  }
+  
+  return $block;
+}
+
+/** 
+ *
+ */
+function theme_tripal_daemon_status_block_content() {
+  $output = '';
+
+  // Get information.
+  $is_running = drushd_is_daemon_running('tripal_daemon');
+  $status_file = drushd_get_daemon_status_file('tripal_daemon');
+  $status = unserialize(file_get_contents($status_file));
+
+  $status_class = ($is_running) ? 'active' : 'inactive';
+
+  // Theme content.
+  drupal_add_css(drupal_get_path('module','tripal_daemon') . '/theme/status_block.css');
+
+  $output .= '<div class="daemon-status">';
+  if ($is_running) {
+    $output .= theme_image(array(
+      'path' => 'misc/message-24-ok.png',
+      'alt' => 'status-ok',
+    ));
+    $output .= 'Running';
+  }
+  else {
+    $output .= theme_image(array(
+      'path' => 'misc/message-24-error.png',
+      'alt' => 'status-error',
+    ));
+    $output .= 'Stopped';
+  }
+  $output .= '</div>';
+
+  $output .= '<ul>';
+  foreach ($status as $k => $v) {
+    if (is_bool($v)) {
+      $v = ($v) ? 'True' : 'False';
+    }
+
+    $output .= '<li><strong>' . $k . '</strong>: ' . $v . '</li>';
+  }
+  $output .= '</ul>';
+
+  return '<div class="inner '.$status_class.'">' . $output . '</div>';
+}

+ 29 - 0
tripal_daemon/theme/status_block.css

@@ -0,0 +1,29 @@
+/**
+ * Tripal Daemon Status Block.
+ */
+#dashboard #block-tripal-daemon-tripal-daemon-status div.content,
+  div#block-tripal-daemon-tripal-daemon-status div.content {
+    padding: 0;
+}
+#block-tripal-daemon-tripal-daemon-status div.content div.inner {
+  padding: 15px; 
+}
+#block-tripal-daemon-tripal-daemon-status .active {
+  background-color: #f8fff0; 
+  color: #234600;
+}
+#block-tripal-daemon-tripal-daemon-status .inactive {
+  background-color: #fef5f1;
+  color: #8c2e0b;
+}
+
+#block-tripal-daemon-tripal-daemon-status .daemon-status {
+  font-weight: bolder;
+  font-size: 1.2em;
+  margin-bottom: 15px;
+}
+#block-tripal-daemon-tripal-daemon-status .daemon-status img {
+  top: 6px;
+  position: relative;
+  padding-right: 5px;
+}

+ 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
+
+files[] = TripalDaemon.inc

+ 25 - 0
tripal_daemon/tripal_daemon.module

@@ -0,0 +1,25 @@
+<?php
+/**
+ * @file
+ * Non-Drush Tripal Daemon functionality.
+ */
+
+require_once('includes/tripal_daemon.blocks.inc');
+
+/**
+ * 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;
+}