background_process.admin.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * @file
  4. */
  5. /**
  6. * FAPI definition for settings page.
  7. */
  8. function background_process_settings_form() {
  9. $form = array();
  10. $form['background_process_service_timeout'] = array(
  11. '#type' => 'textfield',
  12. '#title' => t('Service timeout'),
  13. '#description' => t('Timeout for service call in seconds (0 = disabled)'),
  14. '#default_value' => variable_get('background_process_service_timeout', BACKGROUND_PROCESS_SERVICE_TIMEOUT),
  15. );
  16. $form['background_process_connection_timeout'] = array(
  17. '#type' => 'textfield',
  18. '#title' => t('Connection timeout'),
  19. '#description' => t('Timeout for connection in seconds'),
  20. '#default_value' => variable_get('background_process_connection_timeout', BACKGROUND_PROCESS_CONNECTION_TIMEOUT),
  21. );
  22. $form['background_process_stream_timeout'] = array(
  23. '#type' => 'textfield',
  24. '#title' => t('Stream timeout'),
  25. '#description' => t('Timeout for stream in seconds'),
  26. '#default_value' => variable_get('background_process_stream_timeout', BACKGROUND_PROCESS_STREAM_TIMEOUT),
  27. );
  28. $form['background_process_redispatch_threshold'] = array(
  29. '#type' => 'textfield',
  30. '#title' => t('Redispatch threshold (for locked processes)'),
  31. '#description' => t('Seconds to wait before redispatching processes that never started.'),
  32. '#default_value' => variable_get('background_process_redispatch_threshold', BACKGROUND_PROCESS_REDISPATCH_THRESHOLD),
  33. );
  34. $form['background_process_cleanup_age'] = array(
  35. '#type' => 'textfield',
  36. '#title' => t('Cleanup age (for locked processes)'),
  37. '#description' => t('Seconds to wait before unlocking processes that never started.'),
  38. '#default_value' => variable_get('background_process_cleanup_age', BACKGROUND_PROCESS_CLEANUP_AGE),
  39. );
  40. $form['background_process_cleanup_age_running'] = array(
  41. '#type' => 'textfield',
  42. '#title' => t('Cleanup age (for running processes)'),
  43. '#description' => t('Unlock processes that has been running for more than X seconds.'),
  44. '#default_value' => variable_get('background_process_cleanup_age_running', BACKGROUND_PROCESS_CLEANUP_AGE_RUNNING),
  45. );
  46. $form['background_process_cleanup_age_queue'] = array(
  47. '#type' => 'textfield',
  48. '#title' => t('Cleanup age for queued jobs'),
  49. '#description' => t('Unlock queued processes that have been more than X seconds to start.'),
  50. '#default_value' => variable_get('background_process_cleanup_age_queue', BACKGROUND_PROCESS_CLEANUP_AGE_QUEUE),
  51. );
  52. $options = background_process_get_service_hosts();
  53. foreach ($options as $key => &$value) {
  54. $new = empty($value['description']) ? $key : $value['description'];
  55. $base_url = empty($value['base_url']) ? $base_url : $value['base_url'];
  56. $http_host = empty($value['http_host']) ? parse_url($base_url, PHP_URL_HOST) : $value['http_host'];
  57. $new .= ' (' . $base_url . ' - ' . $http_host . ')';
  58. $value = $new;
  59. }
  60. $form['background_process_default_service_host'] = array(
  61. '#type' => 'select',
  62. '#title' => t('Default service host'),
  63. '#description' => t('The default service host to use'),
  64. '#options' => $options,
  65. '#default_value' => variable_get('background_process_default_service_host', 'default'),
  66. );
  67. $methods = module_invoke_all('service_group');
  68. $options = background_process_get_service_groups();
  69. foreach ($options as $key => &$value) {
  70. $value = (empty($value['description']) ? $key : $value['description']) . ' (' . join(',', $value['hosts']) . ') : ' . $methods['methods'][$value['method']];
  71. }
  72. $form['background_process_default_service_group'] = array(
  73. '#type' => 'select',
  74. '#title' => t('Default service group'),
  75. '#description' => t('The default service group to use.'),
  76. '#options' => $options,
  77. '#default_value' => variable_get('background_process_default_service_group', 'default'),
  78. );
  79. $form = system_settings_form($form);
  80. // Add determine button and make sure all the buttons are shown last.
  81. $form['buttons']['#weight'] = 1000;
  82. $form['buttons']['determine'] = array(
  83. '#value' => t("Determine default service host"),
  84. '#description' => t('Tries to determine the default service host.'),
  85. '#type' => 'submit',
  86. '#submit' => array('background_process_settings_form_determine_submit'),
  87. );
  88. return $form;
  89. }
  90. /**
  91. * Submit handler for determining default service host
  92. */
  93. function background_process_settings_form_determine_submit($form, &$form_state) {
  94. background_process_determine_and_save_default_service_host();
  95. }
  96. /**
  97. * Overview of background processes.
  98. */
  99. function background_process_overview_page() {
  100. $processes = background_process_get_processes();
  101. $data = array();
  102. foreach ($processes as $process) {
  103. $progress = progress_get_progress($process->handle);
  104. $data[] = array(
  105. $process->handle,
  106. $process->callback,
  107. $process->uid,
  108. $process->service_host,
  109. format_date((int)$process->start, 'custom', 'Y-m-d H:i:s'),
  110. $progress ? sprintf("%.02f%%", $progress->progress * 100) : t('N/A'),
  111. l(t('Unlock'), 'background-process/unlock/' . rawurlencode($process->handle),
  112. array('attributes' => array('class' => 'button-unlock'), 'query' => drupal_get_destination())
  113. )
  114. );
  115. }
  116. $header = array('Handle', 'Callback', 'User', 'Host', 'Start time', 'Progress', '');
  117. $output = '';
  118. $output .= theme('table', array(
  119. 'header' => $header,
  120. 'rows' => $data,
  121. 'class' => 'background-process-overview'
  122. ));
  123. return $output;
  124. }
  125. /**
  126. * Unlock background process.
  127. *
  128. * @param $handle
  129. * Handle of process to unlock
  130. */
  131. function background_process_service_unlock($handle) {
  132. $handle = rawurldecode($handle);
  133. if (background_process_unlock($handle)) {
  134. drupal_set_message(t('Process %handle unlocked', array('%handle' => $handle)));
  135. }
  136. else {
  137. drupal_set_message(t('Process %handle could not be unlocked', array('%handle' => $handle)), 'error');
  138. }
  139. drupal_goto();
  140. }