/** * List all the scheduled task in place on the forum. * * @uses ManageScheduledTasks template, view_scheduled_tasks sub-template */ public function action_tasks() { global $context, $txt, $scripturl; // We'll need to recalculate dates and stuff like that. require_once SUBSDIR . '/ScheduledTasks.subs.php'; // Mama, setup the template first - cause it's like the most important bit, like pickle in a sandwich. // ... ironically I don't like pickle. </grudge> $context['sub_template'] = 'view_scheduled_tasks'; $context['page_title'] = $txt['maintain_tasks']; // Saving changes? if (isset($_REQUEST['save']) && isset($_POST['enable_task'])) { checkSession(); // Enable and disable as required. $enablers = array(0); foreach ($_POST['enable_task'] as $id => $enabled) { if ($enabled) { $enablers[] = (int) $id; } } // Do the update! updateTaskStatus($enablers); // Pop along... calculateNextTrigger(); } // Want to run any of the tasks? if (isset($_REQUEST['run']) && isset($_POST['run_task'])) { // Lets figure out which ones they want to run. $tasks = array(); foreach ($_POST['run_task'] as $task => $dummy) { $tasks[] = (int) $task; } // Load up the tasks. $nextTasks = loadTasks($tasks); // Lets get it on! require_once SUBSDIR . '/ScheduledTask.class.php'; call_integration_include_hook('integrate_autotask_include'); ignore_user_abort(true); foreach ($nextTasks as $task_id => $taskname) { run_this_task($task_id, $taskname); } // Things go as expected? If not save the error in session if (!empty($context['scheduled_errors'])) { $_SESSION['st_error'] = $context['scheduled_errors']; } redirectexit('action=admin;area=scheduledtasks;done'); } // Build the list so we can see the tasks $listOptions = array('id' => 'scheduled_tasks', 'title' => $txt['maintain_tasks'], 'base_href' => $scripturl . '?action=admin;area=scheduledtasks', 'get_items' => array('function' => array($this, 'list_getScheduledTasks')), 'columns' => array('name' => array('header' => array('value' => $txt['scheduled_tasks_name'], 'style' => 'width: 40%;'), 'data' => array('sprintf' => array('format' => ' <a class="linkbutton" href="' . $scripturl . '?action=admin;area=scheduledtasks;sa=taskedit;tid=%1$d" title="' . $txt['scheduled_task_edit'] . ' %2$s"><i class="fa fa-pencil-square-o"></i> %2$s</a><br /><span class="smalltext">%3$s</span>', 'params' => array('id' => false, 'name' => false, 'desc' => false)))), 'next_due' => array('header' => array('value' => $txt['scheduled_tasks_next_time']), 'data' => array('db' => 'next_time', 'class' => 'smalltext')), 'regularity' => array('header' => array('value' => $txt['scheduled_tasks_regularity']), 'data' => array('db' => 'regularity', 'class' => 'smalltext')), 'enabled' => array('header' => array('value' => $txt['scheduled_tasks_enabled'], 'style' => 'width: 6%;text-align: center;'), 'data' => array('sprintf' => array('format' => ' <input type="hidden" name="enable_task[%1$d]" id="task_%1$d" value="0" /><input type="checkbox" name="enable_task[%1$d]" id="task_check_%1$d" %2$s class="input_check" />', 'params' => array('id' => false, 'checked_state' => false)), 'class' => 'centertext')), 'run_now' => array('header' => array('value' => $txt['scheduled_tasks_run_now'], 'style' => 'width: 12%;text-align: center;'), 'data' => array('sprintf' => array('format' => ' <input type="checkbox" name="run_task[%1$d]" id="run_task_%1$d" class="input_check" />', 'params' => array('id' => false)), 'class' => 'centertext'))), 'form' => array('href' => $scripturl . '?action=admin;area=scheduledtasks'), 'additional_rows' => array(array('class' => 'submitbutton', 'position' => 'below_table_data', 'value' => ' <input type="submit" name="run" value="' . $txt['scheduled_tasks_run_now'] . '" class="right_submit" /> <input type="submit" name="save" value="' . $txt['scheduled_tasks_save_changes'] . '" class="right_submit" />'), array('position' => 'after_title', 'value' => $txt['scheduled_tasks_time_offset'], 'class' => 'windowbg2'))); require_once SUBSDIR . '/GenericList.class.php'; createList($listOptions); $context['sub_template'] = 'view_scheduled_tasks'; $context['tasks_were_run'] = isset($_GET['done']); // If we had any errors, place them in context as well if (isset($_SESSION['st_error'])) { $context['scheduled_errors'] = $_SESSION['st_error']; unset($_SESSION['st_error']); } }
/** * Process the next tasks, one by one, and update the results. * * @package ScheduledTasks * @param int $ts = 0 */ function processNextTasks($ts = 0) { $db = database(); // We'll run tasks, or so we hope. require_once SUBSDIR . '/ScheduledTask.class.php'; // Select the next task to do. $request = $db->query('', ' SELECT id_task, task, next_time, time_offset, time_regularity, time_unit FROM {db_prefix}scheduled_tasks WHERE disabled = {int:not_disabled} AND next_time <= {int:current_time} ORDER BY next_time ASC LIMIT 1', array('not_disabled' => 0, 'current_time' => time())); if ($db->num_rows($request) != 0) { // The two important things really... $row = $db->fetch_assoc($request); // When should this next be run? $next_time = next_time($row['time_regularity'], $row['time_unit'], $row['time_offset']); // How long in seconds is the gap? $duration = $row['time_regularity']; if ($row['time_unit'] == 'm') { $duration *= 60; } elseif ($row['time_unit'] == 'h') { $duration *= 3600; } elseif ($row['time_unit'] == 'd') { $duration *= 86400; } elseif ($row['time_unit'] == 'w') { $duration *= 604800; } // If we were really late running this task actually skip the next one. if (time() + $duration / 2 > $next_time) { $next_time += $duration; } // Update it now, so no others run this! $db->query('', ' UPDATE {db_prefix}scheduled_tasks SET next_time = {int:next_time} WHERE id_task = {int:id_task} AND next_time = {int:current_next_time}', array('next_time' => $next_time, 'id_task' => $row['id_task'], 'current_next_time' => $row['next_time'])); $affected_rows = $db->affected_rows(); // Do also some timestamp checking, // and do this only if we updated it before. if ((empty($ts) || $ts == $row['next_time']) && $affected_rows) { ignore_user_abort(true); run_this_task($row['id_task'], $row['task']); } } $db->free_result($request); }