My action is called by WP Cron so I’m trying to prevent it from running twice at the same time. So, I have the following function that uses doing_action function to check if the action is running. Apparently, the code below can’t run even when the action is not running. But when I removed the doing_action check, the code runs.
function upload_to_ipfs() {
if ( !doing_action( 'upload_file' ) ) {
//Code to run here
}
}
add_action( 'upload_file', 'upload_to_ipfs' );
2
Answers
You’re running into a scenario where the code won’t run because it’s calling
!doing_action
on itself because the action IS running.Also,
WordPress
doing_action
works by looking at a global PHP variable is all.This will not work since you’re probably not on the same thread process (since a new one is started with every PHP request on most servers).
In which case you’ll have to resort to an alternative method for checking.
One such alternative is checking the servers running processes for the action. Take a look at something like this https://write.corbpie.com/check-if-a-process-is-running-on-linux-with-php/ to help guide you in the right direction.
You should probably have a look at site transients.
docs:
get_site_transient()
set_site_transient()
delete_site_transient()