Source for file cronjobs.ctrl.php
Documentation is available at cronjobs.ctrl.php
* This file implements the UI controller for Cron table.
* This file is part of Quam Plures - {@link http://quamplures.net/}
* See also {@link https://launchpad.net/quam-plures}.
* @copyright (c) 2009 - 2011 by the Quam Plures developers - {@link http://quamplures.net/}
* @copyright (c)2003-2009 by Francois PLANQUE - {@link http://fplanque.net/}
* {@internal License choice
* - If you have received this file as part of a package, please find the license.txt file in
* the same folder or the closest folder above for complete license terms.
* - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
* then you must choose one of the following licenses before using the file:
* - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
* - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
* {@internal Open Source relicensing agreement:
* {@internal Below is a list of authors who have contributed to design/coding of this file: }}
* @author fplanque: Francois PLANQUE.
if( !defined('QP_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
// Check minimum permission:
$current_User->check_perm( 'options', 'view', true );
$AdminUI->set_path( 'tools', 'cron' );
param( 'action', 'string', 'list' );
// We want to remember these params from page to page:
param( 'ctst_pending', 'integer', 0, true );
param( 'ctst_started', 'integer', 0, true );
param( 'ctst_timeout', 'integer', 0, true );
param( 'ctst_error', 'integer', 0, true );
param( 'ctst_finished', 'integer', 0, true );
param( 'results_crontab_order', 'string', '-A', true );
param( 'results_crontab_page', 'integer', 1, true );
// Init names and params for "static" available jobs and ask Plugins about their jobs:
if( $action == 'new' || $action == 'create' )
// NOTE: keys starting with "plugin_" are reserved for jobs provided by Plugins
'prune_hits_sessions' => T_('Prune old hits & sessions'),
$cron_job_params = array(
'prune_hits_sessions' => array(
'ctrl' => 'cron/jobs/_prune_hits_sessions.job.php',
// Get additional jobs from Plugins:
foreach( $Plugins->trigger_collect( 'GetCronJobs' ) as $plug_ID => $jobs )
$Debuglog->add( sprintf( T_('GetCronJobs() for plugin #%d did not return array. Ignoring its jobs.'), $plug_ID ), array('plugins', 'error') );
// Validate params from plugin:
if( ! isset ( $job['params'] ) )
if( ! is_array( $job ) || ! isset ( $job['ctrl'], $job['name'] ) )
$Debuglog->add( sprintf( T_('GetCronJobs() for plugin #%d returned an invalid job. Ignoring.'), $plug_ID ), array('plugins', 'error') );
if( isset ($job['params']) && ! is_array($job['params']) )
$Debuglog->add( sprintf( T_('GetCronJobs() for plugin #%d did return invalid job params (not an array). Ignoring.'), $plug_ID ), array('plugins', 'error') );
$ctrl_id = 'plugin_'. $plug_ID. '_'. $job['ctrl'];
$cron_job_names[$ctrl_id] = $job['name'];
$cron_job_params[$ctrl_id] = array(
'params' => $job['params'],
// Check that we have permission to edit options:
$current_User->check_perm( 'options', 'edit', true, NULL );
// Check that we have permission to edit options:
$current_User->check_perm( 'options', 'edit', true, NULL );
$cjob_type = param( 'cjob_type', 'string', true );
if( !isset ( $cron_job_params[$cjob_type] ) )
param_date( 'cjob_date', T_('Please enter a valid date.'), true );
$cjob_repeat_after_days = param( 'cjob_repeat_after_days', 'integer', 0 );
$cjob_repeat_after_hours = param( 'cjob_repeat_after_hours', 'integer', 0 );
$cjob_repeat_after_minutes = param( 'cjob_repeat_after_minutes', 'integer', 0 );
$cjob_repeat_after = ( ( ($cjob_repeat_after_days* 24) + $cjob_repeat_after_hours )* 60 + $cjob_repeat_after_minutes)* 60; // seconds
if( $cjob_repeat_after == 0 )
$cjob_repeat_after = NULL;
$edited_Cronjob->set( 'repeat_after', $cjob_repeat_after );
$edited_Cronjob->set( 'name', $cron_job_names[$cjob_type] );
$edited_Cronjob->set( 'controller', $cron_job_params[$cjob_type]['ctrl'] );
$edited_Cronjob->set( 'params', $cron_job_params[$cjob_type]['params'] );
$edited_Cronjob->dbinsert();
$Messages->add( T_('New job has been scheduled.'), 'success' );
// Make sure we got an ord_ID:
param( 'ctsk_ID', 'integer', true );
// Check that we have permission to edit options:
$current_User->check_perm( 'options', 'edit', true, NULL );
// TODO: prevent deletion of running tasks.
$tsk_status = $DB->get_var(
WHERE clog_ctsk_ID= '. $ctsk_ID,
0, 0, 'Check that task is not running' );
if( $tsk_status == 'started' )
$Messages->add( sprintf( T_('Job #%d is currently running. It cannot be deleted.'), $ctsk_ID ), 'error' );
WHERE ctsk_ID = '. $ctsk_ID );
// Delete log (if exists):
WHERE clog_ctsk_ID = '. $ctsk_ID );
$Messages->add( sprintf( T_('Scheduled job #%d deleted.'), $ctsk_ID ), 'success' );
$cjob_ID = param( 'cjob_ID', 'integer', true );
LEFT JOIN T_cron__log ON ctsk_ID = clog_ctsk_ID
WHERE ctsk_ID = '. $cjob_ID;
$cjob_row = $DB->get_row( $sql, OBJECT, 0, 'Get cron job and log' );
$Messages->add( sprintf( T_('Job #%d does not exist any longer.'), $cjob_ID ), 'error' );
// Detect timed out tasks:
$sql = "UPDATE T_cron__log
SET clog_status = 'timeout'
WHERE clog_status = 'started'
AND clog_realstart_datetime < ". $DB->quote( date2mysql( time() + $time_difference - $cron_timeout_delay ) );
$DB->query( $sql, 'Detect cron timeouts.' );
// Display <html><head>...</head> section! (Note: should be done early if actions do not redirect)
$AdminUI->disp_html_head();
// Display title, menu, messages, etc. (Note: messages MUST be displayed AFTER the actions)
$AdminUI->disp_body_top();
$AdminUI->disp_payload_begin();
$AdminUI->disp_view( 'cron/views/_cronjob.form.php' );
$AdminUI->disp_view( 'cron/views/_cronjob.view.php' ); // uses $cjob_row
$AdminUI->disp_view( 'cron/views/_cronjob_list.view.php' );
$AdminUI->disp_payload_end();
// Display body bottom, debug info and close </html>:
$AdminUI->disp_global_footer();
|