elysia_cron.admin.inc 35 KB

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