فهرست منبع

Created my outline class and daemon script

Lacey Sanderson 10 سال پیش
والد
کامیت
189c6981d2
3فایلهای تغییر یافته به همراه203 افزوده شده و 0 حذف شده
  1. 93 0
      tripal_daemon/classes/TripalJobDaemon.inc
  2. 73 0
      tripal_daemon/tripal_daemon.drush.inc
  3. 37 0
      tripal_daemon/tripal_daemon_script.php

+ 93 - 0
tripal_daemon/classes/TripalJobDaemon.inc

@@ -0,0 +1,93 @@
+<?php
+
+/**
+ *
+ */
+class TripalJobDaemon {
+
+  // Maximum Memory Threshold
+  // 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
+  protected $log_filename;
+
+  // The filename & path of the status file
+  protected $status_filename;
+
+  /**
+   * 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['logfile'])) ? $args['logfile'] : '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'];
+    }
+  }
+
+  /**
+   * Start the Daemon
+   */
+  public function start() {
+    print "Starting the Tripal Daemon...\n";
+  }
+
+  /**
+   * 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";
+  }
+}

+ 73 - 0
tripal_daemon/tripal_daemon.drush.inc

@@ -0,0 +1,73 @@
+<?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.',
+      'restart'  => 'Restart the daemon',
+      'show-log' => 'Show the log file.',
+    ),
+    'options' => array(
+      //'--feedback' => 'Frequency of progress messages, in seconds or items processed.',
+    ),
+    '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 stop'              => 'Stop the daemon, allowing 45 seconds for processing.',
+      //'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, allowing 45 seconds for processing.',
+      //'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.',
+    ),
+    'aliases' => array('trpjob-daemon'),
+  );
+
+  return $items;
+}
+
+/**
+ * Drush Command for Daemonized management of Tripal Jobs
+ *
+ * @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) {
+
+  drush_print("\nTripal Jobs Daemon\n".str_repeat("=",60)."\n");
+  drush_print(" You have choosen to $action the daemon.\n");
+
+}

+ 37 - 0
tripal_daemon/tripal_daemon_script.php

@@ -0,0 +1,37 @@
+<?php
+
+require_once 'classes/TripalDaemon.inc';
+
+/**
+ * This is the script that is actually Daemonized.
+ *
+ * Arguments expected to be passed to this script:
+ *  - action: One of 'start','stop','restart',status','show-log'. Meant to indicate what
+ *       you want the daemon to do.
+ *  - log_file: the full path & filename of the log file. If it doesn't exist this script will
+ *       create it.
+ */
+
+// 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');
+}
+
+$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 "\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");
+}