public function index() { // Debug $debug = ""; // Get all active scheduled items foreach (ORM::factory('scheduler')->where('scheduler_active', '1')->find_all() as $scheduler) { $scheduler_id = $scheduler->id; $scheduler_last = $scheduler->scheduler_last; // Next run time $scheduler_weekday = $scheduler->scheduler_weekday; // Day of the week $scheduler_day = $scheduler->scheduler_day; // Day of the month $scheduler_hour = $scheduler->scheduler_hour; // Hour $scheduler_minute = $scheduler->scheduler_minute; // Minute // Controller that performs action $scheduler_controller = $scheduler->scheduler_controller; if ($scheduler_day <= -1) { // Ran every day? $scheduler_day = "*"; } if ($scheduler_weekday <= -1) { // Ran every day? $scheduler_weekday = "*"; } if ($scheduler_hour <= -1) { // Ran every hour? $scheduler_hour = "*"; } if ($scheduler_minute <= -1) { // Ran every minute? $scheduler_minute = "*"; } $scheduler_cron = $scheduler_minute . " " . $scheduler_hour . " " . $scheduler_day . " * " . $scheduler_weekday; //Start new cron parser instance $cron = new CronParser(); if (!$cron->calcLastRan($scheduler_cron)) { echo "Error parsing CRON"; } $lastRan = $cron->getLastRan(); //Array (0=minute, 1=hour, 2=dayOfMonth, 3=month, 4=week, 5=year) $cronRan = mktime($lastRan[1], $lastRan[0], 0, $lastRan[3], $lastRan[2], $lastRan[5]); if (isset($_GET['debug']) and $_GET['debug'] == 1) { $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>"; } if (!($scheduler_last > $cronRan) || $scheduler_last == 0) { $run = Dispatch::controller("{$scheduler_controller}", "scheduler/")->method('index', ''); if ($run !== FALSE) { // Set last time of last execution $schedule_time = time(); $scheduler->scheduler_last = $schedule_time; $scheduler->save(); // Record Action to Log $scheduler_log = new Scheduler_Log_Model(); $scheduler_log->scheduler_id = $scheduler_id; $scheduler_log->scheduler_name = $scheduler->scheduler_name; $scheduler_log->scheduler_status = "200"; $scheduler_log->scheduler_date = $schedule_time; $scheduler_log->save(); if (isset($_GET['debug']) and $_GET['debug'] == 1) { $debug .= "<BR /> STATUS: {{ EXECUTED }}"; } } else { if (isset($_GET['debug']) and $_GET['debug'] == 1) { $debug .= "<BR /> STATUS: {{ SCHEDULER NOT FOUND! }}"; } } } else { if (isset($_GET['debug']) and $_GET['debug'] == 1) { $debug .= "<BR /> STATUS: {{ NOT RUN }}"; } } if (isset($_GET['debug']) and $_GET['debug'] == 1) { //$debug .= "<BR /><BR />CRON DEBUG:<BR \>".nl2br($cron->getDebug()); $debug .= "<BR \\>~~~~~~~~~~~~~~~~~~~~~~~~~~~<BR />~~~~~~~~~~~~~~~~~~~~~~~~~~~<BR /><BR /><BR />"; } } // If DEBUG is TRUE echo DEBUG info instead of transparent GIF if (isset($_GET['debug']) and $_GET['debug'] == 1) { echo $debug; } else { // Transparent GIF Header("Content-type: image/gif"); Header("Expires: Wed, 11 Nov 1998 11:11:11 GMT"); Header("Cache-Control: no-cache"); Header("Cache-Control: must-revalidate"); Header("Content-Length: 49"); echo pack('H*', '47494638396101000100910000000000ffffffff' . 'ffff00000021f90405140002002c000000000100' . '01000002025401003b'); } }
function index() { $this->template->content = new View('admin/scheduler'); // Check if we should be running the scheduler and then do it if (isset($_GET['run_scheduler'])) { // Get all active scheduled items foreach (ORM::factory('scheduler')->where('scheduler_active', '1')->find_all() as $scheduler) { $s_controller = $scheduler->scheduler_controller; $run = Dispatch::controller("{$s_controller}", "scheduler/")->method('index', ''); if ($run !== FALSE) { // Set last time of last execution $schedule_time = time(); $scheduler->scheduler_last = $schedule_time; $scheduler->save(); // Record Action to Log $scheduler_log = new Scheduler_Log_Model(); $scheduler_log->scheduler_id = $scheduler->id; $scheduler_log->scheduler_name = $scheduler->scheduler_name; $scheduler_log->scheduler_status = "200"; $scheduler_log->scheduler_date = $schedule_time; $scheduler_log->save(); } } } // setup and initialize form field names $form = array('action' => '', 'schedule_id' => '', 'scheduler_weekday' => '', 'scheduler_day' => '', 'scheduler_hour' => '', 'scheduler_minute' => '', 'scheduler_active' => ''); // copy the form as errors, so the errors will be stored with keys corresponding to the form field names $errors = $form; $form_error = FALSE; $form_saved = FALSE; $form_action = ""; if ($_POST) { //print_r($_POST); $post = Validation::factory($_POST); // Add some filters $post->pre_filter('trim', TRUE); if ($post->action == 'a') { // Add some rules, the input field, followed by a list of checks, carried out in order $post->add_rules('scheduler_weekday', 'required', 'between[1,7]'); $post->add_rules('scheduler_day', 'required', 'between[-1,31]'); $post->add_rules('scheduler_hour', 'required', 'between[-1,23]'); $post->add_rules('scheduler_minute', 'required', 'between[-1,59]'); } if ($post->validate()) { $scheduler_id = $post->scheduler_id; $scheduler = new Scheduler_Model($scheduler_id); if ($post->action == 'v') { // Active/Inactive Action if ($scheduler->loaded == TRUE) { $scheduler->scheduler_active = $scheduler->scheduler_active == 1 ? 0 : 1; $scheduler->save(); $form_saved = TRUE; $form_action = strtoupper(Kohana::lang('ui_admin.modified')); } } else { // SAVE Schedule $scheduler->scheduler_weekday = $post->scheduler_weekday; $scheduler->scheduler_day = $post->scheduler_day; $scheduler->scheduler_hour = $post->scheduler_hour; $scheduler->scheduler_minute = $post->scheduler_minute; $scheduler->save(); $form_saved = TRUE; $form_action = strtoupper(Kohana::lang('ui_admin.added_edited')); } } else { // repopulate the form fields $form = arr::overwrite($form, $post->as_array()); // populate the error fields, if any $errors = arr::overwrite($errors, $post->errors('scheduler')); $form_error = TRUE; } } // Pagination $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => $this->items_per_page, 'total_items' => ORM::factory('scheduler')->count_all())); $schedules = ORM::factory('scheduler')->orderby('scheduler_name', 'asc')->find_all($this->items_per_page, $pagination->sql_offset); $this->template->content->weekday_array = array("-1" => "ALL", "0" => "Sunday", "1" => "Monday", "2" => "Tuesday", "3" => "Wednesday", "4" => "Thursday", "5" => "Friday", "6" => "Saturday"); for ($i = 0; $i <= 31; $i++) { $day_array = $i; } $this->template->content->day_array = $day_array; $day_array = array(); $day_array[-1] = "ALL"; for ($i = 1; $i <= 31; $i++) { $day_array[] = $i; } $this->template->content->day_array = $day_array; $hour_array = array(); $hour_array[-1] = "ALL"; for ($i = 0; $i <= 23; $i++) { $hour_array[] = $i; } $this->template->content->hour_array = $hour_array; $minute_array = array(); $minute_array[-1] = "ALL"; for ($i = 0; $i <= 59; $i++) { $minute_array[] = $i; } $this->template->content->minute_array = $minute_array; $this->template->content->form_error = $form_error; $this->template->content->form_saved = $form_saved; $this->template->content->form_action = $form_action; $this->template->content->pagination = $pagination; $this->template->content->total_items = $pagination->total_items; $this->template->content->schedules = $schedules; $this->template->content->errors = $errors; // Javascript Header $this->template->js = new View('admin/scheduler_js'); }
public function index() { // Debug $debug = ""; // @todo abstract most of this into a library, especially locking // Ensure settings entry for `scheduler_lock` exists Database::instance()->query("INSERT IGNORE INTO `" . Kohana::config('database.default.table_prefix') . "settings`\n\t\t\t(`key`, `value`) VALUES ('scheduler_lock', 0)"); // Now try and update the scheduler_lock $result = Database::instance()->query("UPDATE `" . Kohana::config('database.default.table_prefix') . "settings`\n\t\t\tSET `value` = UNIX_TIMESTAMP() + 180\n\t\t\tWHERE `value` < UNIX_TIMESTAMP() AND `key` = 'scheduler_lock';"); // If no entries were changed, scheduler is already running if ($result->count() <= 0) { Kohana::log('info', 'Could not acquire scheduler lock'); if (isset($_GET['debug']) and $_GET['debug'] == 1) { echo 'Could not acquire scheduler lock'; } return; } // Get all active scheduled items foreach (ORM::factory('scheduler')->where('scheduler_active', '1')->find_all() as $scheduler) { $scheduler_id = $scheduler->id; $scheduler_last = $scheduler->scheduler_last; // Next run time $scheduler_weekday = $scheduler->scheduler_weekday; // Day of the week $scheduler_day = $scheduler->scheduler_day; // Day of the month $scheduler_hour = $scheduler->scheduler_hour; // Hour $scheduler_minute = $scheduler->scheduler_minute; // Minute // Controller that performs action $scheduler_controller = $scheduler->scheduler_controller; if ($scheduler_day <= -1) { // Ran every day? $scheduler_day = "*"; } if ($scheduler_weekday <= -1) { // Ran every day? $scheduler_weekday = "*"; } if ($scheduler_hour <= -1) { // Ran every hour? $scheduler_hour = "*"; } if ($scheduler_minute <= -1) { // Ran every minute? $scheduler_minute = "*"; } $scheduler_cron = $scheduler_minute . " " . $scheduler_hour . " " . $scheduler_day . " * " . $scheduler_weekday; //Start new cron parser instance $cron = new CronParser(); if (!$cron->calcLastRan($scheduler_cron)) { echo "Error parsing CRON"; } $lastRan = $cron->getLastRan(); //Array (0=minute, 1=hour, 2=dayOfMonth, 3=month, 4=week, 5=year) $cronRan = mktime($lastRan[1], $lastRan[0], 0, $lastRan[3], $lastRan[2], $lastRan[5]); if (isset($_GET['debug']) and $_GET['debug'] == 1) { $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>"; } if ($scheduler_controller and (!($scheduler_last > $cronRan) or $scheduler_last == 0)) { $run = FALSE; // Catch errors from missing scheduler or other bugs try { $dispatch = Dispatch::controller($scheduler_controller, "scheduler/"); if ($dispatch instanceof Dispatch && method_exists($dispatch, 'method')) { $run = $dispatch->method('index', ''); } } catch (Exception $e) { // Nada. } // @todo allow tasks to save state between runs. if ($run !== FALSE) { // Set last time of last execution $schedule_time = time(); $scheduler->scheduler_last = $schedule_time; $scheduler->save(); // Record Action to Log $scheduler_log = new Scheduler_Log_Model(); $scheduler_log->scheduler_id = $scheduler_id; $scheduler_log->scheduler_status = "200"; $scheduler_log->scheduler_date = $schedule_time; $scheduler_log->save(); if (isset($_GET['debug']) and $_GET['debug'] == 1) { $debug .= "<BR /> STATUS: {{ EXECUTED }}"; } } else { if (isset($_GET['debug']) and $_GET['debug'] == 1) { $debug .= "<BR /> STATUS: {{ SCHEDULER NOT FOUND! }}"; } } } else { if (isset($_GET['debug']) and $_GET['debug'] == 1) { $debug .= "<BR /> STATUS: {{ NOT RUN }}"; } } if (isset($_GET['debug']) and $_GET['debug'] == 1) { //$debug .= "<BR /><BR />CRON DEBUG:<BR />".nl2br($cron->getDebug()); $debug .= "<BR />~~~~~~~~~~~~~~~~~~~~~~~~~~~<BR />~~~~~~~~~~~~~~~~~~~~~~~~~~~<BR /><BR /><BR />"; } } if (Kohana::config('cdn.cdn_gradual_upgrade') != FALSE) { cdn::gradual_upgrade(); } // If DEBUG is TRUE echo DEBUG info instead of transparent GIF if (isset($_GET['debug']) and $_GET['debug'] == 1) { echo $debug; if (isset($this->profiler)) { echo $this->profiler->render(TRUE); } } else { // Transparent GIF Header("Content-type: image/gif"); Header("Expires: Wed, 11 Nov 1998 11:11:11 GMT"); Header("Cache-Control: no-cache"); Header("Cache-Control: must-revalidate"); Header("Content-Length: 49"); echo pack('H*', '47494638396101000100910000000000ffffffff' . 'ffff00000021f90405140002002c000000000100' . '01000002025401003b'); } // Release lock $result = Database::instance()->query("UPDATE `" . Kohana::config('database.default.table_prefix') . "settings`\n\t\t\tSET `value` = 0\n\t\t\tWHERE `key` = 'scheduler_lock';"); }