summaryrefslogtreecommitdiff
path: root/modules/cron.module
blob: d316b0b3996df083b0f8faf2ff291c9097879213 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php

$module = array("help" => "cron_help",
                "admin" => "cron_admin");

function cron_help() {
 ?>
  <P>Cron (which stands for chronograph) is a periodic command scheduler: it executes commands at intervals specified in seconds.  It can be used to control the execution of daily, weekly and monthly jobs (or anything with a period of <i>n</i> seconds).   Automating tasks is one of the best ways to keep a system running smoothly, and if most of your administration does not require your direct involvement, cron is an ideal solution.</P>
  <P>Note that cron does not guarantee the commands will be executed at the specified interval.  However, the engine will make sure that the commands are run as close to the specified intervals as possible.</P>
  <P>Check the documentation page for more information about cron and how to setup it correctly.</P>
 <?php
}

function cron_save($edit) {
  foreach ($edit as $key=>$value) {
    db_query("UPDATE crons SET scheduled = '$value' WHERE module = '$key'");
  }
}

function cron_execute($name) {
  global $repository;
  watchdog("message", "cron: executed '". $name ."_cron()'");
  $repository[$name]["cron"]();
  db_query("UPDATE crons SET timestamp = ". time() ." WHERE module = '$name'");
}

function cron_display() {
  $intervals = array(300, 900, 1800, 3600, 7200, 10800, 21600, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200);

  // Perform query:
  $result = db_query("SELECT * FROM crons");

  // Generate output:
  $output .= "<FORM ACTION=\"admin.php?mod=cron\" METHOD=\"post\">\n";
  $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
  $output .= " <TR><TH>module</TH><TH>period</TH><TH>last execution</TH><TH>operations</TH></TR>\n";
  while ($cron = db_fetch_object($result)) {
    foreach ($intervals as $value) $period .= "<OPTION VALUE=\"$value\"". (($cron->scheduled == $value) ? " SELECTED" : "") .">every ". format_interval($value) ."</OPTION>\n";
    $output .= " <TR><TD>". check_output($cron->module) ."</TD><TD><SELECT NAME=\"edit[$cron->module]\">$period</SELECT></TD><TD ALIGN=\"center\">". ($cron->timestamp ? format_interval(time() - $cron->timestamp) ." ago" : "never" )."</TD><TD ALIGN=\"center\"><A HREF=\"admin.php?mod=cron&op=execute&name=$cron->module\">execute</A></TD></TR>\n";
    unset($period);
  }
  $output .= "</TABLE>\n";
  $output .= "<INPUT NAME=\"op\" TYPE=\"submit\" VALUE=\"Save crons\">\n";
  $output .= "</FORM>\n";
  print $output;
}

function cron_admin() {
  global $op, $edit, $name;

  print "<SMALL><A HREF=\"admin.php?mod=cron\">overview</A> | <A HREF=\"admin.php?mod=cron&op=help\">help</A></SMALL><HR>\n";

  switch($op) {
    case "help":
      cron_help();
      break;
    case "execute":
      cron_execute($name);
      cron_display();
      break;
    case "Save crons":
      cron_save($edit);
      // fall through
    default:
      cron_display();
  }
}

?>