[wp-trac] Re: [WordPress Trac] #4278: OPTIMIZE some TABLES from
time to time
WordPress Trac
wp-trac at lists.automattic.com
Thu May 17 16:23:33 GMT 2007
#4278: OPTIMIZE some TABLES from time to time
----------------------------+-----------------------------------------------
Reporter: ozh | Owner: anonymous
Type: enhancement | Status: new
Priority: normal | Milestone: 2.4
Component: Optimization | Version:
Severity: trivial | Resolution:
Keywords: mysql optimize |
----------------------------+-----------------------------------------------
Comment (by Otto42):
+1 for letting this be a plugin.
JeremyVisser:
It's really not all that difficult to do recurring cron jobs with the new
cron code. Here's some example code (may not be 100% correct, but it's
pretty close).
{{{
function this_only_ever_gets_called_once_on_activation_of_your_plugin() {
wp_schedule_event(time(), 'daily', 'do_my_thing_action_hook'); // no args
to be passed in this example
}
add_action('activate_pluginname.php','this_only_ever_gets_called_once_on_activation_of_your_plugin');
function do_my_thing_function() {
// do whatever you want to do daily
}
add_action('do_my_thing_action_hook','do_my_thing_function');
function this_gets_called_on_deactivation_of_your_plugin() {
wp_clear_scheduled_hook('do_my_thing_action_hook');
}
add_action('deactivate_pluginname.php','this_gets_called_on_deactivation_of_your_plugin');
}}}
Essentially what's going on is that you're telling the cron process to,
once a day (starting right now, due to the time() call), call some action
hook. The action hook can be any text you want, of course. Then, you hook
your own functions into those hooks. Args will get passed along to the
functions, if you need them. Usually you probably won't need them.
In the above example, I'm adding a recurring hook upon plugin activation
and then clearing it on deactivation. This is important: If you fail to
clean up after yourself on deactivation, then cron will continually hit
that action hook even though nothing is attached to it anymore. This
causes a waste of cycles and CPU time.
Another important thing to remember is that you only ever call
wp_schedule_event once, ever. It will recur on its own after that. This is
why you do it on activation only and not on every load of the plugin.
If you want to schedule a single event at some specific time, use the
wp_schedule_single_event() function instead. It takes a time parameter
(when the hook will be called upon) and the name of a hook to call. This
gives you finer control if you prefer to do it that way. You can even make
this recur if you want, by having the function that gets called actually
reschedule the same call for the future. This might be a bit safer too,
since, in the worst case, you won't leave a recurring hook out there if
your deactivation fails for whatever reason (like they just delete the
plugin instead of deactivating it).
The cron code has hourly and daily built into it as schedules. But if you
want to add your own schedule instead, do this:
{{{
function add_my_own_schedule($useless_arg1_is_an_empty_array)
{
return array(
'fiveminute' => array( 'interval' => 300, 'display' => __('Once Every 5
minutes') ), 'weekly' => array( 'interval' => 604800, 'display' =>
__('Once Weekly') )
);
}
add_filter('cron_schedules','add_my_own_schedule');
}}}
Pretty straightforward.
--
Ticket URL: <http://trac.wordpress.org/ticket/4278#comment:3>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list