Scheduler
From KnowledgeTree Document Management Made Simple
The Scheduler takes care of managing and running tasks or batch jobs. The scheduler gets called from a cron on *nix systems and via the task scheduler in windows. It iterates through a list of tasks running those that are due at the time. Tasks can be set to run at given intervals (every 5 minutes, hourly, daily, etc).
Contents |
Adding the scheduler to the *nix cron
Open up a root terminal and check if the scheduler is already in the list of cron jobs, by typing: crontab -l. If the scheduler is not in the list then add it by insert the following line at the end of the crontab:
*/5 * * * * /<Path to KnowledgeTree>/bin/schedulerTask.sh > /dev/null
This tells the cron to run the scheduler script every 5 minutes;
Cron Commands
- View jobs in the crontab: crontab -l
- Edit the crontab: crontab -e
- Press 'o' to start a new line.
- Press 'i' to edit a line.
- Press the 'esc' key to exit the edit mode.
- Type ZZ to save changes and exit.
- Type :q! to exit without saving.
Adding the scheduler to the Windows Task Scheduler
Open up the Task Scheduler via the Control Panel.
If the scheduler is not in the list of jobs then add it by clicking on "Add Scheduled Task" or by right-clicking and selecting New->Scheduled Task. If the scheduler exists then double click on it to edit.
Using the Add Scheduled Task wizard:
- Click "Add Scheduled Task".
- Click Next.
- Click "Browse" and locate the KnowledgeTree Scheduler executable: <KnowledgeTree Directory>\bin\schedulerTask.bat
- Select the frequency of the task as Daily.
- Select the time to run as midnight: 12:00AM and set it to run every day.
- Enter your windows password.
- Click "OK" to save the task.
Advanced Properties:
- Right click and go to properties.
- Select the Schedule tab, click on Advanced.
- Set it to Repeat every 5 minutes.
Using the scheduler class
Tasks can be added to the scheduler using the scheduler class.
Adding a task to the scheduler
To add a task to the scheduler using the code:
require_once(KT_DIR.'/plugins/ktcore/scheduler/scheduler.php');
$oScheduler = new scheduler('My task name');
Set the path to the script:
$sPath = KT_DIR.'/bin/myscript.php'; $oScheduler->setScriptPath($sPath);
If the script requires certain parameters to be passed in use:
$param = 'parameterName'; $value = 'parameterValue'; $oScheduler->addParameter($param, $value);
Set the frequency at which the script should be run. The frequency can be 5mins, 10mins, quarterly, half_hourly, hourly, daily, weekly, monthly or once if it should only be run once.
$sFrequency = 'hourly'; $oScheduler->setFrequency($sFrequency);
If the script must be started at a given time. For example, if a task should be run at midnight every night:
$iTime = date('Y-m-d').' 11:59:59';
$oScheduler->setFirstRunTime($iTime);
Once all the variables have been set up. Add the task to the list:
$oScheduler->registerTask()
Creating a background task
Tasks can be created and run in the background by calling the background class using an ajax script.
Create the javascript for the ajax script:
var runBG = function(sUrl) {
var callback = {
success: function(o) {
alert('Task successfully run in the background');
},
failure: function(o) {
alert('Background task failed!');
}
}
var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
}
Create the UI to call the ajax - in this case a link:
$link = KTUtil::ktLink('admin.php', 'misc/scheduler', 'action=background');
$sBackgroundLink = "
<a href='#' onclick='javascript: runBG(\"{$link}\");'>"._kt('Run in background').'</a>';
Create the function / action called by the link:
require_once('background.php');
function do_background() {
$bg = new background();
$bg->checkConnection();
$bg->keepConnectionAlive();
// Run script
// ...
return TRUE;
}
del.icio.us
reddit

