mtrace("{$filename}: {$rlipshortname} plugin '{$plugin}' unknown!"); continue; } mtrace("{$filename}: Processing external cron function for: {$plugin}, taskname: {$task->taskname} ..."); //determine the "ideal" target start time $targetstarttime = $ipjob->nextruntime; // Set the next run time & lastruntime //record last runtime $lastruntime = $ipjob->lastruntime; $data = unserialize($ipjob->config); $state = isset($data['state']) ? $data['state'] : null; $nextruntime = rlip_calc_next_runtime($targetstarttime, $data['period']); $task->nextruntime = $nextruntime; $task->lastruntime = $timenow; $DB->update_record('local_eliscore_sched_tasks', $task); //update the next runtime on the ip schedule record $ipjob->nextruntime = $task->nextruntime; $ipjob->lastruntime = $timenow; unset($data['state']); $ipjob->config = serialize($data); $DB->update_record(RLIP_SCHEDULE_TABLE, $ipjob); $instance = rlip_get_run_instance($filename, $plugin, $plugparts[0], $ipjob->userid, $state); if ($instance == null) { continue; } $instance->run($targetstarttime, $lastruntime, 0, $state); //^TBD: since this should run 'til complete, no state should be returned } } mtrace("\n{$rlipshortname} external cron end - Server Time: " . date('r', time()) . "\n\n"); // end of file
/** * Callback function for local_eliscore_sched_tasks IP jobs * * @param string $taskname The task name, in the form ipjob_{id}, where id * is the IP job's schedule id * @param int $maxruntime Maximum number of secs allowed to process job * * @return boolean true on success, otherwise false */ function run_ipjob($taskname, $maxruntime = 0) { global $CFG, $DB; $fcnname = "run_ipjob({$taskname}, {$maxruntime})"; $disabledincron = !empty($CFG->forcedatahubcron) || get_config('local_datahub', 'disableincron'); $rlipshortname = 'DH'; if (empty($maxruntime)) { $maxruntime = IP_SCHEDULE_TIMELIMIT; } require_once $CFG->dirroot . '/local/datahub/lib/rlip_dataplugin.class.php'; require_once $CFG->dirroot . '/local/datahub/lib/rlip_fileplugin.class.php'; require_once $CFG->dirroot . '/local/datahub/lib/rlip_importprovider_csv.class.php'; // Get the schedule record list($prefix, $id) = explode('_', $taskname); $ipjob = $DB->get_record(RLIP_SCHEDULE_TABLE, array('id' => $id)); if (empty($ipjob)) { mtrace("{$fcnname}: DB Error retrieving {$rlipshortname} schedule record - aborting!"); return false; } $plugin = $ipjob->plugin; $data = unserialize($ipjob->config); $state = isset($data['state']) ? $data['state'] : null; //determine the "ideal" target start time $targetstarttime = $ipjob->nextruntime; // Set the next run time & lastruntime if ($task = $DB->get_record('local_eliscore_sched_tasks', array('taskname' => $taskname))) { // Check if a job is already in progress. try { $like = $DB->sql_like('config', '?'); $inprogress = $DB->get_field_select(RLIP_SCHEDULE_TABLE, 'id', 'plugin = ? AND ' . $like, array($plugin, '%s:5:"state";%')); } catch (dml_multiple_records_exception $e) { $inprogress = 0; // Multiple in progress so set to != $id and !== false } if ($inprogress !== false && $inprogress != $id) { // Put times back to pre-run state. $task->nextruntime = $ipjob->nextruntime; $task->lastruntime = $ipjob->lastruntime; $DB->update_record('local_eliscore_sched_tasks', $task); // mtrace("{$fcnname}: Similiar task has not completed yet!"); return false; } else { if (empty($disabledincron)) { //record last runtime $lastruntime = (int) $ipjob->lastruntime; $nextruntime = rlip_calc_next_runtime($targetstarttime, $data['period']); $task->nextruntime = $nextruntime; //update the next runtime on the ip schedule record $ipjob->nextruntime = $task->nextruntime; $DB->update_record(RLIP_SCHEDULE_TABLE, $ipjob); } else { // running Datahub cron externally, put times back to pre-run state $task->nextruntime = $ipjob->nextruntime; $task->lastruntime = $ipjob->lastruntime; } } $DB->update_record('local_eliscore_sched_tasks', $task); } else { mtrace("{$fcnname}: DB Error retrieving task record!"); //todo: return false? } // Must set last & next run times before exiting! if (!empty($disabledincron)) { // mtrace("{$fcnname}: Internal {$rlipshortname} cron disabled by settings - aborting job!"); return false; // TBD } // Perform the IP scheduled action $instance = rlip_get_run_instance($fcnname, $plugin, $data['type'], $ipjob->userid, $state); if ($instance == null) { return false; } $ipjob->lastruntime = $task->lastruntime; //run the task, specifying the ideal start time, maximum run time & state if (($newstate = $instance->run($targetstarttime, $lastruntime, $maxruntime, $state)) !== null) { // Task did not complete - RESET nextruntime back & save new state! // mtrace("{$fcnname}: {$rlipshortname} scheduled task exceeded time limit of {$maxruntime} secs"); //update next runtime on the scheduled task record $task->nextruntime = $targetstarttime; $task->lastruntime = $ipjob->lastruntime = $lastruntime; $DB->update_record('local_eliscore_sched_tasks', $task); //update the next runtime on the ip schedule record $ipjob->nextruntime = $task->nextruntime; $data['state'] = $newstate; $ipjob->config = serialize($data); } else { if ($state !== null) { unset($data['state']); $ipjob->config = serialize($data); } } $DB->update_record(RLIP_SCHEDULE_TABLE, $ipjob); return true; }