tripal_daemon.blocks.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * @file
  4. * Contains functions related to administrative blocks for daemon monitoring.
  5. */
  6. /**
  7. * Implements hook_block_info().
  8. */
  9. function tripal_daemon_block_info() {
  10. $blocks = array();
  11. // Status Blocks.
  12. $blocks['trpdaemon_status'] = array(
  13. 'info' => t('Tripal Daemon Status'),
  14. 'cache' => DRUPAL_NO_CACHE,
  15. );
  16. $blocks['trpdaemon_status_admin'] = array(
  17. 'info' => t('Tripal Daemon Status: ADMIN'),
  18. 'cache' => DRUPAL_NO_CACHE,
  19. 'status' => TRUE,
  20. 'region' => 'dashboard_sidebar',
  21. );
  22. // Display Log Block.
  23. $blocks['trpdaemon_log'] = array(
  24. 'info' => t('Tripal Daemon Log'),
  25. 'status' => TRUE,
  26. 'region' => 'dashboard_main',
  27. );
  28. return $blocks;
  29. }
  30. /**
  31. * Implements hook_block_view().
  32. */
  33. function tripal_daemon_block_view($delta='') {
  34. $block = array();
  35. switch($delta) {
  36. case 'trpdaemon_status_admin':
  37. $block['subject'] = t('Job Daemon Status');
  38. $block['content'] = theme_tripal_daemon_status_block_content(TRUE);
  39. break;
  40. case 'trpdaemon_status':
  41. $block['subject'] = t('Job Daemon Status');
  42. $block['content'] = theme_tripal_daemon_status_block_content();
  43. break;
  44. case 'trpdaemon_log':
  45. $block['subject'] = t('Job Daemon Log');
  46. $block['content'] = drupal_get_form('trpdaemon_display_log_form');
  47. break;
  48. }
  49. return $block;
  50. }
  51. /**
  52. * Provide markup for the Tripal Job Daemon Status block.
  53. *
  54. * @param $show_all
  55. * A boolean indicating whether to show administrative detail (TRUE) or not (FALSE).
  56. * @return
  57. * HTML to be rendered for the block.
  58. */
  59. function theme_tripal_daemon_status_block_content($show_all = FALSE) {
  60. $output = '';
  61. // Get information.
  62. $is_running = drushd_is_daemon_running('tripal_daemon');
  63. $status_file = drushd_get_daemon_status_file('tripal_daemon');
  64. $status = unserialize(file_get_contents($status_file));
  65. $PID = $status['PID'];
  66. $is_alive = `ps h --pid $PID | wc -l`;
  67. $is_alive = trim($is_alive);
  68. $status_class = ($is_running) ? 'active' : 'inactive';
  69. $status_class = ($is_running AND !$is_alive) ? 'dead' : $status_class;
  70. // Theme content.
  71. drupal_add_css(drupal_get_path('module','tripal_daemon') . '/theme/status_block.css');
  72. // Display the status.
  73. $output .= '<div class="daemon-status">';
  74. if ($is_running and $is_alive) {
  75. $output .= theme_image(array(
  76. 'path' => 'misc/message-24-ok.png',
  77. 'alt' => 'status-ok',
  78. 'attributes' => array(),
  79. ));
  80. if ($status['Running Job']) {
  81. $output .= 'Running Job(s)';
  82. }
  83. else {
  84. $output .= 'Waiting for Job';
  85. }
  86. }
  87. else {
  88. $output .= theme_image(array(
  89. 'path' => 'misc/message-24-error.png',
  90. 'alt' => 'status-error',
  91. 'attributes' => array(),
  92. ));
  93. if ($is_running AND !$is_alive) {
  94. $output .= 'Dead';
  95. }
  96. else {
  97. $output .= 'Stopped';
  98. }
  99. }
  100. $output .= '</div>';
  101. // If asked, show all the details.
  102. if ($show_all) {
  103. $output .= '<ul>';
  104. foreach ($status as $k => $v) {
  105. // If it's a boolean, then make it readable.
  106. if (is_bool($v)) {
  107. $v = ($v) ? 'True' : 'False';
  108. }
  109. // If these are current jobs then we want to link to details.
  110. if ($k == 'Current Jobs' AND !empty($v)) {
  111. $list = array();
  112. foreach ($v as $job_id) {
  113. $url = 'admin/tripal/tripal_jobs/view/' . $job_id;
  114. $list[$job_id] = l($job_id, $url);
  115. }
  116. $v = $list;
  117. }
  118. // If it's an array then make it a list.
  119. if (is_array($v)) {
  120. if (empty($v)) {
  121. $v = 'None';
  122. }
  123. else {
  124. $v = implode(', ', $v);
  125. }
  126. }
  127. $output .= '<li><strong>' . $k . '</strong>: ' . $v . '</li>';
  128. }
  129. $output .= '</ul>';
  130. }
  131. return '<div class="inner '.$status_class.'">' . $output . '</div>';
  132. }
  133. /**
  134. * Form to display a user selected number of lines from the Tripal Job Daemon log file.
  135. */
  136. function trpdaemon_display_log_form($form, $form_state) {
  137. $form['#attached']['css'][] = drupal_get_path('module','tripal_daemon') . '/theme/tripal_daemon.log_block.css';
  138. $status_file = drushd_get_daemon_status_file('tripal_daemon');
  139. if ($status_file) {
  140. $status = unserialize(file_get_contents($status_file));
  141. $file = $status['Current Log File'];
  142. }
  143. else {
  144. $file = NULL;
  145. }
  146. $form['num_lines'] = array(
  147. '#type' => 'radios',
  148. '#title' => 'Lines',
  149. '#description' => 'The number of lines to display from the end of the Tripal Job Daemon Log file.',
  150. '#options' => array(
  151. '10' => '10',
  152. '25' => '25',
  153. '50' => '50',
  154. '100' => '100',
  155. '200' => '200',
  156. '500' => '500',
  157. ),
  158. '#default_value' => '25',
  159. '#attributes' => array(
  160. 'onChange' => 'this.form.submit();',
  161. 'class' => array('container-inline'),
  162. ),
  163. );
  164. if ($file) {
  165. $num_lines = (isset($form_state['values'])) ? $form_state['values']['num_lines'] : $form['num_lines']['#default_value'];
  166. $text = `tail -n $num_lines $file`;
  167. $text = str_replace("\n", '<br />', $text);
  168. }
  169. else {
  170. $num_lines = 0;
  171. $text = '';
  172. }
  173. $form['log'] = array(
  174. '#type' => 'markup',
  175. '#markup' => $text,
  176. '#prefix' => '<pre id="daemon-log">',
  177. '#suffix' => '</pre>',
  178. );
  179. $form['submit'] = array(
  180. '#type' => 'submit',
  181. '#value' => t('Apply'),
  182. '#attributes' => array(
  183. 'style' => array('display: none;'),
  184. ),
  185. );
  186. return $form;
  187. }
  188. /**
  189. * Display Log Form: Submit.
  190. */
  191. function trpdaemon_display_log_form_submit($form, &$form_state) {
  192. $form_state['rebuild'] = TRUE;
  193. }