Cron setup for retrieving live sports scores - php

I'm developing a mobile app that display the live scores for premiership football games. When a game starts, I want to query an external API every 30 seconds to retrieve the latest scores. Most games start at 3pm on a saturday, but some start at 12.45pm, others at 1.30pm, 2pm and 3pm on Sundays and some during the week at the latest time of 8pm.
I have a table in my database populated with all the fixtures for the season and the times they start at.
So I'm guessing I need a cron that runs every 15 minutes between 12.45 and 8pm (games never start outside of these times) that checks my database to see if a game is starting. Then, if there is a game starting, another cron must begin that queries the external API every 30 seconds for the latest score in that game. This cron would run for approximately 1 hour and 45 minutes.
What would be the best way to achieve this sort of cron setup? I'm on a shared server with Plesk software running on it, and don't have ssh access to the server.

Judging by one of your comments
the main problem is I'm on a shared server with no ssh access...
I think the issue here is you don't have access to the command shell and can only render your files to the server using Plesk. Hence, to render your solution, you are going to use cron jobs.
In that case, I will recommend double-checking with your hosting provider for any restrictions on number of cron jobs you can run / minimum frequency permitted for cron jobs. If there are restrictions on processing of cron jobs, you can use an online cron scheduler (like this). Note that external cron service will only allow hitting publicly accessible urls, so you will need to write and deploy code accordingly.
Hence forth, I will assume your cron jobs are working and there are no issues with them.
When a game starts, I want to query an external API every 30 seconds to retrieve the latest scores. Most games start at 3pm on a saturday, but some start at 12.45pm, others at 1.30pm, 2pm and 3pm on Sundays and some during the week at the latest time of 8pm.
Approach 1
Use a single updateMatchesAndScores.php file to update match information for any new matches (mark in the db as active/closed), and update scores for currently active matches.
Cron jobs can not handle the logic of the kind if match is on only then run this, that logic has to go to a script.
You can then run it from 12-10 PM like following
* 12-22 * * * php -f updateMatchesAndScores.php
* 12-22 * * * sleep 30 && php -f updateMatchesAndScores.php
In case of url http://some.server.address.com/updateMatchesAndScores, this becomes
* 12-22 * * * wget http://some.server.address.com/updateMatchesAndScores
* 12-22 * * * sleep 30 && wget http://some.server.address.com/updateMatchesAndScores
You can break this into multiple cron jobs (12.45-12.59, 13:00-20:59, 21:00-21:45) assuming games happen in time range [12.45, 21:45]. This will optimize the unnecessary runs from 12.00-12.45 and so on.
Approach 2
Start a daemon process once using a one time cron job, and then check every minute if its still running or not.
Lets call the script updateMatchesAndScores.php. Put the sleep functionality in this for (1) 15 minutes if no game is on (2) 30 seconds if any game is on in this (3) Sleep from 21:46 to 12:44 next day. You can spawn a separate sub process for every game, so that you don't have to implement (2) for sleeping every 30 minutes in this script.
Caveats - (1) the execution time of the script will start delaying the code a bit, so 15 minutes will soon turn into 15 minutes and x seconds (2) There is a max execution time within php, so you will have to set it accordingly (3) Based on your code quality, there might be memory leaks affecting you slightly.
An optimization here could be to run the process every day using a cron job (which stops after the last game is over - irrespective of whether its 21:46 or 4:30) and restart it accordingly if not already running.

It might be more straightforward to have just one cron job, that looks for game start times, as you have suggested, but which then starts a job for each game that runs for the entire length of the game, looping until the game is over, and sleeping for 30 seconds at the bottom of the loop.

From what you describe, a cron-based solution does not sound like the best approach.
It would be better to set up a daemon (or daemon-like) process, which periodically checks for games starting (e.g. every 15 minutes, playing the role of your first cron job), and spawns a subprocesses to carry out the tasks for each game starting (the role of your second cron job).
If you particularly like cron, you can have a cron job checking the daemon process is running, and starting it if not ;)

Related

Setting a PHP cronjob for multiple timeframes

I need to set a PHP script to run once every 5 minutes between 8pm and 9pm, nightly.
My understanding of cronjobs is that they run on a fixed time interval such as once every day, once every week etc.
What I need is some way of running regular cronjobs (every 5 minutes) within a specific time frame (8pm to 9pm). The script that runs is the same script.
Ideas
Run one PHP script at 8pm via cronjob ad loop the code inside a timer loop so the script runs constantly (with sleep) for the hour. This seems to me to be a poor solution; inefficient.
Run a secondary cronjob trigger by the first so at 8pm run a cronjob to then run a second cronjob to run every 300 seconds to call the script. Is this even possible?
Install multiple duplicate cronjobs at each required time interval, so one at 20:00, one at 20:05, one at 20:10, 20:15, etc. This is not very DRY programming.
Run the cronjob every 5 minutes 24/7 and exit the PHP script early when the time does not fall between 8pm and 9pm . This seems wasteful/inefficient as 95% of cronjob script triggers will simply exit.
None of these ideas seem to really efficiently fit the bill. Are there choices I have missed?
To be absolutely clear:
I use cronjobs already but they run on single-interval timeframes. I have read all about cronjobs on here and elsewhere and can't find any guidance for multi-inteval time frames.
I am looking for something that runs within two time frames rather than simply one!
Question:
What method(s) can I use to run a standalone PHP script every 5 minutes between two timepoints (8pm and 9pm, in this case)?
Typical, that after posting this question I find a solution from this useful link
The answer being:
*/5 20 * * * /usr/bin/php /www/virtual/username/cron.php
This */5 runs every five minutes between the times of 2000 and 2000 + 1 hours. As pointed out by Barmar this means the final cronjob execution would be 2055.

