TransWikia.com

Non-blocking file_put_contents in function.php

WordPress Development Asked by Mustafa on November 26, 2021

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.com/api/all/get_all_content/', 'r'));
}
add_action( 'save_post', 'export_all_in_json' );

2 Answers

Instead 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?

  1. Register a REST API endpoint to recieve the data on
    • In that endpoint, take in the data and save it in all.json
  2. 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.

Answered by Tom J Nowell on November 26, 2021

Maybe you can use Action Scheduler to trigger a single asyncronous event. Just like a cronjob but triggered one time.

https://actionscheduler.org/

Answered by Carlos Faria on November 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP