PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/lib/quartz.php

http://prails.googlecode.com/
PHP | 168 lines | 126 code | 12 blank | 30 comment | 42 complexity | 30d91df684080869bd82e838909ae82d MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. Prails Web Framework
  4. Copyright (C) 2011 Robert Kunze
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. class Quartz {
  17. /**
  18. * checks if Quartz can be used on current server.
  19. */
  20. static function isAvailable() {
  21. $disabled = explode(', ', ini_get('disable_functions'));
  22. if(in_array('exec', $disabled)) return false;
  23. if (!Quartz::_checkApp("crontab")) return false;
  24. return Quartz::_getFirstAvailable() != false;
  25. }
  26. /**
  27. * schedules a new job, if it already existed, the new one isn't added
  28. * @param ARRAY $time array ("min" => "0", "hour" => "5", "day" => "*", "month" => "*", "week" => "*")
  29. * @param STRING $event event's name (for example user:notify)
  30. * @return job's ID if the job has been scheduled successfully, else FALSE
  31. */
  32. static function addJob($time, $event, $id = null) {
  33. global $SERVER, $log;
  34. if (!Quartz::isAvailable()) {
  35. $log->error("Tried to schedule job ".$event." but Quartz found no full cron support. Please check if crontab and lynx, w3m or wget is available");
  36. return false;
  37. }
  38. $time = Quartz::_normalizeTime($time);
  39. if (!$id) $id = md5(implode($time).$event);
  40. exec("crontab -l > cache/temp.cron");
  41. $cron = file("cache/temp.cron");
  42. foreach ($cron as $line) {
  43. if (preg_match('@\s*#\s*[$]id:\s*([a-zA-Z0-9]+)\s*$@', $line, $match)) {
  44. if ($id == $match[1]) {
  45. return $id;
  46. }
  47. }
  48. }
  49. $base = dirname($_SERVER["SCRIPT_FILENAME"]);
  50. $mail = ">> ".$base."/log/quartz.log 2>&1";
  51. $prog = (Quartz::_getFirstAvailable())." '".str_replace("://", "://".$_SERVER["PHP_AUTH_USER"].":".$_SERVER["PHP_AUTH_PW"]."@", $SERVER)."?event=".$event."'";
  52. $cron[] = $time["min"]." ".$time["hour"]." ".$time["day"]." ".$time["month"]." ".$time["week"]." ".$prog." ".$mail." # \$id: ".$id;
  53. file_put_contents("cache/temp.cron", implode("\n", $cron)."\n");
  54. exec("crontab cache/temp.cron");
  55. unlink("cache/temp.cron");
  56. return $id;
  57. }
  58. /**
  59. * removes a scheduled job
  60. * @param MIXED $idTime job's ID or a time array
  61. * @param MIXED $event event's name in case no ID has been specified
  62. * @return TRUE if successfully removed, else false
  63. */
  64. static function removeJob($idTime, $event = false, $id = null) {
  65. if (!Quartz::isAvailable()) {
  66. $log->error("Tried to remove scheduled job ".$event." but Quartz found no full cron support.");
  67. return false;
  68. }
  69. if (!$event && !$id) {
  70. $id = $idTime;
  71. } else {
  72. $idTime = Quartz::_normalizeTime($idTime);
  73. if (!$id) $id = md5(implode($idTime).$event);
  74. }
  75. exec("crontab -l > cache/temp.cron");
  76. $cron = file("cache/temp.cron");
  77. $file = "";
  78. $found = false;
  79. foreach ($cron as $line) {
  80. if (preg_match('@\s*#\s*[$]id:\s*([a-zA-Z0-9]+)\s*$@', $line, $match)) {
  81. if ($id != $match[1]) {
  82. $file .= $line."\n";
  83. } else {
  84. $found = true;
  85. }
  86. }
  87. }
  88. if ($found) {
  89. file_put_contents("cache/temp.cron", $file);
  90. exec("crontab cache/temp.cron");
  91. }
  92. unlink("cache/temp.cron");
  93. return $found;
  94. }
  95. static function getJob($id) {
  96. $lineData = null;
  97. exec("crontab -l > cache/temp.cron");
  98. $cron = file("cache/temp.cron");
  99. $file = "";
  100. $found = false;
  101. foreach ($cron as $line) {
  102. if (preg_match('@\s*#\s*[$]id:\s*([a-zA-Z0-9]+)\s*$@', $line, $match)) {
  103. if ($id == $match[1]) {
  104. $lineItems = explode(" ", $line);
  105. $lineData = Array(
  106. "min" => $lineItems[0],
  107. "hour" => $lineItems[1],
  108. "day" => $lineItems[2],
  109. "month" => $lineItems[3],
  110. "week" => $lineItems[4]
  111. );
  112. break;
  113. }
  114. }
  115. }
  116. unlink("cache/temp.cron");
  117. return $lineData;
  118. }
  119. static function _getFirstAvailable() {
  120. exec("whereis php", $list);
  121. if (strpos(implode("\n", $list), "php ") === false) return null;
  122. return "/usr/bin/env php -q ".__FILE__;
  123. }
  124. static function _checkApp($name) {
  125. $result = exec($name." 2>&1", $result, $returnValue);
  126. return $returnValue != 127;
  127. }
  128. static function _normalizeTime($time) {
  129. $entries = Array("min", "hour", "day", "month", "week");
  130. if (!is_array($time)) $time = Array();
  131. $foundStar = false;
  132. foreach ($entries as $entry) {
  133. if ($foundStar && ($entry == "day" || $entry == "month" || $entry == "hour")) {
  134. $time[$entry] = "*";
  135. } else if (!isset($time[$entry]) || strlen($time[$entry]) <= 0) {
  136. $time[$entry] = "*";
  137. $foundStar = true;
  138. } else if ($time[$entry][0] == "*") {
  139. $foundStar = true;
  140. } else {
  141. $time[$entry] = preg_replace('/0([0-9]+)/mi', '\1', $time[$entry]);
  142. }
  143. }
  144. return $time;
  145. }
  146. }
  147. if (defined('STDIN')) {
  148. // job is to be executed!
  149. if ($argc > 1) {
  150. preg_match('/[^?]+\?event=(.*)/mi', $argv[1], $match);
  151. echo "[".date("Y-m-d H:i:s")."] [".$match[1]."] ".file_get_contents($argv[1])."\n";
  152. } else {
  153. echo "[".date("Y-m-d H:i:s")."] ERROR - unable to find job to be executed!\n";
  154. }
  155. }
  156. ?>