elysia_cron.admin.inc 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callbacks for the elysia cron module.
  5. */
  6. /**
  7. * Page callback for 'admin/config/system/cron' path.
  8. *
  9. * @return array
  10. * Renderable array.
  11. *
  12. * @throws Exception
  13. * Exceptions from theme().
  14. */
  15. function elysia_cron_admin_page() {
  16. $aoutput = array();
  17. if (elysia_cron_access('execute elysia_cron')) {
  18. $aoutput[] = drupal_get_form('elysia_cron_run_form');
  19. }
  20. $output = '';
  21. elysia_cron_initialize();
  22. global $_elysia_cron_settings_by_channel;
  23. $global_disabled = variable_get('elysia_cron_disabled', FALSE);
  24. $last_channel = elysia_cron_last_channel();
  25. $output .= '<p>' . t('Global disable') . ': <i>' . ($global_disabled ? '<span class="warn">' . t('YES') . '</span>' : t('no')) . '</i></p>';
  26. $output .= '<p>' . t('Last channel executed') . ': <i>' . ($last_channel ? $last_channel : t('n/a')) . '</i></p>';
  27. $running = '';
  28. foreach ($_elysia_cron_settings_by_channel as $channel => $data) {
  29. if (elysia_cron_is_channel_running($channel)) {
  30. $running .= $channel . ' ';
  31. }
  32. }
  33. if ($running) {
  34. $output .= '<p>' . t('Running channels') . ': <span class="warn">' . $running . '</span></p>';
  35. }
  36. $output .= '<p>' . t('Last run') . ': ' . elysia_cron_date(_ec_variable_get('elysia_cron_last_run', 0)) . '</p>';
  37. $rows = array();
  38. $ipath = drupal_get_path('module', 'elysia_cron') . '/images/icon_';
  39. foreach ($_elysia_cron_settings_by_channel as $channel => $data) {
  40. $running = elysia_cron_is_channel_running($channel);
  41. $rows[] = array(
  42. array(
  43. 'data' => '<h3>' . t('Channel') . ': ' . $channel . ($data['#data']['disabled'] ? ' <span class="warn">(' . t('DISABLED') . ')</span>' : '') . '</h3>',
  44. 'colspan' => 2,
  45. 'header' => TRUE,
  46. ),
  47. array(
  48. 'data' => elysia_cron_date($data['#data']['last_run']),
  49. 'header' => TRUE,
  50. ),
  51. array(
  52. 'data' => $data['#data']['last_execution_time'] . 's ' . t('(Shutdown: !shutdown)', array('!shutdown' => $data['#data']['last_shutdown_time'] . 's')),
  53. 'header' => TRUE,
  54. ),
  55. array(
  56. 'data' => $data['#data']['execution_count'],
  57. 'header' => TRUE,
  58. ),
  59. array(
  60. 'data' => $data['#data']['avg_execution_time'] . 's / ' . $data['#data']['max_execution_time'] . 's',
  61. 'header' => TRUE,
  62. ),
  63. );
  64. $messages = '';
  65. if ($running) {
  66. $messages .= t('Running since !date', array('!date' => elysia_cron_date($running))) . '<br />';
  67. }
  68. if ($data['#data']['last_aborted'] || $data['#data']['abort_count']) {
  69. $msg = array();
  70. if ($data['#data']['last_aborted']) {
  71. $msg[] = t('Last aborted') . (!empty($data['#data']['last_abort_function']) ? ': <span class="warn">' . t('On function !function', array('!function' => $data['#data']['last_abort_function'])) . '</span>' : '');
  72. }
  73. if ($data['#data']['abort_count']) {
  74. $msg[] = t('Abort count') . ': <span class="warn">' . $data['#data']['abort_count'] . '</span>';
  75. }
  76. $messages .= implode(', ', $msg) . '<br />';
  77. }
  78. if ($messages) {
  79. $rows[] = array(
  80. '',
  81. '',
  82. array('data' => $messages, 'colspan' => 4, 'header' => TRUE),
  83. );
  84. $rows[] = array(array('data' => '', 'colspan' => 6));
  85. }
  86. foreach ($data as $job => $conf) {
  87. $icon = 'idle';
  88. $caption = '<b>' . $job . '</b>';
  89. $tip = t('Idle');
  90. if ($conf['disabled']) {
  91. $icon = 'disabled';
  92. $caption = '<strike><b>' . $job . '</b></strike>';
  93. $tip = t('Disabled');
  94. }
  95. elseif (!empty($conf['running'])) {
  96. $icon = 'running';
  97. $caption = '<b><u>' . $job . '</u></b>';
  98. $tip = t('Running');
  99. }
  100. elseif (elysia_cron_should_run($conf)) {
  101. $icon = 'waiting';
  102. $tip = t('Waiting for execution');
  103. }
  104. if ($job != '#data') {
  105. $force_run = elysia_cron_access('execute elysia_cron')
  106. ? l(t('Force run'), 'admin/config/system/cron/execute/' . $job, array('attributes' => array('onclick' => 'return confirm("' . t('Force execution of !job?', array('!job' => $job)) . '");')))
  107. : '';
  108. $icon_image = theme('image', array(
  109. 'path' => $ipath . $icon . '.png',
  110. 'alt' => $tip,
  111. 'title' => $tip,
  112. ));
  113. $rows[] = array(
  114. array('data' => $icon_image, 'align' => 'right'),
  115. array('data' => $caption . ': <i>' . elysia_cron_description($job) . '</i> ', 'colspan' => 4),
  116. array('data' => $force_run, 'align' => 'right'),
  117. );
  118. $rows[] = array(
  119. '',
  120. check_plain($conf['rule']) . (!empty($conf['weight']) ? ' <small>(' . t('Weight') . ': ' . $conf['weight'] . ')</small>' : ''),
  121. elysia_cron_date($conf['last_run']),
  122. $conf['last_execution_time'] . 's',
  123. $conf['execution_count'],
  124. $conf['avg_execution_time'] . 's / ' . $conf['max_execution_time'] . 's',
  125. );
  126. }
  127. }
  128. $rows[] = array('&nbsp;', '', '', '', '', '');
  129. }
  130. $output .= theme('table', array(
  131. 'header' => array(
  132. '',
  133. t('Job / Rule'),
  134. t('Last run'),
  135. t('Last exec time'),
  136. t('Exec count'),
  137. t('Avg/Max Exec time'),
  138. ),
  139. 'rows' => $rows,
  140. ));
  141. $output .= '<br />';
  142. $legend_icons = array(
  143. 'idle' => theme('image', array(
  144. 'path' => $ipath . 'idle.png',
  145. 'alt' => t('Idle'),
  146. 'title' => t('Idle'),
  147. )),
  148. 'waiting' => theme('image', array(
  149. 'path' => $ipath . 'waiting.png',
  150. 'alt' => t('Waiting for execution'),
  151. 'title' => t('Waiting for execution'),
  152. )),
  153. 'running' => theme('image', array(
  154. 'path' => $ipath . 'running.png',
  155. 'alt' => t('Running'),
  156. 'title' => t('Running'),
  157. )),
  158. 'disabled' => theme('image', array(
  159. 'path' => $ipath . 'disabled.png',
  160. 'alt' => t('Disabled'),
  161. 'title' => t('Disabled'),
  162. )),
  163. );
  164. $output .= theme('table', array(
  165. 'header' => array(t('Legend')),
  166. 'rows' => array(
  167. array($legend_icons['idle'] . ' - ' . t("Job shouldn't do anything right now")),
  168. array($legend_icons['waiting'] . ' - ' . t('Job is ready to be executed, and is waiting for system cron call')),
  169. array($legend_icons['running'] . ' - ' . t('Job is running right now')),
  170. array($legend_icons['disabled'] . ' - ' . t("Job is disabled by settings, and won't run until enabled again")),
  171. array(t("Notes: job times don't include shutdown times (only shown on channel times).")),
  172. array(t('If an abort occurs usually the job is not properly terminated, and so job timings can be inaccurate or wrong.')),
  173. ),
  174. ));
  175. $aoutput[] = array(
  176. '#type' => 'markup',
  177. '#markup' => $output,
  178. );
  179. return $aoutput;
  180. }
  181. /**
  182. * Form builder for general settings form.
  183. *
  184. * @return array
  185. * From API array.
  186. */
  187. function elysia_cron_settings_form() {
  188. global $_elysia_cron_settings_by_channel;
  189. elysia_cron_initialize();
  190. $form = array();
  191. $form['#attached']['js'][] = drupal_get_path('module', 'elysia_cron') . '/js/elysia_cron.js';
  192. $form['prefix_1'] = array(
  193. '#type' => 'fieldset',
  194. '#title' => t('Click for help and cron rules and script syntax'),
  195. '#collapsible' => TRUE,
  196. '#collapsed' => TRUE,
  197. '#description' => t(<<<EOT
  198. <h3>Fields order</h3>
  199. <pre>
  200. +---------------- minute (0 - 59)
  201. | +------------- hour (0 - 23)
  202. | | +---------- day of month (1 - 31)
  203. | | | +------- month (1 - 12)
  204. | | | | +---- day of week (0 - 6) (Sunday=0)
  205. | | | | |
  206. * * * * *
  207. </pre>
  208. <p>Each of the patterns from the first five fields may be either * (an asterisk),
  209. which matches all legal values, or a list of elements separated by commas (see below).</p>
  210. <p>For "day of the week" (field 5), 0 is considered Sunday, 6 is Saturday
  211. (7 is an illegal value)</p>
  212. <p>A job is executed when the time/date specification fields all match the current
  213. time and date. There is one exception: if both "day of month" and "day of week"
  214. are restricted (not "*"), then either the "day of month" field (3) or the "day of week"
  215. field (5) must match the current day (even though the other of the two fields
  216. need not match the current day).</p>
  217. <h3>Fields operators</h3>
  218. <p>There are several ways of specifying multiple date/time values in a field:</p>
  219. <ul>
  220. <li>The comma (',') operator specifies a list of values, for example: "1,3,4,7,8"</li>
  221. <li>The dash ('-') operator specifies a range of values, for example: "1-6", which is equivalent to "1,2,3,4,5,6"</li>
  222. <li>The asterisk ('*') operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to 'every hour' (subject to matching other specified fields).</li>
  223. <li>The slash ('/') operator (called "step") can be used to skip a given number of values. For example, "*/3" in the hour time field is equivalent to "0,3,6,9,12,15,18,21".</li>
  224. </ul>
  225. <h3>Examples</h3>
  226. <pre>
  227. */15 * * * * : Execute job every 15 minutes
  228. 0 2,14 * * *: Execute job every day at 2:00 and 14:00
  229. 0 2 * * 1-5: Execute job at 2:00 of every working day
  230. 0 12 1 */2 1: Execute job every 2 month, at 12:00 of first day of the month OR at every monday.
  231. </pre>
  232. <h3>Script</h3>
  233. <p>You can use the script section to easily create new jobs (by calling a php function)
  234. or to change the scheduling of an existing job.</p>
  235. <p>Every line of the script can be a comment (if it starts with #) or a job definition.</p>
  236. <p>The syntax of a job definition is:</p>
  237. <code>
  238. &lt;-&gt; [rule] &lt;ch:CHANNEL&gt; [job]
  239. </code>
  240. <p>(Tokens between [] are mandatory)</p>
  241. <ul>
  242. <li>&lt;-&gt;: a line starting with "-" means that the job is DISABLED.</li>
  243. <li>[rule]: a crontab schedule rule. See above.</li>
  244. <li>&lt;ch:CHANNEL&gt;: set the channel of the job.</li>
  245. <li>[job]: could be the name of a supported job (for example: 'search_cron') or a function call, ending with ; (for example: 'process_queue();').</li>
  246. </ul>
  247. <p>A comment on the line just preceding a job definition is considered the job description.</p>
  248. <p>Remember that script OVERRIDES all settings on single jobs sections or channel sections of the configuration</p>
  249. <h3>Examples of script</h3>
  250. <pre>
  251. # Search indexing every 2 hours (i'm setting this as the job description)
  252. 0 */2 * * * search_cron
  253. # I'll check for module status only on sunday nights
  254. # (and this is will not be the job description, see the empty line below)
  255. 0 2 * * 0 update_status_cron
  256. # Trackback ping process every 15min and on a channel called "net"
  257. */15 * * * * ch:net trackback_cron
  258. # Disable node_cron (i must set the cron rule even if disabled)
  259. - */15 * * * * node_cron
  260. # Launch function send_summary_mail('test@test.com', FALSE); every night
  261. # And set its description to "Send daily summary"
  262. # Send daily summary
  263. 0 1 * * * send_summary_mail('test@test.com', FALSE);
  264. </pre>
  265. EOT
  266. ),
  267. );
  268. $form['prefix_2'] = array(
  269. '#markup' => '<hr>',
  270. );
  271. $form['main'] = array(
  272. '#title' => t('Main'),
  273. '#type' => 'fieldset',
  274. '#collapsible' => FALSE,
  275. '#collapsed' => FALSE,
  276. );
  277. $form['main']['elysia_cron_disabled'] = array(
  278. '#title' => t('Global disable'),
  279. '#type' => 'checkbox',
  280. '#default_value' => variable_get('elysia_cron_disabled', FALSE),
  281. );
  282. $form['main']['elysia_cron_run_maintenance'] = array(
  283. '#title' => t('Run in maintenance'),
  284. '#description' => t('Allow to run cron in maintenance mode.'),
  285. '#type' => 'checkbox',
  286. '#default_value' => variable_get('elysia_cron_run_maintenance', FALSE),
  287. );
  288. $form['installation'] = array(
  289. '#title' => t('Installation settings'),
  290. '#type' => 'fieldset',
  291. '#collapsible' => TRUE,
  292. '#collapsed' => TRUE,
  293. );
  294. $options[0] = t('Never / Use external crontab');
  295. $options += drupal_map_assoc(array(3600, 10800, 21600, 43200, 86400, 604800), 'format_interval');
  296. $form['installation']['cron_safe_threshold'] = array(
  297. '#type' => 'select',
  298. '#title' => t("Run cron on visitor's requests, every"),
  299. '#default_value' => variable_get('cron_safe_threshold', DRUPAL_CRON_DEFAULT_THRESHOLD),
  300. '#description' => t('Setting a time here will enable the "poormanscron" method, which runs the Drupal cron operation using normal browser/page requests instead of having to set up a crontab to request the cron.php script. This approach requires that your site gets regular traffic/visitors in order to trigger the cron request.')
  301. . '<br>'
  302. . t("This way is fine if you don't need a great control over job starting times and execution frequency.")
  303. . '<br />'
  304. . t('If you need fine-grained control over cron timings use the crontab method, as <a href="!cron_url">described in Drupal installation guide</a>.', array('!cron_url' => url('http://drupal.org/cron')))
  305. . '<br />'
  306. . t("If you have a very large site, or you need to execute some jobs very often (more than once an hour) refer to Elysia cron's INSTALL.TXT to improve main cron setup."),
  307. '#options' => $options,
  308. );
  309. $form['installation']['elysia_cron_queue_show_count'] = array(
  310. '#title' => t('Show the number of items in queues'),
  311. '#description' => t('Some queue backends may have performance issue related with counting items in queue. If you faced with it, just disable this option.'),
  312. '#type' => 'checkbox',
  313. '#default_value' => variable_get('elysia_cron_queue_show_count', TRUE),
  314. );
  315. $form['installation']['cron_key'] = array(
  316. '#title' => t('Cron key'),
  317. '#type' => 'textfield',
  318. '#default_value' => variable_get('cron_key'),
  319. '#description' => t("This is used to avoid external cron calling. If you set this cron will by accessible only by calling <em>http://site/cron.php?cron_key=XXX</em>, so you'll need to modify system crontab to support this (Logged users with <em>execute elysia_cron</em> permission avoid this check).
  320. <br>If you left this field empty, you can run cron without cron_key parameter, like this <em>http://site/cron.php</em>, but it <b>HIGHLY NOT RECOMMENDED</b>."),
  321. );
  322. $form['installation']['elysia_cron_allowed_hosts'] = array(
  323. '#title' => t('Allowed hosts'),
  324. '#type' => 'textfield',
  325. '#default_value' => variable_get('elysia_cron_allowed_hosts', ''),
  326. '#description' => t('Insert a list of ip addresses separated by , that can run cron.php (Logged user with [execute elysia_cron] permission avoid this check).'),
  327. );
  328. $form['installation']['elysia_cron_default_rule'] = array(
  329. '#title' => t('Default schedule rule'),
  330. '#type' => 'textfield',
  331. '#default_value' => variable_get('elysia_cron_default_rule', FALSE),
  332. '#description' => t("If you don't specify a rule for a process, and if it has not a module specified one, this rule will apply"),
  333. );
  334. if (!ini_get('safe_mode')) {
  335. $form['installation']['elysia_cron_time_limit'] = array(
  336. '#title' => t('Time limit'),
  337. '#type' => 'textfield',
  338. '#default_value' => variable_get('elysia_cron_time_limit', 240),
  339. '#description' => t('Set the number of seconds a channel is allowed to run. If you have some jobs that needs more time to execute increase it or set to 0 to disable the limit (WARN: that way a stuck job will block the channel forever!).'),
  340. );
  341. }
  342. $form['installation']['elysia_cron_stuck_time'] = array(
  343. '#title' => t('Stuck time'),
  344. '#type' => 'textfield',
  345. '#default_value' => variable_get('elysia_cron_stuck_time', 3600),
  346. '#description' => t('How many seconds the process should wait to consider the job as stuck (so the channel can run again)'),
  347. );
  348. $form['installation']['elysia_cron_debug_messages'] = array(
  349. '#title' => t('Debug'),
  350. '#type' => 'select',
  351. '#default_value' => variable_get('elysia_cron_debug_messages', 0),
  352. '#options' => array(
  353. 0 => t('Disabled'),
  354. 1 => t('Enabled'),
  355. ),
  356. '#description' => t('Enable extended logging (in watchdog)'),
  357. );
  358. $default_rules_human = '';
  359. $default_rules = variable_get('elysia_cron_default_rules', _elysia_cron_default_rules());
  360. foreach ($default_rules as $dk => $dr) {
  361. $default_rules_human .= $dr . ' = ' . $dk . PHP_EOL;
  362. }
  363. $form['installation']['elysia_cron_default_rules'] = array(
  364. '#title' => t('Predefined rules'),
  365. '#type' => 'textarea',
  366. '#rows' => 5,
  367. '#default_value' => $default_rules_human,
  368. '#description' => t('You can put here standard rules used in your system, each one with its own caption. Put each rule in a separate line, in the form "caption = rule". For example: <i>"every 15 minutes = */15 * * * *"</i>.'),
  369. );
  370. $form['installation']['elysia_cron_alert_fieldset'] = array(
  371. '#title' => t('External cron tracking'),
  372. '#type' => 'fieldset',
  373. '#collapsible' => TRUE,
  374. '#collapsed' => TRUE,
  375. '#description' => t('This lets you use an external tracking system like <a href="http://www.host-tracker.com/">Host Tracker</a> to be used to monitor the health of cron on your site. Point the tracking service to <a href="!cron-ping-url">!cron-ping-url</a>. If Elysia cron has been called within the time interval specified below, the ping page will return HTTP 200. If not, the ping page will throw a 404 (page not found).', array('!cron-ping-url' => url('admin/build/cron/ping'))),
  376. );
  377. $form['installation']['elysia_cron_alert_fieldset']['elysia_cron_alert_interval'] = array(
  378. '#title' => t('Lapse interval (minutes)'),
  379. '#type' => 'textfield',
  380. '#size' => 20,
  381. '#default_value' => variable_get('elysia_cron_alert_interval', 60),
  382. '#description' => t('Specify the number of minutes to allow to lapse before the cron ping page returns a 404 (page not found).'),
  383. );
  384. $form['elysia_cron_script_fieldset'] = array(
  385. '#title' => t('Script'),
  386. '#type' => 'fieldset',
  387. '#collapsible' => TRUE,
  388. '#collapsed' => !variable_get('elysia_cron_script', ''),
  389. );
  390. $form['elysia_cron_script_fieldset']['elysia_cron_script'] = array(
  391. '#type' => 'textarea',
  392. '#rows' => 20,
  393. '#default_value' => variable_get('elysia_cron_script', ''),
  394. '#description' => t('You can specify new cron jobs or modify existing schedules by adding lines to the script.')
  395. . '<br />'
  396. . t('<b>Warning</b> All rules specified in the script will OVERRIDE single job settings and channel settings (sections below).'),
  397. );
  398. $form['single_job'] = array(
  399. '#title' => t('Single job settings'),
  400. '#description' => '<b>' . t('Disabled') . '</b>: ' . t('Flag this to disable job execution') . '<br />'
  401. . '<b>' . t('Schedule rule') . '</b>: ' . t('Timing rule for the job. Leave empty to use default rule (shown after the field in parenthesis)') . '<br />'
  402. . '<b>' . t('Weight') . '</b>: ' . t('Use this to specify execution order: low weights are executed before high weights. Default value shown in parenthesis') . '<br />'
  403. . '<b>' . t('Channel') . '</b>: ' . t('Specify a channel for the job (create the channel if not exists)') . '<br /><br />',
  404. '#type' => 'fieldset',
  405. '#collapsible' => TRUE,
  406. );
  407. foreach ($_elysia_cron_settings_by_channel as $channel => $cconf) {
  408. foreach ($cconf as $job => $conf) {
  409. if ($job != '#data' && empty($conf['expression'])) {
  410. $form['single_job']['elysia_cron_' . $job] = array(
  411. '#title' => $job,
  412. '#description' => elysia_cron_description($job),
  413. '#type' => 'fieldset',
  414. '#collapsible' => TRUE,
  415. '#collapsed' => !elysia_cron_get_job_rule($job) && !elysia_cron_get_job_weight($job) && !elysia_cron_is_job_disabled($job) && !elysia_cron_get_job_channel($job),
  416. );
  417. $rule = elysia_cron_get_job_rule($job);
  418. $options = array_merge(array('default' => t('Default') . ' (' . (!empty($default_rules[$conf['default_rule']]) ? $default_rules[$conf['default_rule']] : $conf['default_rule']) . ')'), $default_rules);
  419. if ($rule && !isset($options[$rule])) {
  420. $options[$rule] = $rule;
  421. }
  422. $options['custom'] = t('Custom') . ' ...';
  423. $form['single_job']['elysia_cron_' . $job]['_elysia_cron_seljob_rule_' . $job] = array(
  424. '#title' => t('Schedule rule'),
  425. '#type' => 'select',
  426. '#options' => $options,
  427. '#default_value' => $rule ? $rule : 'default',
  428. );
  429. $form['single_job']['elysia_cron_' . $job]['_elysia_cron_job_rule_' . $job] = array(
  430. '#title' => t('Schedule rule'),
  431. '#type' => 'textfield',
  432. '#size' => 20,
  433. '#default_value' => $rule ? $rule : $conf['default_rule'],
  434. );
  435. $form['single_job']['elysia_cron_' . $job]['_elysia_cron_job_weight_' . $job] = array(
  436. '#title' => t('Weight'),
  437. '#type' => 'textfield',
  438. '#size' => 4,
  439. '#default_value' => elysia_cron_get_job_weight($job),
  440. '#description' => '(' . $conf['default_weight'] . ')',
  441. );
  442. $form['single_job']['elysia_cron_' . $job]['_elysia_cron_job_disabled_' . $job] = array(
  443. '#title' => t('Disabled'),
  444. '#type' => 'checkbox',
  445. '#default_value' => elysia_cron_is_job_disabled($job, FALSE),
  446. );
  447. $form['single_job']['elysia_cron_' . $job]['_elysia_cron_job_channel_' . $job] = array(
  448. '#title' => t('Channel'),
  449. '#type' => 'textfield',
  450. '#size' => 20,
  451. '#default_value' => elysia_cron_get_job_channel($job),
  452. );
  453. }
  454. }
  455. }
  456. $form['channels'] = array(
  457. '#title' => t('Channels settings'),
  458. '#type' => 'fieldset',
  459. '#collapsible' => TRUE,
  460. );
  461. foreach ($_elysia_cron_settings_by_channel as $channel => $conf) {
  462. $form['channels']['elysia_cron_ch_' . $channel] = array(
  463. '#title' => $channel,
  464. '#type' => 'fieldset',
  465. );
  466. $form['channels']['elysia_cron_ch_' . $channel]['_elysia_cron_ch_disabled_' . $channel] = array(
  467. '#title' => t('Disabled'),
  468. '#type' => 'checkbox',
  469. '#default_value' => elysia_cron_is_channel_disabled($channel, ''),
  470. );
  471. $form['channels']['elysia_cron_ch_' . $channel]['_elysia_cron_ch_rule_' . $channel] = array(
  472. '#title' => t('Default schedule rule'),
  473. '#type' => 'textfield',
  474. '#size' => 20,
  475. '#default_value' => elysia_cron_get_channel_rule($channel),
  476. );
  477. }
  478. $form['buttons'] = array('#type' => 'actions');
  479. $form['buttons']['submit'] = array(
  480. '#type' => 'submit',
  481. '#value' => t('Save configuration'),
  482. );
  483. $form['buttons']['reset'] = array(
  484. '#type' => 'submit',
  485. '#value' => t('Reset to defaults'),
  486. );
  487. if (!empty($_POST) && form_get_errors()) {
  488. elysia_cron_error('The settings have not been saved because of the errors.');
  489. }
  490. return $form;
  491. }
  492. /**
  493. * Theme function for general settings form.
  494. *
  495. * @param array $variables
  496. * Theme vars.
  497. *
  498. * @return string
  499. * Ready for print HTML.
  500. */
  501. function theme_elysia_cron_settings_form(array &$variables) {
  502. $form = &$variables['form'];
  503. $coutput = '<table>';
  504. $i = 0;
  505. foreach (element_children($form['single_job']) as $c) {
  506. $key = substr($c, 12);
  507. if ($i++ == 0) {
  508. $coutput .= '<tr>'
  509. . '<th>' . $form['single_job'][$c]['_elysia_cron_job_disabled_' . $key]['#title'] . '</th>'
  510. . '<th>' . $form['single_job'][$c]['_elysia_cron_job_rule_' . $key]['#title'] . '</th>'
  511. . '<th colspan="2">' . $form['single_job'][$c]['_elysia_cron_job_weight_' . $key]['#title'] . '</th>'
  512. . '<th>' . $form['single_job'][$c]['_elysia_cron_job_channel_' . $key]['#title'] . '</th>'
  513. . '</tr>';
  514. }
  515. $def_weight = $form['single_job'][$c]['_elysia_cron_job_weight_' . $key]['#description'];
  516. $posted_key = $form['single_job'][$c]['_elysia_cron_seljob_rule_' . $key]['#name'];
  517. $posted_val = !empty($_REQUEST[$posted_key]) ? $_REQUEST[$posted_key] : FALSE;
  518. $form['single_job'][$c]['_elysia_cron_job_rule_' . $key]['#prefix'] = '<span id="_ec_custom_' . $key . '" style="' . ($posted_val != 'custom' ? 'display: none;' : '') . '">';
  519. $form['single_job'][$c]['_elysia_cron_job_rule_' . $key]['#suffix'] = '</span>';
  520. $form['single_job'][$c]['_elysia_cron_job_rule_' . $key]['#title'] = NULL;
  521. $form['single_job'][$c]['_elysia_cron_job_rule_' . $key]['#description'] = NULL;
  522. $form['single_job'][$c]['_elysia_cron_seljob_rule_' . $key]['#prefix'] = '<span id="_ec_select_' . $key . '" style="' . ($posted_val == 'custom' ? 'display: none;' : '') . '">';
  523. $form['single_job'][$c]['_elysia_cron_seljob_rule_' . $key]['#suffix'] = '</span>';
  524. $form['single_job'][$c]['_elysia_cron_seljob_rule_' . $key]['#title'] = NULL;
  525. $form['single_job'][$c]['_elysia_cron_seljob_rule_' . $key]['#description'] = NULL;
  526. $form['single_job'][$c]['_elysia_cron_seljob_rule_' . $key]['#attributes']['class'][] = 'ec-select';
  527. $form['single_job'][$c]['_elysia_cron_seljob_rule_' . $key]['#attributes']['data-key'] = $key;
  528. $form['single_job'][$c]['_elysia_cron_job_weight_' . $key]['#title'] = NULL;
  529. $form['single_job'][$c]['_elysia_cron_job_weight_' . $key]['#description'] = NULL;
  530. $form['single_job'][$c]['_elysia_cron_job_weight_' . $key]['#attributes']['style'] = 'margin: 0';
  531. $form['single_job'][$c]['_elysia_cron_job_disabled_' . $key]['#title'] = NULL;
  532. $form['single_job'][$c]['_elysia_cron_job_disabled_' . $key]['#attributes']['style'] = 'margin: 0';
  533. $form['single_job'][$c]['_elysia_cron_job_channel_' . $key]['#title'] = NULL;
  534. $form['single_job'][$c]['_elysia_cron_job_channel_' . $key]['#attributes']['style'] = 'margin: 0';
  535. $coutput .= '<tr><td colspan="6"><b>' . $form['single_job'][$c]['#title'] . '</b>' . (($d = $form['single_job'][$c]['#description']) && $d != '-' ? ' <i>(' . $d . ')</i>' : '') . '</td></tr>';
  536. $coutput .= '<tr>'
  537. . '<td align="center">' . drupal_render($form['single_job'][$c]['_elysia_cron_job_disabled_' . $key]) . '</td>'
  538. . '<td>' . drupal_render($form['single_job'][$c]['_elysia_cron_seljob_rule_' . $key]) . drupal_render($form['single_job'][$c]['_elysia_cron_job_rule_' . $key]) . '</td>'
  539. . '<td>' . drupal_render($form['single_job'][$c]['_elysia_cron_job_weight_' . $key]) . '</td><td><small>' . $def_weight . '</small></td>'
  540. . '<td>' . drupal_render($form['single_job'][$c]['_elysia_cron_job_channel_' . $key]) . '</td>'
  541. . '</tr>';
  542. drupal_render($form['single_job'][$c]);
  543. }
  544. $coutput .= '</table>';
  545. $form['single_job']['#children'] = $coutput;
  546. $coutput = '<table>';
  547. $i = 0;
  548. foreach (element_children($form['channels']) as $c) {
  549. $key = substr($c, 15);
  550. if ($i++ == 0) {
  551. $coutput .= '<tr>'
  552. . '<th>' . t('Name') . '</th>'
  553. . '<th>' . $form['channels'][$c]['_elysia_cron_ch_disabled_' . $key]['#title'] . '</th>'
  554. . '<th>' . $form['channels'][$c]['_elysia_cron_ch_rule_' . $key]['#title'] . '</th>'
  555. . '</tr>';
  556. }
  557. $form['channels'][$c]['_elysia_cron_ch_disabled_' . $key]['#title'] = NULL;
  558. $form['channels'][$c]['_elysia_cron_ch_disabled_' . $key]['#attributes']['style'] = 'margin: 0';
  559. $form['channels'][$c]['_elysia_cron_ch_rule_' . $key]['#title'] = NULL;
  560. $form['channels'][$c]['_elysia_cron_ch_rule_' . $key]['#attributes']['style'] = 'margin: 0';
  561. $coutput .= '<tr>'
  562. . '<td><b>' . $form['channels'][$c]['#title'] . '</b></td>'
  563. . '<td>' . drupal_render($form['channels'][$c]['_elysia_cron_ch_disabled_' . $key]) . '</td>'
  564. . '<td>' . drupal_render($form['channels'][$c]['_elysia_cron_ch_rule_' . $key]) . '</td>'
  565. . '</tr>';
  566. drupal_render($form['channels'][$c]);
  567. }
  568. $coutput .= '</table>';
  569. $form['channels']['#children'] = $coutput;
  570. return drupal_render_children($form);
  571. }
  572. /**
  573. * Validate handler for 'elysia_cron_settings_form' form.
  574. *
  575. * @param array $form
  576. * Form API array.
  577. * @param array $form_state
  578. * Filled form_state array with input values.
  579. */
  580. function elysia_cron_settings_form_validate(array $form, array &$form_state) {
  581. $values = $form_state['values'];
  582. $script = $form_state['values']['elysia_cron_script'];
  583. if ($script) {
  584. $errors = elysia_cron_decode_script($script, FALSE);
  585. if ($errors) {
  586. form_set_error('elysia_cron_script', t('Invalid lines:') . implode('<br>', $errors));
  587. }
  588. }
  589. foreach ($values as $key => $value) {
  590. if ($value && ((preg_match('/^_elysia_cron_([^_]+_[^_]+)_(.*)$/', $key, $r) && ($r[1] == 'job_rule' || $r[1] == 'ch_rule')) || $key == 'elysia_cron_default_rule')) {
  591. if (!preg_match('/^\\s*([0-9*,\/-]+[ ]+[0-9*,\/-]+[ ]+[0-9*,\/-]+[ ]+[0-9*,\/-]+[ ]+[0-9*,\/-]+)\\s*$/', $value)) {
  592. form_set_error($key, t('Invalid rule: %rule', array('%rule' => $value)));
  593. }
  594. }
  595. }
  596. if (!empty($values['elysia_cron_default_rules'])) {
  597. $rules = explode(PHP_EOL, $values['elysia_cron_default_rules']);
  598. foreach ($rules as $rule) {
  599. $rule = trim($rule);
  600. if (empty($rule)) {
  601. continue;
  602. }
  603. $rule = explode('=', $rule);
  604. if (empty($rule[1])) {
  605. form_set_error('elysia_cron_default_rules', t('Invalid rule: %rule', array('%rule' => $rule[0])));
  606. }
  607. elseif (!preg_match('/^\\s*([0-9*,\/-]+[ ]+[0-9*,\/-]+[ ]+[0-9*,\/-]+[ ]+[0-9*,\/-]+[ ]+[0-9*,\/-]+)\\s*$/', trim($rule[1]))) {
  608. form_set_error('elysia_cron_default_rules', t('Invalid rule: %rule', array('%rule' => $rule[0])));
  609. }
  610. }
  611. }
  612. }
  613. /**
  614. * Submit handler for 'elysia_cron_settings_form' form.
  615. *
  616. * @param array $form
  617. * Form API array.
  618. * @param array $form_state
  619. * Filled form_state array with input values.
  620. */
  621. function elysia_cron_settings_form_submit(array $form, array &$form_state) {
  622. $form_values = $form_state['values'];
  623. $op = isset($form_values['op']) ? $form_values['op'] : '';
  624. // Exclude unnecessary elements.
  625. unset($form_values['submit'], $form_values['reset'], $form_values['form_id'], $form_values['op'], $form_values['form_token']);
  626. $elysia_cron_default_rules = array();
  627. $rules = explode(PHP_EOL, $form_values['elysia_cron_default_rules']);
  628. foreach ($rules as $r) {
  629. if (trim($r)) {
  630. $rr = explode("=", $r);
  631. $elysia_cron_default_rules[trim($rr[1])] = trim($rr[0]);
  632. }
  633. }
  634. variable_set('elysia_cron_default_rules', $elysia_cron_default_rules);
  635. foreach ($form_values as $key => $value) {
  636. $value = trim($value);
  637. if (!preg_match('/^_elysia_cron_([^_]+_[^_]+)_(.*)$/', $key, $r)) {
  638. if ($op == t('Reset to defaults') || ($key != 'cron_safe_threshold' && !$value)) {
  639. variable_del($key);
  640. }
  641. elseif ($key != 'elysia_cron_default_rules') {
  642. if (is_array($value) && isset($form_values['array_filter'])) {
  643. $value = array_keys(array_filter($value));
  644. }
  645. variable_set($key, $value);
  646. }
  647. }
  648. else {
  649. $nullvalue = $r[1] != 'job_weight' ? !$value : !$value && $value !== '0';
  650. if ($op == t('Reset to defaults') || $nullvalue) {
  651. switch ($r[1]) {
  652. case 'job_channel':
  653. elysia_cron_reset_job_channel($r[2]);
  654. break;
  655. case 'job_rule':
  656. elysia_cron_reset_job_rule($r[2]);
  657. break;
  658. case 'job_weight':
  659. elysia_cron_reset_job_weight($r[2]);
  660. break;
  661. case 'job_disabled':
  662. elysia_cron_reset_job_disabled($r[2]);
  663. break;
  664. case 'ch_disabled':
  665. elysia_cron_reset_channel_disabled($r[2]);
  666. break;
  667. case 'ch_rule':
  668. elysia_cron_reset_channel_rule($r[2]);
  669. break;
  670. }
  671. }
  672. else {
  673. switch ($r[1]) {
  674. case 'job_channel':
  675. elysia_cron_set_job_channel($r[2], $value);
  676. break;
  677. case 'job_rule':
  678. if ($form_values['_elysia_cron_seljob_rule_' . $r[2]] == 'custom') {
  679. elysia_cron_set_job_rule($r[2], $value);
  680. }
  681. break;
  682. case 'seljob_rule':
  683. if ($value != 'custom') {
  684. if ($value == 'default') {
  685. elysia_cron_reset_job_rule($r[2]);
  686. }
  687. else {
  688. elysia_cron_set_job_rule($r[2], $value);
  689. }
  690. }
  691. break;
  692. case 'job_weight':
  693. elysia_cron_set_job_weight($r[2], $value);
  694. break;
  695. case 'job_disabled':
  696. elysia_cron_set_job_disabled($r[2], $value);
  697. break;
  698. case 'ch_disabled':
  699. elysia_cron_set_channel_disabled($r[2], $value);
  700. break;
  701. case 'ch_rule':
  702. elysia_cron_set_channel_rule($r[2], $value);
  703. break;
  704. }
  705. }
  706. }
  707. }
  708. if ($op == t('Reset to defaults')) {
  709. elysia_cron_message('The configuration options have been reset to their default values.');
  710. }
  711. else {
  712. elysia_cron_message('The configuration options have been saved.');
  713. }
  714. }
  715. /**
  716. * Build time in "ago" format.
  717. *
  718. * @param int $timestamp
  719. * Time of latest cron.
  720. *
  721. * @return string
  722. * Date in 'ago' format.
  723. */
  724. function elysia_cron_date($timestamp) {
  725. return $timestamp > 0 ? t('!time ago', array('!time' => format_interval(REQUEST_TIME - $timestamp, 2))) : t('n/a');
  726. }
  727. /**
  728. * Form builder for cron run form.
  729. *
  730. * @return array
  731. * From API array.
  732. */
  733. function elysia_cron_run_form() {
  734. $form['runf'] = array(
  735. '#type' => 'fieldset',
  736. '#access' => elysia_cron_access('execute elysia_cron'),
  737. );
  738. $form['runf']['run'] = array(
  739. '#type' => 'submit',
  740. '#value' => t('Run cron'),
  741. );
  742. return $form;
  743. }
  744. /**
  745. * Submit handler for 'elysia_cron_run_form' form.
  746. *
  747. * @param array $form
  748. * Form API array.
  749. * @param array $form_state
  750. * Filled form_state array with input values.
  751. */
  752. function elysia_cron_run_form_submit(array $form, array &$form_state) {
  753. // Run cron manually from Cron form.
  754. if (elysia_cron_run(TRUE)) {
  755. elysia_cron_message('Cron run successfully.');
  756. }
  757. else {
  758. elysia_cron_error('Cron run failed.');
  759. }
  760. drupal_goto('admin/config/system/cron');
  761. }
  762. /**
  763. * Page callback for 'admin/config/system/cron/execute/%' path.
  764. *
  765. * @param string $job
  766. * Name of cron job to be executed.
  767. */
  768. function elysia_cron_execute_page($job = '') {
  769. if (!$job) {
  770. elysia_cron_error('No job specified');
  771. drupal_goto('admin/config/system/cron');
  772. }
  773. // Run also if disabled or not ready (but not if it's already running).
  774. elysia_cron_run_job($job, TRUE, TRUE, FALSE);
  775. drupal_goto('admin/config/system/cron');
  776. }
  777. /**
  778. * Form builder for statistic reset form.
  779. *
  780. * @return array
  781. * From API array.
  782. */
  783. function elysia_cron_reset_statistics_form() {
  784. $form['fieldset'] = array(
  785. '#type' => 'fieldset',
  786. '#title' => t('Reset statistics'),
  787. '#description' => t('Deletes all cron execution statistics (Last run, last exec time, exec count, avg/max exec time...). Do not touch cron settings.<br /><b>This operation can not be reverted</b><br />'),
  788. );
  789. $form['fieldset']['reset'] = array(
  790. '#type' => 'submit',
  791. '#value' => t('Reset'),
  792. '#attributes' => array(
  793. 'onclick' => 'return confirm(\'' . htmlentities(t('Are you sure you want to reset statistics?')) . '\')',
  794. ),
  795. );
  796. return $form;
  797. }
  798. /**
  799. * Submit handler for 'elysia_cron_reset_statistics_form' form.
  800. *
  801. * @param array $form
  802. * Form API array.
  803. * @param array $form_state
  804. * Filled form_state array with input values.
  805. */
  806. function elysia_cron_reset_statistics_form_submit(array $form, array &$form_state) {
  807. elysia_cron_reset_stats();
  808. elysia_cron_message('Reset done.');
  809. drupal_goto('admin/config/system/cron/maintenance');
  810. }