PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/application/controllers/scheduler.php

http://github.com/ushahidi/Ushahidi_Web
PHP | 225 lines | 149 code | 33 blank | 43 comment | 31 complexity | 5935367214ce0a30cab90304f7169eea MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Scheduler Controller (FAUX Cron)
  4. * Generates 1x1 pixel image while executing scheduled tasks
  5. *
  6. * PHP version 5
  7. * LICENSE: This source file is subject to LGPL license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.gnu.org/copyleft/lesser.html
  10. * @author Ushahidi Team <team@ushahidi.com>
  11. * @package Ushahidi - http://source.ushahididev.com
  12. * @subpackage Controllers
  13. * @copyright Ushahidi - http://www.ushahidi.com
  14. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
  15. */
  16. class Scheduler_Controller extends Controller {
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. // Increase max execution time
  21. if (ini_get('max_execution_time') < 180)
  22. {
  23. ini_set('max_execution_time', 180);
  24. }
  25. // Set time limit only if we're not on safe_mode and set_time_limit is enabled
  26. $safe_mode_enabled = ini_get('safe_mode');
  27. $disabled_functions = ini_get('disable_functions');
  28. if (empty($safe_mode_enabled) && strstr($disabled_functions, "set_time_limit") === false)
  29. {
  30. set_time_limit(180);
  31. }
  32. }
  33. public function index()
  34. {
  35. // Debug
  36. $debug = "";
  37. // @todo abstract most of this into a library, especially locking
  38. // Ensure settings entry for `scheduler_lock` exists
  39. Database::instance()->query(
  40. "INSERT IGNORE INTO `".Kohana::config('database.default.table_prefix')."settings`
  41. (`key`, `value`) VALUES ('scheduler_lock', 0)");
  42. // Now try and update the scheduler_lock
  43. $result = Database::instance()->query(
  44. "UPDATE `".Kohana::config('database.default.table_prefix')."settings`
  45. SET `value` = UNIX_TIMESTAMP() + 180
  46. WHERE `value` < UNIX_TIMESTAMP() AND `key` = 'scheduler_lock';");
  47. // If no entries were changed, scheduler is already running
  48. if ($result->count() <= 0)
  49. {
  50. Kohana::log('info', 'Could not acquire scheduler lock');
  51. if (isset($_GET['debug']) AND $_GET['debug'] == 1)
  52. {
  53. echo 'Could not acquire scheduler lock';
  54. }
  55. return;
  56. }
  57. // Get all active scheduled items
  58. foreach (ORM::factory('scheduler')
  59. ->where('scheduler_active','1')
  60. ->find_all() as $scheduler)
  61. {
  62. $scheduler_id = $scheduler->id;
  63. $scheduler_last = $scheduler->scheduler_last;
  64. // Next run time
  65. $scheduler_weekday = $scheduler->scheduler_weekday;
  66. // Day of the week
  67. $scheduler_day = $scheduler->scheduler_day;
  68. // Day of the month
  69. $scheduler_hour = $scheduler->scheduler_hour;
  70. // Hour
  71. $scheduler_minute = $scheduler->scheduler_minute;
  72. // Minute
  73. // Controller that performs action
  74. $scheduler_controller = $scheduler->scheduler_controller;
  75. if ($scheduler_day <= -1)
  76. {
  77. // Ran every day?
  78. $scheduler_day = "*";
  79. }
  80. if ($scheduler_weekday <= -1)
  81. {
  82. // Ran every day?
  83. $scheduler_weekday = "*";
  84. }
  85. if ($scheduler_hour <= -1)
  86. {
  87. // Ran every hour?
  88. $scheduler_hour = "*";
  89. }
  90. if ($scheduler_minute <= -1)
  91. {
  92. // Ran every minute?
  93. $scheduler_minute = "*";
  94. }
  95. $scheduler_cron = $scheduler_minute . " " . $scheduler_hour . " " . $scheduler_day . " * " . $scheduler_weekday;
  96. //Start new cron parser instance
  97. $cron = new CronParser();
  98. if (!$cron->calcLastRan($scheduler_cron))
  99. {
  100. echo "Error parsing CRON";
  101. }
  102. $lastRan = $cron->getLastRan();
  103. //Array (0=minute, 1=hour, 2=dayOfMonth, 3=month, 4=week, 5=year)
  104. $cronRan = mktime($lastRan[1], $lastRan[0], 0, $lastRan[3], $lastRan[2], $lastRan[5]);
  105. if (isset($_GET['debug']) AND $_GET['debug'] == 1)
  106. {
  107. $debug .= "~~~~~~~~~~~~~~~~~~~~~~~~~~~" . "<BR />~~~~~~~~~~~~~~~~~~~~~~~~~~~" . "<BR />RUNNING: " . $scheduler->scheduler_name . "<BR />~~~~~~~~~~~~~~~~~~~~~~~~~~~" . "<BR /> LAST RUN: " . date("r", $scheduler_last) . "<BR /> LAST DUE AT: " . date('r', $cron->getLastRanUnix()) . "<BR /> SCHEDULE: <a href=\"http://en.wikipedia.org/wiki/Cron\" target=\"_blank\">" . $scheduler_cron . "</a>";
  108. }
  109. if ($scheduler_controller AND (!($scheduler_last > $cronRan) OR $scheduler_last == 0))
  110. {
  111. $run = FALSE;
  112. // Catch errors from missing scheduler or other bugs
  113. try {
  114. $dispatch = Dispatch::controller($scheduler_controller, "scheduler/");
  115. if ($dispatch instanceof Dispatch && method_exists($dispatch,'method'))
  116. {
  117. $run = $dispatch->method('index', '');
  118. }
  119. }
  120. catch (Exception $e)
  121. {
  122. // Nada.
  123. }
  124. // @todo allow tasks to save state between runs.
  125. if ($run !== FALSE)
  126. {
  127. // Set last time of last execution
  128. $schedule_time = time();
  129. $scheduler->scheduler_last = $schedule_time;
  130. $scheduler->save();
  131. // Record Action to Log
  132. $scheduler_log = new Scheduler_Log_Model();
  133. $scheduler_log->scheduler_id = $scheduler_id;
  134. $scheduler_log->scheduler_status = "200";
  135. $scheduler_log->scheduler_date = $schedule_time;
  136. $scheduler_log->save();
  137. if (isset($_GET['debug']) AND $_GET['debug'] == 1)
  138. {
  139. $debug .= "<BR /> STATUS: {{ EXECUTED }}";
  140. }
  141. }
  142. else
  143. {
  144. if (isset($_GET['debug']) AND $_GET['debug'] == 1)
  145. {
  146. $debug .= "<BR /> STATUS: {{ SCHEDULER NOT FOUND! }}";
  147. }
  148. }
  149. }
  150. else
  151. {
  152. if (isset($_GET['debug']) AND $_GET['debug'] == 1)
  153. {
  154. $debug .= "<BR /> STATUS: {{ NOT RUN }}";
  155. }
  156. }
  157. if (isset($_GET['debug']) AND $_GET['debug'] == 1)
  158. {
  159. //$debug .= "<BR /><BR />CRON DEBUG:<BR />".nl2br($cron->getDebug());
  160. $debug .= "<BR />~~~~~~~~~~~~~~~~~~~~~~~~~~~<BR />~~~~~~~~~~~~~~~~~~~~~~~~~~~<BR /><BR /><BR />";
  161. }
  162. }
  163. if (Kohana::config('cdn.cdn_gradual_upgrade') != FALSE)
  164. {
  165. cdn::gradual_upgrade();
  166. }
  167. // If DEBUG is TRUE echo DEBUG info instead of transparent GIF
  168. if (isset($_GET['debug']) AND $_GET['debug'] == 1)
  169. {
  170. echo $debug;
  171. if (isset($this->profiler))
  172. {
  173. echo $this->profiler->render(TRUE);
  174. }
  175. }
  176. else
  177. {
  178. // Transparent GIF
  179. Header("Content-type: image/gif");
  180. Header("Expires: Wed, 11 Nov 1998 11:11:11 GMT");
  181. Header("Cache-Control: no-cache");
  182. Header("Cache-Control: must-revalidate");
  183. Header("Content-Length: 49");
  184. echo pack('H*', '47494638396101000100910000000000ffffffff' . 'ffff00000021f90405140002002c000000000100' . '01000002025401003b');
  185. }
  186. // Release lock
  187. $result = Database::instance()->query(
  188. "UPDATE `".Kohana::config('database.default.table_prefix')."settings`
  189. SET `value` = 0
  190. WHERE `key` = 'scheduler_lock';");
  191. }
  192. }