lassie.module

This is the primary module file for the lassie module.

File

modules/lassie/lassie.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * This is the primary module file for the lassie module.
  5. */
  6. /**
  7. * Converts the string into a compatible "machine name" for Lassie to use.
  8. */
  9. function lassie_get_machine_name($str) {
  10. $str = fp_get_machine_readable($str);
  11. $str = trim(substr($str, 0, 200)); // make sure the name isn't more than 200 chars, which might cause problems.
  12. return $str;
  13. }
  14. /**
  15. * Set a new job for Lassie to watch.
  16. *
  17. * @param $job_machine_name - The machine name of this job. Must be alphanumeric + underscore. Ex: job_123
  18. * @param $hours - must be integer or a warning will be logged and displayed via fpm().
  19. * @param $emails - comma separated listed of email addresses to notify if the job fails to finish.
  20. */
  21. function lassie_start($job_machine_name, $hours = 2, $emails = "") {
  22. global $user;
  23. if (!is_int($hours)) {
  24. fpm("WARNING: hours ($hours) supplied to lassie_start() function is not an integer. Casting to int.");
  25. fpm(debug_backtrace());
  26. watchdog('lassie', "WARNING: hours ($hours) supplied to lassie_start() function is not integer. Casting to int. Backtrace: <pre>" . print_r(debug_backtrace(), TRUE) . "</pre>", array(), WATCHDOG_ERROR);
  27. $hours = intval($hours);
  28. }
  29. $job_machine_name = lassie_get_machine_name($job_machine_name);
  30. $data = array(
  31. 'start' => time(),
  32. 'hours' => $hours,
  33. 'expires' => strtotime("NOW + $hours HOURS"),
  34. 'emails' => $emails,
  35. 'user_uid' => $user->uid,
  36. );
  37. variable_set('lassie_job__' . $job_machine_name, $data);
  38. watchdog('lassie', t("Beginning new job: ") . $job_machine_name . ". " . print_r($data, TRUE));
  39. }
  40. /**
  41. * Tell Lassie that we have finished the job.
  42. */
  43. function lassie_finish($job_machine_name) {
  44. $job_machine_name = lassie_get_machine_name($job_machine_name);
  45. $data = variable_get('lassie_job__' . $job_machine_name, array());
  46. $start = $data['start'];
  47. $elapsed = round((time() - $start) / 60, 3);
  48. watchdog('lassie', t("Finished job: ") . $job_machine_name . ". " . $elapsed . " minutes elapsed.");
  49. // TODO: Implement a hook?
  50. db_query("DELETE FROM variables WHERE name = ?", array("lassie_job__$job_machine_name"));
  51. }
  52. /**
  53. * Check to see if an email needs to be sent regarding this job.
  54. */
  55. function lassie_check($job_machine_name) {
  56. $job_machine_name = lassie_get_machine_name($job_machine_name);
  57. $data = variable_get('lassie_job__' . $job_machine_name, array());
  58. $start = $data['start'];
  59. $elapsed = round((time() - $start) / 60, 3);
  60. $expires = $data['expires'];
  61. if (time() > $expires) {
  62. // YES! We need to trigger the email, this job has expired.
  63. $emails = $data['emails'];
  64. // TODO: also include any default emails from settings.
  65. $hours = $data['hours'];
  66. $user_uid = $data['user_uid'];
  67. // TODO: Implement a hook so other modules can act?
  68. // Implement the warning email.
  69. $msg = "";
  70. $msg .= "FlightPath Lassie module reports that a job has failed to complete in the expected time.\n\n";
  71. $msg .= "Current Time: " . format_date(convert_time(time())) . "\n-------------------------\n";
  72. $msg .= "Site: " . $GLOBALS['fp_system_settings']["base_url"] . "\n";
  73. $msg .= "Job Name: $job_machine_name \n";
  74. $msg .= "Started: " . format_date(convert_time($start)) . "\n";
  75. $msg .= "Hours : $hours \n";
  76. $msg .= "Expires: " . format_date(convert_time($expires)) . "\n";
  77. $msg .= "Elapsed: $elapsed min.";
  78. if ($emails != "") {
  79. fp_mail($emails, "FlightPath job failed to end: $job_machine_name", $msg);
  80. }
  81. // Delete from variables table.
  82. db_query("DELETE FROM variables WHERE name = ?", array("lassie_job__$job_machine_name"));
  83. watchdog('lassie', t("Expired job: ") . $job_machine_name . ". " . $elapsed . " minutes elapsed.", array(), WATCHDOG_ERROR);
  84. }
  85. }
  86. /**
  87. * Check all of the jobs we are currently waiting on.
  88. */
  89. function lassie_check_all() {
  90. $res = db_query("SELECT * FROM variables WHERE name LIKE 'lassie_job__%' ");
  91. while ($cur = db_fetch_array($res)) {
  92. $name = $cur['name'];
  93. $job_name = str_replace("lassie_job__", "", $name);
  94. lassie_check($job_name);
  95. }
  96. }
  97. /**
  98. * Implements hook_cron
  99. */
  100. function lassie_cron() {
  101. // If its been less than 45 minutes since last check, then skip. Saves checks if our cron is running very frequently.
  102. $last_check = variable_get("lassie_last_check", 0);
  103. if (time() < ($last_check + (45 * 60))) return;
  104. watchdog("lassie", "Lassie checking for any long-running jobs....", array(), WATCHDOG_DEBUG);
  105. lassie_check_all();
  106. variable_set("lassie_last_check", time());
  107. }

Functions

Namesort descending Description
lassie_check Check to see if an email needs to be sent regarding this job.
lassie_check_all Check all of the jobs we are currently waiting on.
lassie_cron Implements hook_cron
lassie_finish Tell Lassie that we have finished the job.
lassie_get_machine_name Converts the string into a compatible "machine name" for Lassie to use.
lassie_start Set a new job for Lassie to watch.