Skip to content

Instantly share code, notes, and snippets.

@gamaup
Last active September 10, 2021 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamaup/bcf7dba88990857647c1a49cba450038 to your computer and use it in GitHub Desktop.
Save gamaup/bcf7dba88990857647c1a49cba450038 to your computer and use it in GitHub Desktop.
Action Scheduler & Monolog Sample
<?php
// Pastikan plugin berikut telah terinstall & aktif
// Action Scheduler : https://wordpress.org/plugins/action-scheduler/
// WP Monolog : https://git.tonjoo.com/tonjoo/wp-monolog
/**
* Create job queue
*
* Param 1: Timestamp kapan job akan dirun. Isi pakai time() kalau job ingin dirun sekarang juga.
* Param 2: Nama hook ketika job dirun.
* Param 3: Argument/param yang dikirim ke callback function
*/
as_schedule_single_action( time(), 'do_send_request', array( 'args' => $args ) );
// register hook action ketika job dirun.
add_action( 'do_send_request', 'send_request', 10, 1 );
function send_request($args) {
// wp monolog
global $logger;
try {
// jalankan fungsi kirim request API disini
wp_remote_post($args);
// log the result
$logger->addInfo( 'Success sending request' );
} catch ( Exception $e ) {
// log the error
$logger->addError( 'Error sending request: ' . $e->getMessage() );
// re-queue job kalau request gagal
as_schedule_single_action( time(), 'do_send_request', array( 'args' => $args ) );
}
}
@gamaup
Copy link
Author

gamaup commented Sep 10, 2021

View all jobs & run manual job

image

Lihat log

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment