Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Getting Started with the Cron API
Search
JR Tashjian
October 11, 2014
Programming
0
110
Getting Started with the Cron API
WordCamp Saratoga 2014
JR Tashjian
October 11, 2014
Tweet
Share
Other Decks in Programming
See All in Programming
DevinとCursorから学ぶAIエージェントメモリーの設計とMoatの考え方
itarutomy
1
460
ASP. NET CoreにおけるWebAPIの最新情報
tomokusaba
0
170
[Fin-JAWS 第38回 ~re:Invent 2024 金融re:Cap~]FaultInjectionServiceアップデート@pre:Invent2024
shintaro_fukatsu
0
310
Lookerは可視化だけじゃない。UIコンポーネントもあるんだ!
ymd65536
1
140
Запуск 1С:УХ в крупном энтерпрайзе: мечта и реальность ПМа
lamodatech
0
970
SwiftUIで単方向アーキテクチャを導入して得られた成果
takuyaosawa
0
110
為你自己學 Python
eddie
0
530
Beyond ORM
77web
11
1.6k
[JAWS-UG横浜 #79] re:Invent 2024 の DB アップデートは Multi-Region!
maroon1st
0
120
AWS Lambda functions with C# 用の Dev Container Template を作ってみた件
mappie_kochi
0
210
非ブラウザランタイムとWeb標準 / Non-Browser Runtimes and Web Standards
petamoriken
0
440
オニオンアーキテクチャを使って、 Unityと.NETでコードを共有する
soi013
0
380
Featured
See All Featured
No one is an island. Learnings from fostering a developers community.
thoeni
19
3.1k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.5k
A Philosophy of Restraint
colly
203
16k
Agile that works and the tools we love
rasmusluckow
328
21k
GraphQLとの向き合い方2022年版
quramy
44
13k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Building Better People: How to give real-time feedback that sticks.
wjessup
366
19k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
52k
VelocityConf: Rendering Performance Case Studies
addyosmani
327
24k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Scaling GitHub
holman
459
140k
Transcript
@jrtashjian
WordPress Cron API
WordPress Cron API
What’s Cron?
* * * * * /path/to/command
* * * * * /path/to/command day of the week
* * * * * /path/to/command day of the week
month
* * * * * /path/to/command day of the week
month day of the month
* * * * * /path/to/command day of the week
month day of the month hour
* * * * * /path/to/command day of the week
month day of the month hour minute
# run once a year 0 0 1 1 *
/path/to/command
# run once a year @yearly /path/to/command @annually /path/to/command
@monthly /path/to/command @weekly /path/to/command @daily /path/to/command @hourly /path/to/command
Asynchronous Jobs
Database Job System Clients Enqueue Workers Dequeue
None
WordPress Cron API
Database Job System Clients Enqueue Workers Dequeue
Database WP Cron API Plugin Enqueue Site Visits Dequeue
WordPress Cron API
Using the API
2 Main Functions • wp_schedule_event( $timestamp, $recurrence, $hook, $args )
• wp_schedule_single_event( $timestamp, $hook, $args )
2 Main Functions • wp_schedule_event( $timestamp, $recurrence, $hook, $args )
• wp_schedule_single_event( $timestamp, $hook, $args )
2 Main Functions • wp_schedule_event( $timestamp, $recurrence, $hook, $args )
• wp_schedule_single_event( $timestamp, $hook, $args )
2 Main Functions • wp_schedule_event( $timestamp, $recurrence, $hook, $args )
• wp_schedule_single_event( $timestamp, $hook, $args )
2 Main Functions • wp_schedule_event( $timestamp, $recurrence, $hook, $args )
• wp_schedule_single_event( $timestamp, $hook, $args )
Custom Schedule
Get Schedules <?php wp_get_schedules(); ?> Array ( [hourly] => Array
( [interval] => 3600 [display] => Once Hourly ) [twicedaily] => Array ( [interval] => 43200 [display] => Twice Daily ) [daily] => Array … )
Create a Schedule <?php add_filter( 'cron_schedules', 'cron_add_weekly' ); function cron_add_weekly(
$schedules ) { $schedules['weekly'] = array( 'interval' => 60 * 60 * 24 * 7, // 604800 'display' => __( 'Once Weekly' ) ); }
Create a Schedule <?php add_filter( 'cron_schedules', 'cron_add_weekly' ); function cron_add_weekly(
$schedules ) { $schedules['weekly'] = array( 'interval' => 60 * 60 * 24 * 7, // 604800 'display' => __( 'Once Weekly' ) ); }
Create a Schedule <?php add_filter( 'cron_schedules', 'cron_add_weekly' ); function cron_add_weekly(
$schedules ) { $schedules['weekly'] = array( 'interval' => 60 * 60 * 24 * 7, // 604800 'display' => __( 'Once Weekly' ) ); }
Recurring Event
Recurring Event <?php register_activation_hook( __FILE__, 'prefix_activation' ); function prefix_activation() {
wp_schedule_event( time(), 'hourly', 'prefix_hourly_event_hook' ); } add_action( 'prefix_hourly_event_hook', 'prefix_do_this_hourly' ); function prefix_do_this_hourly() { // do something. }
Recurring Event <?php register_activation_hook( __FILE__, 'prefix_activation' ); function prefix_activation() {
wp_schedule_event( time(), 'hourly', 'prefix_hourly_event_hook' ); } add_action( 'prefix_hourly_event_hook', 'prefix_do_this_hourly' ); function prefix_do_this_hourly() { // do something. }
Recurring Event <?php register_activation_hook( __FILE__, 'prefix_activation' ); function prefix_activation() {
wp_schedule_event( time(), 'hourly', 'prefix_hourly_event_hook' ); } add_action( 'prefix_hourly_event_hook', 'prefix_do_this_hourly' ); function prefix_do_this_hourly() { // do something. }
Single Event
Single Event <?php add_action( 'prefix_single_event', 'prefix_single_event ); function prefix_single_event() {
// do something } wp_schedule_single_event( time() + 3600, 'prefix_single_event' );
Single Event <?php add_action( 'prefix_single_event', 'prefix_single_event ); function prefix_single_event() {
// do something } wp_schedule_single_event( time() + 3600, 'prefix_single_event' );
Single Event with arguments <?php add_action( 'prefix_single_event', 'prefix_single_event, 10, 3
); function prefix_single_event( $arg1, $arg2, $arg3 ) { // do something } wp_schedule_single_event( time() + 3600, 'prefix_single_event', array( $arg1, $arg2, $arg3 ) );
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
Strengths
Weaknesses
When to use it
Getting More Accurate
define( 'DISABLE_WP_CRON', true );
0 * * * * wget http://yourwebsite.com/wp- cron.php >& /dev/null
WP-CLI http://wp-cli.org
wp cron command • event list • event delete •
event schedule • event run • schedule list
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 | +----------------------+---------------------+--------------------+------------+
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 );
Contributing https://make.wordpress.org/core/components/cron-api/
Questions? @jrtashjian jrtashjian.com