tripal_daemon.blocks.inc 5.4 KB

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