Last active
September 10, 2021 07:27
-
-
Save gamaup/bcf7dba88990857647c1a49cba450038 to your computer and use it in GitHub Desktop.
Action Scheduler & Monolog Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
View all jobs & run manual job
Lihat log