CRON JOB to remove old events.
2 posts by 2 authors in: Forums > CMS Builder
Last Post: November 14, 2017 (RSS)
By Mikey - November 6, 2017
I'm trying to create a cron job to remove events that are older than 7 days. I haven't had much success getting this to work... probably the mysql_delete is configured incorrectly, because nothing gets removed when I run it manually. Anyone have any ideas on what I'm doing wrong?
<?php
/*
Plugin Name: Cron Script Remove Aging Events Data
Description: Remove Aging Events Data background task at a set interval such as once a day or once an minute.
Version: 1.00
Requires at least: 2.53
Plugin URI: ?menu=admin&action=bgtasks
*/
// Setup scheduled background tasks
addCronJob('remove_old_events', "Cron Script Remove Aging Events Data", '0 * * * *'); // Run every hour
// Plugin Menu - Add link to allow users to "Run Manually", this can be useful for testing
pluginAction_addHandlerAndLink(t('Run Manually'), 'remove_old_events', 'admins');
function remove_old_events() {
if (!inCLI()) { // send headers (for running through web with "Run Manually" link above)
if (!headers_sent()) {
header("Content-type: text/plain");
header("Content-Disposition: inline; filename='output.txt'"); // Force IE to display as text and not download file
ob_disable(); // Turn off browser buffering
}
// This lines are only needed if your cron script is going to be called directly and you need to enforce security, if cron is added as a CMS "Background Task" then you can specify access rights in: pluginAction_addHandlerAndLink
// $CMS_USER = getCurrentUserFromCMS(); // security check for web access - don't show cron filepaths unless logged in
// if (!@$CMS_USER['isAdmin']) { die(t("You must be logged in as Admin to run this script from the web!")); } // security check for web access - don't show cron filepaths unless logged in
ignore_user_abort( true ); // continue running even if user clicks stop on their browser
session_write_close(); // v2.51 - End the current session and store session data so locked session data doesn't prevent concurrent access to CMS by user while backup in progress
}
set_time_limit(0); // ignore PHP's max_execution_time directive
//
print "Cron Script Remove Aging Events Data \n";
print "--------------------------------------------------------------------------------\n";
//Delete old records
mysql_delete('events', null, " `createdDate` < TIMESTAMP( DATE(NOW() - INTERVAL 7 DAY) ) ");
print "The current time is: " .date('Y-m-d H:i:s'). "\n\n";
print "Done!\n";
// return summary message
$summary = "Successful";
if (@$_REQUEST['_pluginAction'] == __FUNCTION__) { exit; } // exit if being run manually from web
return $summary; // otherwise, return summary if being run by cron
}
// eof
Thanks, Zicky
By Dave - November 14, 2017
Hi Zicky,
It looks good to me at first glance. What I'd recommend for debugging this is to install the Developer Console:
https://www.interactivetools.com/add-ons/developer-console/
And then test your query in the MySQL Console as a select:
SELECT * FROM events WHERE `createdDate` < TIMESTAMP( DATE(NOW() - INTERVAL 7 DAY)
And confirm that you're getting matches that way.
Hope that helps!
interactivetools.com