I have a function in function.php which is called on save_post. Currently, when the user is publishing or updating a post, this function will execute but it will take a lot of time.
What is the easiest way so this function run in a background process - non-blocking?
function export_all_in_json() {
file_put_contents(ABSPATH.'all.json', fopen('/', 'r'));
}
add_action( 'save_post', 'export_all_in_json' );
I have a function in function.php which is called on save_post. Currently, when the user is publishing or updating a post, this function will execute but it will take a lot of time.
What is the easiest way so this function run in a background process - non-blocking?
function export_all_in_json() {
file_put_contents(ABSPATH.'all.json', fopen('https://example/api/all/get_all_content/', 'r'));
}
add_action( 'save_post', 'export_all_in_json' );
Share
Improve this question
asked Jul 22, 2020 at 15:49
MustafaMustafa
35 bronze badges
9
|
Show 4 more comments
2 Answers
Reset to default 1Instead of contacting the remote site to get the information in an expensive HTTP request, why not have the remote site send you the data at regular intervals?
- Register a REST API endpoint to recieve the data on
- In that endpoint, take in the data and save it in
all.json
- In that endpoint, take in the data and save it in
- On the remote site add a cron job
- grab the data
- make a non-blocking remote request to the site with this data
Now we have a completely asynchronous non-blocking system. Why fetch the data when it can be given to you?
This gives us several bonuses:
- The
save_post
filter can be completely removed making post saving faster - This fixes a number of bugs in the filter by removing the filter
- Updates to the file can happen even when no posts are being created
- Updates to the file are now predictable and routine, e.g. every 5 minutes, or every hour
- This avoids race conditions where multiple requests are sent to the API at the same time, resulting in extra server load and broken JSON files
- Your API endpoint takes a little time to figure out the JSON data, so this gives you control over how often it happens, e.g. if the site is struggling change the cron job from 5 minutes too 10 minutes to ease the load
- You could ping the API and tell it to trigger sending the data to the endpoint when a post is saved, rather than doing the full fetch and save. This would allow you to use a fetch paradigm and still have the advantages. It's similar to how some payment and authentication flows work too.
Maybe you can use Action Scheduler to trigger a single asyncronous event. Just like a cronjob but triggered one time.
https://actionscheduler/
fopen
to fetch the data? – Tom J Nowell ♦ Commented Jul 22, 2020 at 15:56file_put_contents
, then focused onfile_put_contents
as the slow part. It's not. Fetching a remote resource is one of the slowest things you can do. It's thefopen
call that's super super slow/expensive. That's what you need to make async, but you're passing it intofile_put_contents
, so you can't. Moving this to a cron job would make this process async, but then it wouldn't be immediate. WP Cron only runs if users visit. – Tom J Nowell ♦ Commented Jul 22, 2020 at 16:16save_post
filter entirely. Afterall you're not sending any information, if the goal is to keep a JSON file up to date, ask about that problem! Don't ask about a proposed solution instead – Tom J Nowell ♦ Commented Jul 22, 2020 at 16:25