Slide 1

Slide 1 text

@jrtashjian

Slide 2

Slide 2 text

WordPress Cron API

Slide 3

Slide 3 text

WordPress Cron API

Slide 4

Slide 4 text

What’s Cron?

Slide 5

Slide 5 text

* * * * * /path/to/command

Slide 6

Slide 6 text

* * * * * /path/to/command day of the week

Slide 7

Slide 7 text

* * * * * /path/to/command day of the week month

Slide 8

Slide 8 text

* * * * * /path/to/command day of the week month day of the month

Slide 9

Slide 9 text

* * * * * /path/to/command day of the week month day of the month hour

Slide 10

Slide 10 text

* * * * * /path/to/command day of the week month day of the month hour minute

Slide 11

Slide 11 text

# run once a year 0 0 1 1 * /path/to/command

Slide 12

Slide 12 text

# run once a year @yearly /path/to/command @annually /path/to/command

Slide 13

Slide 13 text

@monthly /path/to/command @weekly /path/to/command @daily /path/to/command @hourly /path/to/command

Slide 14

Slide 14 text

Asynchronous Jobs

Slide 15

Slide 15 text

Database Job System Clients Enqueue Workers Dequeue

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

WordPress Cron API

Slide 18

Slide 18 text

Database Job System Clients Enqueue Workers Dequeue

Slide 19

Slide 19 text

Database WP Cron API Plugin Enqueue Site Visits Dequeue

Slide 20

Slide 20 text

WordPress Cron API

Slide 21

Slide 21 text

Using the API

Slide 22

Slide 22 text

2 Main Functions • wp_schedule_event( $timestamp, $recurrence, $hook, $args ) • wp_schedule_single_event( $timestamp, $hook, $args )

Slide 23

Slide 23 text

2 Main Functions • wp_schedule_event( $timestamp, $recurrence, $hook, $args ) • wp_schedule_single_event( $timestamp, $hook, $args )

Slide 24

Slide 24 text

2 Main Functions • wp_schedule_event( $timestamp, $recurrence, $hook, $args ) • wp_schedule_single_event( $timestamp, $hook, $args )

Slide 25

Slide 25 text

2 Main Functions • wp_schedule_event( $timestamp, $recurrence, $hook, $args ) • wp_schedule_single_event( $timestamp, $hook, $args )

Slide 26

Slide 26 text

2 Main Functions • wp_schedule_event( $timestamp, $recurrence, $hook, $args ) • wp_schedule_single_event( $timestamp, $hook, $args )

Slide 27

Slide 27 text

Custom Schedule

Slide 28

Slide 28 text

Get Schedules Array ( [hourly] => Array ( [interval] => 3600 [display] => Once Hourly ) [twicedaily] => Array ( [interval] => 43200 [display] => Twice Daily ) [daily] => Array … )

Slide 29

Slide 29 text

Create a Schedule 60 * 60 * 24 * 7, // 604800 'display' => __( 'Once Weekly' ) ); }

Slide 30

Slide 30 text

Create a Schedule 60 * 60 * 24 * 7, // 604800 'display' => __( 'Once Weekly' ) ); }

Slide 31

Slide 31 text

Create a Schedule 60 * 60 * 24 * 7, // 604800 'display' => __( 'Once Weekly' ) ); }

Slide 32

Slide 32 text

Recurring Event

Slide 33

Slide 33 text

Recurring Event

Slide 34

Slide 34 text

Recurring Event

Slide 35

Slide 35 text

Recurring Event

Slide 36

Slide 36 text

Single Event

Slide 37

Slide 37 text

Single Event

Slide 38

Slide 38 text

Single Event

Slide 39

Slide 39 text

Single Event with arguments

Slide 40

Slide 40 text

How it works 1. A visitor requests a page on your site. 2. wp_cron() is called 3. spawn_cron() is called 4. One event processed, then removed from queue

Slide 41

Slide 41 text

Strengths

Slide 42

Slide 42 text

Weaknesses

Slide 43

Slide 43 text

When to use it

Slide 44

Slide 44 text

Getting More Accurate

Slide 45

Slide 45 text

define( 'DISABLE_WP_CRON', true );

Slide 46

Slide 46 text

0 * * * * wget http://yourwebsite.com/wp- cron.php >& /dev/null

Slide 47

Slide 47 text

WP-CLI http://wp-cli.org

Slide 48

Slide 48 text

wp cron command • event list • event delete • event schedule • event run • schedule list

Slide 49

Slide 49 text

wp cron event list >> wp cron event list +----------------------+---------------------+--------------------+------------+ | hook | next_run_gmt | next_run_relative | recurrence | +----------------------+---------------------+--------------------+------------+ | wp_version_check | 2014-10-11 17:50:37 | 2 hours 13 minutes | 12 hours | | wp_update_plugins | 2014-10-11 17:50:37 | 2 hours 13 minutes | 12 hours | | wp_update_themes | 2014-10-11 17:50:37 | 2 hours 13 minutes | 12 hours | | wp_scheduled_delete | 2014-10-11 18:42:25 | 3 hours 5 minutes | 1 day | | wp_maybe_auto_update | 2014-10-11 19:50:00 | 4 hours 12 minutes | 12 hours | +----------------------+---------------------+--------------------+------------+

Slide 50

Slide 50 text

wp shell >> wp shell wp> prefix_do_this_hourly(); wp> $arg1 = "first value"; wp> $arg2 = "second value"; wp> $arg3 = "third value"; wp> prefix_single_event( $arg1, $arg2, $arg3 );

Slide 51

Slide 51 text

Contributing https://make.wordpress.org/core/components/cron-api/

Slide 52

Slide 52 text

Questions? @jrtashjian jrtashjian.com