wrangling wp_cron - wordcamp grand rapids 2014

22
@cklosowski cklosows Wrangling WP_Cron Scheduling Your Tasks the WordPress Way

Upload: cklosowski

Post on 16-Nov-2014

258 views

Category:

Technology


0 download

DESCRIPTION

Learning to schedule tasks, the WordPress way

TRANSCRIPT

Page 1: Wrangling WP_Cron - WordCamp Grand Rapids 2014

@cklosowski cklosows

Wrangling WP_Cron Scheduling Your Tasks the WordPress Way

Page 2: Wrangling WP_Cron - WordCamp Grand Rapids 2014

Chris Klosowski

• Plugin Developer (WP-Push.com, PostPromoterPro.com, GitHub, WordPress.org)

• Contributing Developer and Support Tech for Easy Digital Downloads

• Hang out with my Wife and Kid

@cklosowski cklosows

Page 3: Wrangling WP_Cron - WordCamp Grand Rapids 2014

WP_Cron

A tool to replace a tool

Page 4: Wrangling WP_Cron - WordCamp Grand Rapids 2014

What is “Cron”

• A time-based job scheduler in Unix-like computer operating systems.

• Used to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.

• Cleanup, populate data into cache, etc.

Page 5: Wrangling WP_Cron - WordCamp Grand Rapids 2014

WP_Cron

The platform-agnostic task scheduler

Page 6: Wrangling WP_Cron - WordCamp Grand Rapids 2014

WP_Cron vs Cron

• Works on Windows based systems

• Works when you don’t have access to cron

• Easily allows access to the WordPress core

• “Reliably” available to Theme and Plugin developers

Page 7: Wrangling WP_Cron - WordCamp Grand Rapids 2014

Basic Usage

<?php wp_schedule_event( $timestamp, $recurrence, $hook, $args );

Simple Right?

Page 8: Wrangling WP_Cron - WordCamp Grand Rapids 2014

Sort Of

Some Caveats

Page 9: Wrangling WP_Cron - WordCamp Grand Rapids 2014

$timestamp

• The first time you want this item to run

• UNIX Timestamp format

• Always in UTC (not localized timezone)

• Use time(); as it uses GMT for right now

• No Default

get_option( 'gmt_offset' );

Page 10: Wrangling WP_Cron - WordCamp Grand Rapids 2014

$recurrence

• The name of the schedule value

• hourly

• twicedaily

• daily

• Using cron_schedules filter, you can add your own recurrences.

• Not a timestamp, but a string that references a timestamp

• Number of seconds between occurrences

Page 11: Wrangling WP_Cron - WordCamp Grand Rapids 2014

$hook

• Essentially does a do_action() when it runs. You would then attach a function to this hook you’re creating.

• Possible issue on some systems where the hook must not contain underscores or uppercase characters. (Have not seen this myself)

Page 12: Wrangling WP_Cron - WordCamp Grand Rapids 2014

$args

• Optional array of data to pass through the hook

• Great for when doing post or page specific crons

• Used later when doing lookups for scheduled crons.

• Must be an indexed array (no numeric keys)

Page 13: Wrangling WP_Cron - WordCamp Grand Rapids 2014

// Be Kind, Rewind register_deactivation_hook( __FILE__, 'ck_deactivation' ); function ck_deactivation() { wp_clear_scheduled_hook( 'ck_hourly_event_hook' ); }

<?php register_activation_hook( __FILE__, 'ck_activation' ); !function ck_activation() { wp_schedule_event( time(), 'hourly', 'ck_hourly_event_hook' ); } !add_action( 'ck_hourly_event_hook', 'ck_do_this_hourly' ); function ck_do_this_hourly() { // do something every hour }

Page 14: Wrangling WP_Cron - WordCamp Grand Rapids 2014

<?php // Situation of single event $args = array( 'post_id' => 42, 'service' => 'twitter' ); wp_schedule_single_event( time(), 'ck_single_event', $args ); !!// Clear with same hook and EXACT same args $args2 = array( 'post_id' => 42, 'service' => 'twitter' ); wp_clear_scheduled_hook( 'ck_single_event', $args2 );

Page 15: Wrangling WP_Cron - WordCamp Grand Rapids 2014

Why Not WP_Cron?

Not all unicorns and rainbows

Page 16: Wrangling WP_Cron - WordCamp Grand Rapids 2014

Because WP_Cron _______________

• Why WP Cron Suckshttp://www.lucasrolff.com/wordpress/why-wp-cron-sucks/

• Disabling WP-Cron in WordPress http://www.limecanvas.com/disabling-wp-cron-in-wordpress/

• Understanding WP Cron, and why I don’t use it http://chrislema.com/understanding-wp-cron/…I could go on…

Page 17: Wrangling WP_Cron - WordCamp Grand Rapids 2014

Low Traffic Sites

• WP_Cron is based off traffic

• No visits, no cron

• Unreliable for time specific tasks

Page 18: Wrangling WP_Cron - WordCamp Grand Rapids 2014

High Traffic Sites

• Possibility of many concurrent cron events.

• Expensive cron events can cause load issues.

• If needing EXACT times, still not 100%

Page 19: Wrangling WP_Cron - WordCamp Grand Rapids 2014

Race Conditions

• Identified by the same scheduled task running twice, at the same time.

• Due to PHP threads, they aren’t aware of each other

Page 20: Wrangling WP_Cron - WordCamp Grand Rapids 2014

Missed Tasks

• They will run.

• Scenario

• Tasks scheduled 8a and 9a.

• No cron run until 9:15a

• Both tasks will run at 9:15a

• Be aware of this when:

• Pushing to Web Services

• APIs

Page 21: Wrangling WP_Cron - WordCamp Grand Rapids 2014

How to Wrangle WP_Cron

• 3rd party ping service (Pingdom)

• Setup an actual cron if available http://domain.com/wp-cron.php?doing_wp_cron=1

• Limit threads with DB or file locks

• Know what crons are scheduled, and what they do

define('DISABLE_WP_CRON', true);

Page 22: Wrangling WP_Cron - WordCamp Grand Rapids 2014

@cklosowski cklosows

Questions? Scheduling Your Tasks the WordPress Way