tripal_daemon.blocks.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. ));
  79. if ($status['Running Job']) {
  80. $output .= 'Running Job(s)';
  81. }
  82. else {
  83. $output .= 'Waiting for Job';
  84. }
  85. }
  86. else {
  87. $output .= theme_image(array(
  88. 'path' => 'misc/message-24-error.png',
  89. 'alt' => 'status-error',
  90. ));
  91. if ($is_running AND !$is_alive) {
  92. $output .= 'Dead';
  93. }
  94. else {
  95. $output .= 'Stopped';
  96. }
  97. }
  98. $output .= '</div>';
  99. // If asked, show all the details.
  100. if ($show_all) {
  101. $output .= '<ul>';
  102. foreach ($status as $k => $v) {
  103. // If it's a boolean, then make it readable.
  104. if (is_bool($v)) {
  105. $v = ($v) ? 'True' : 'False';
  106. }
  107. // If these are current jobs then we want to link to details.
  108. if ($k == 'Current Jobs' AND !empty($v)) {
  109. $list = array();
  110. foreach ($v as $job_id) {
  111. $url = 'admin/tripal/tripal_jobs/view/' . $job_id;
  112. $list[$job_id] = l($job_id, $url);
  113. }
  114. $v = $list;
  115. }
  116. // If it's an array then make it a list.
  117. if (is_array($v)) {
  118. if (empty($v)) {
  119. $v = 'None';
  120. }
  121. else {
  122. $v = implode(', ', $v);
  123. }
  124. }
  125. $output .= '<li><strong>' . $k . '</strong>: ' . $v . '</li>';
  126. }
  127. $output .= '</ul>';
  128. }
  129. return '<div class="inner '.$status_class.'">' . $output . '</div>';
  130. }
  131. /**
  132. * Form to display a user selected number of lines from the Tripal Job Daemon log file.
  133. */
  134. function trpdaemon_display_log_form($form, $form_state) {
  135. $form['#attached']['css'][] = drupal_get_path('module','tripal_daemon') . '/theme/tripal_daemon.log_block.css';
  136. $status_file = drushd_get_daemon_status_file('tripal_daemon');
  137. $status = unserialize(file_get_contents($status_file));
  138. $file = $status['Current Log File'];
  139. $form['num_lines'] = array(
  140. '#type' => 'radios',
  141. '#title' => 'Lines',
  142. '#description' => 'The number of lines to display from the end of the Tripal Job Daemon Log file.',
  143. '#options' => array(
  144. '10' => '10',
  145. '25' => '25',
  146. '50' => '50',
  147. '100' => '100',
  148. '200' => '200',
  149. '500' => '500',
  150. ),
  151. '#default_value' => '25',
  152. '#attributes' => array(
  153. 'onChange' => 'this.form.submit();',
  154. 'class' => array('container-inline'),
  155. ),
  156. );
  157. $num_lines = (isset($form_state['values'])) ? $form_state['values']['num_lines'] : $form['num_lines']['#default_value'];
  158. $text = `tail -n $num_lines $file`;
  159. $text = str_replace("\n", '<br />', $text);
  160. $form['log'] = array(
  161. '#type' => 'markup',
  162. '#markup' => $text,
  163. '#prefix' => '<pre id="daemon-log">',
  164. '#suffix' => '</pre>',
  165. );
  166. $form['submit'] = array(
  167. '#type' => 'submit',
  168. '#value' => t('Apply'),
  169. '#attributes' => array(
  170. 'style' => array('display: none;'),
  171. ),
  172. );
  173. return $form;
  174. }
  175. /**
  176. * Display Log Form: Submit.
  177. */
  178. function trpdaemon_display_log_form_submit($form, &$form_state) {
  179. $form_state['rebuild'] = TRUE;
  180. }