Cron tuning for real-time results

I am developing a mobile application that displays real-time results for premier football games. When the game starts, I want to request an external API every 30 seconds to get the latest ratings. Most games start at 3pm on Saturday, but some start at 12.45pm , others at 1.30pm , 2pm and 3pm on Sundays, and some last week at 8pm .

I have a table in my database filled with all the lights during the season and the start time.

So, I guess I need a cron that runs every 15 minutes between 12.45 and 8pm (games never run outside of these times), which check my database to see if the game starts. Then, if there is a game, another cron starts, which requests an external API every 30 seconds for the last rating in this game. This cron will work for approximately 1 hour and 45 minutes .

What would be the best way to achieve this kind of cron setup? I am on a shared server with Plesk software running on it and do not have access to the ssh server.

+6
source share
3 answers

Judging by one of your comments

The main problem is that I am on a shared server without ssh access ...

I think the problem here is that you do not have access to the shell and you can only display files on the server using Plesk. Therefore, to make your decision, you will use cron jobs.

In this case, I recommend double checking with your hosting provider any restrictions on the number of cron jobs that you can run / the minimum frequency allowed for cron jobs. If there are restrictions on processing cron jobs, you can use the cron online scheduler (like this ). Note that the external cron service will only allow access to public URLs, so you will need to write and deploy the code accordingly.

Therefore, in the future, I will assume that your cron jobs work, and there is no problem with them.

When the game starts, I want to request an external API every 30 seconds to get the latest ratings. Most games start at 3pm on Saturday, but some start at 12.45pm, others at 13:30, 2pm and 3pm on Sundays, and some during the week at most 8pm.

Approach 1

Use one updateMatchesAndScores.php file to update the match information for any new matches (mark db as active / closed) and update the ratings for active matches.

Cron tasks cannot handle this type of logic if a match is enabled, and then run it; for this logic there is to go to the script.

Then you can run it from 12-10 PM as shown below

 * 12-22 * * * php -f updateMatchesAndScores.php * 12-22 * * * sleep 30 && php -f updateMatchesAndScores.php 

In case of url http://some.server.address.com/updateMatchesAndScores it becomes

 * 12-22 * * * wget http://some.server.address.com/updateMatchesAndScores * 12-22 * * * sleep 30 && wget http://some.server.address.com/updateMatchesAndScores 

You can break it down into several cron tasks ( 12.45-12.59 , 13:00-20:59 , 21:00-21:45 ), assuming that the games take place in the time range [12.45, 21:45] . This optimizes unnecessary runs from 12.00-12.45 , etc.

Approach 2

Run the daemon process once using a one-time cron job, and then check every minute if it is still running or not.

Lets you call the script updateMatchesAndScores.php . Put the sleep function in this for (1) 15 minutes if the game is not turned on (2) 30 seconds if the game is turned on in this game (3) Sleep from 21:46 to 12:44 the next day. You can create a separate subprocess for each game so that you do not have to (2) sleep every 30 minutes in this script.

Caveats - (1) The execution time of the script will delay the code a bit, so 15 minutes will soon turn into 15 minutes and x seconds (2). The maximum runtime in php, so you (3) Depending on the quality of the code, memory leaks may occur.

The optimization here may be to start the process every day using the cron task (which stops after the last game is completed - regardless of whether it is 9:46 p.m. or 4:30 a.m.) and restart it if it is not already running.

+2
source

Perhaps it would be easier to have only one cron task, which will look for the start time of the game, as you suggested, but then it starts work for each game that runs the entire length of the game, looping until the game is finished and sleeps for 30 seconds at the bottom cycle.

+1
source

From what you described, a cron-based solution doesn't seem like the best approach.

It would be better to set up a daemon (or daemon-like) process that periodically checks the start of games (for example, every 15 minutes, playing the role of your first cron job) and spawns subprocesses to complete the task for each game starting (the role of the second cron job).

If you especially like cron, you can run a cron job that checks the daemon process and run it if not;)

+1
source

Source: https://habr.com/ru/post/957043/


All Articles