Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialcurious developer
1,768 PointsIs there any background process that I can run from plugin without depending on page hits on a website?
I am developing a WordPress plugin and wants to run a background task that calls an API and updates database table. Now, the API can only give results for 5 DB entries in one go and for 500 entries in my table, I have to make 100 API call. The API has allowed TPS quota of 1 and also in every 40 minutes, its old response expires which means I need to update my table if any entry is older than 40 minutes by making a new API call. And, all these DB entries have to be shown to the page viewer with latest data.
The solution that I came up with is scheduling a cron task that runs every minute and does API calls one after another for 25 seconds and then dies so that it doesn't exceed PHP max execution time limit. And at time a customer comes, he has not to wait for API call or be throttled by API
But the problem is I can't rely on wp-cron as it will be called only when page hit occurs(not like actual cron and since I am plugin developer I can't schedule a system cron on my customer's WordPress hosting environment). Also, if the person receives only one page hit in 3 hours, that first person in 3 hours has to either wait for those API calls to finish(not at all desirable or better to say feasible) or else he will not be shown the data.
Is there any other way to solve this problem of updating the DB entries via some background process that is neither slowing down the client and nor depending on page load. So even if 4 clients come in a day they all get latest data (data updated within last 40 minutes) as its already been updated in the background?
1 Answer
Luke Pettway
16,593 PointsYou could use wp_schedule_event
https://codex.wordpress.org/Function_Reference/wp_schedule_event to set an interval for when the front end should get new data. It still relies on having a user hit the site, but it will check the elapsed time and if it has passed will then There is also wp_schedule_single_event
as well, which will make sure only one event can fire.
This article does a much better job than I could ever do of explaining how you can utilize these APIs to your advantage:
https://webdevstudios.com/2015/05/04/wp_cron-cron-cron-thats-not-really-cron/
Luke Pettway
16,593 PointsLuke Pettway
16,593 PointsI should elaborate a bit more on this. It's better to rely on WordPress' internal functionality because your users might not always have access at the server level to schedule tasks. So it makes more sense to leverage the built in functionality to ensure that all a user has to do is install and configure the plugin.
This doesn't solve the issue of the API call needing to take place but depending on the speed of the API call, a few UX enhancements might possibly help it feel like something is happening for the user.
Jacob Mishkin
23,118 PointsJacob Mishkin
23,118 PointsLike the WebDevStudios link.