Scheduled events in web development

I am working on a web application that requires php code to run at a specific date/time.
Some hypothetical examples would be sending a user an email at 09:00 on their birthday or modifying a database entry (mySQL) at a predetermined date and time.
What would be the conventional way to implement this kind of scheduling feature?
I've seen cron-jobs been used for similar requirements but would this be feasible for a large amount of scheduled tasks?
Depends on the size of your project, but usually we don't have one script do all the cron jobs, because things need to happen at different times. Some emails might send at 9pm, some database cleanup might happen at midnight, and sessions might expire every few minutes. The best thing to do is set up separate cron jobs for each thing. There are a couple exceptions for this:
Some tasks need to run very frequently, like every 30 seconds. For that we set up a cron task which checks "ps" to see whether its own process is already running, and have it just wait 30 seconds and loop.
Some tasks make more sense to run when a queue is full. For that we trigger the task when a certain user-based script executes the 100th or 200th or 300th time. For example when a user pings 100 times without doing anything else, we log them out without using a cron task, but there is a separate cron task which checks if they've been inactive for 10 minutes.

Run scheduled job in PHP

I am using Cakephp.
I want to run scheduled jobs.
Here's the situation:
User set time for some task on UI (say 1 week).
When the time is over, I want to execute some specific task. Meanwhile, the user can also change the time, and the task should be executed at updated time.
What is the best way (and reliable) to achieve this?
PS: Complexity is not the issue. But the task must always run after specific time under all circumstances.
Set the execution date in your table in a field
Set a status (pending) as well
Run a cron job that runs a CakePHP shell every X seconds or minutes, whatever you need OR create a shell that keeps running all time and check the records every X seconds in a loop.
The shell will process tasks that are configure for an execution date lower than the current date
Set the status to success or failed depending on the outcome
It's up to you how you want to handle failed tasks and if it's OK if a task executes 10secs later than configured or 10 minutes. There are multiple factors that play into this: Your interval of the cron job / query against the table. Do they have to processed in parallel? Is it OK to process them after each other? Your information is to vague.
The way to do that in CakePHP is to create a shell and run it with Cronjobs.

execute cron job every 2 hour with 30 min duration

I have searched SO, but can't seem to find any topic covering my little problem.
I'm quite new to cron jobs.
I have an IP based alarm. This alarm can control wireless power outlets, turning them on and off within the web based control panel. I can control the power outlets with a simple http command, making them turn on and off.
I have made a php script taking care of this. Right now they are 2 separate scripts, one for turning on and one for turning off. The script are only controlling one specific power outlet.
My problem is that i need a time based switching scheme. First I thought of making the php script sleep, but at the sleep time is 1 hour, it would not be my first choice.
So here we go.
Is it possible to set up cron job to:
1: run the on script for 1 sec, just to trigger the http command in the script.
2: wait 1 hour.
3: run the off script for 1 sec to trigger the http command.
4: wait 2 hours.
5: start all over again.
There are no problem with the alarm system, sending the OFF http command even if the power outlet is off, and vice versa.
You can combine all commands into single and run it every 3 hours.
0 */3 * * * /path/to/1st_script; sleep 3600; /path/to/2nd_script
This will run the 1st_script every 3 hours on 0-minute, then wait 1 hour, then run the 2nd_script.
Now I just have to figure out how to make the system accept my php script in cronjob.
I have read that I have to add /usr/bin/php to the cronjob command, but that does not exist on my RaspberryPi server (nginx with FPM/FastCGI)

How to send mail in php at scheduled time?

I want to build an application that enables users to schedule emails to send any time. simply, write email message and schedule it so that the server sends it at time specified. I am using zend framework. How to do it in php? Can it be done with cron jobs? If yes, then what are the disadvantages of using cron?
can it be done with cron jobs?
Cron is a time-based job scheduler in Unix-like computer operating systems. The name cron comes from the word "chronos", Greek for "time".1 Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email.
http://ubuntuforums.org/showthread.php?t=586478
I would run a cron job every minute and check if there are any mails ready to be scheduled. The quote from the forum topic below instructs how to run cron every minute.
crontab -e
then set a tab like
* * * * * /command
The first star is the minute section,
so having a star there will execute
every minute
In case it makes it more clear if you
wanted every 5 mins then it would be
*/5 * * * * /command/to/execute
And the other stars are from left to
right
minute hour dayofmonth month
dayofweek*
*0=sunday
Disadvantages of cron?
if yes, then what are the
disadvantages of using cron
When doing a lot of cronjobs you will have to spawn a lot of processes(pay cost of spawning process which is expensive). In that case it would be better to have background process(es) running continually and fetch messages from message queue. But when you want to run a cronjob only ever minute than I assume this will not be a big case.
I would tackle this using a cron job.
Simply create a script that checks for messages to send at a certain time. The user schedules for say 1PM (using a database of course), the script runs every 5 min, or so, and it checks (the db), are there any messages to go out for the current time? If so, it sends out the emails, else it sleeps.
Clean and simple way of handling it.
Disadvantages?
I can't see any, this is what a cron is made for, running tasks at specific times.

Categories