PHP and mySQL Task Scheduler program and database theory? - php

I need to develop a website that runs jobs at certain times (every 15 minutes) I'll use a cron to run the webpage.
What is the best way to store the jobs information in? Some jobs will be daily, others every 8 hours etc. To complicate matters, I also need to take into consideration the timezone differences. This shouldn't be too difficult as PHP has many timezone functions, but how do I integrate it in the programming of the next job to run?
Another question, is how will the user enter the information for the jobs to run? One option, similar to http://www.webcron.org, is to ask the user to select how often the jobs must run, but how do I store that info in a database?
Any assistance will be greatly appreciated!

I would use the crontab syntax, store it in a DB and run a separate crontab invoking the execution script every minute.
There are GPL classes that do that for you already, like : http://www.phpclasses.org/package/4140-PHP-Database-driven-PHP-job-scheduler-like-cron.html

Create a MySQL Event, that will take care also the storing, recurrence, execution.

Related

Triggering code at a certain time

Is it possible to tell PHP to execute a piece of code on a given date and time? For example, Blogger.com allows someone to set a blogpost to be published in the future (e.g. 12/12/14 6:00AM).
Can PHP do something similar?
(Sorry, I don't even know what the correct term for events like these would be to be able to even search for them! :( )
You can do this using a cron job (or scheduled task on Windows); although they are typically used for reoccurring jobs.
If you're using a database, most platforms come with a scheduler.
You can schedule your action in your database and use a cronjob on your server or use a cron job service To run your actions.
https://www.setcronjob.com/
For example when you want to publish your blog in the future, you save your publish date in the future and set some sort of auto-publish bit.
Then every hour a PHP script is ran by a cronjob, this script checks the database for all blogs which need to be published.
It's not possible to tell PHP to do this itself, since it would require a process to run forever to periodically call your PHP code. Thankfully though, there's a couple of things which do this:
1) Call a PHP script from a cron job, which then does any necessary work. If you don't have access to a crontab, you can periodically call this when a user pings your site instead, although that will be less reliable, of course.
2) Use at. This works in basically the same way as cron on Linux systems, but will schedule once and at an exact time.
for "triggering code at a certain time", cron works. But for something as simple as publishing an article at a specific time, it isn't needed. You can just store a publish date with your article. When displaying a list of articles you can adjust your query to something like WHERE PUBLISH_DATE <= NOW() and on the article page check if the article's publish date has passed before showing the article.
On Unix-like systems, there's Cron. You can manage Cron from PHP.
On Windows, there are scheduled tasks - you can also use PHP to manage scheduled tasks.
Be careful with this though - it's kinda hard to test, and you may end up with a schedule that cripples your server.

Can php launch scripts without user interaction in order to interact with the database?

I've search on the web and apparently there is no way to launch a php script without user interaction.
Few advisors recommend me Cron but I am not sure this is the right way to go.
I am building a website where auctions are possible just like ebay. And after an amount of time the objects are not available anymore and the auction is considered as finished.
I would like to know a way to interact with the database automatically.
When do you need to know if an object is available? -> Only if someone asks.
And then you have the user interaction you are searching for.
It's something different if you want to, let's say, send an email to the winner of an auction. In this case you'd need some timer set to the ending time of the auction. The easiest way to do this would be a cron job...
There are several ways to do this. Cron is a valid one of them and the one I would recommend if its available.
Another is to check before handling each request related to an object whether it is still valid. If it is not, you can delete it from the database on-the-fly (or do whatever you need to) and display a different page.
Also you could store the time at which your time-based script was run last in the database and compare that time with the current time. If the delay is large enough, you can run your time based code. However, this is prone to race conditions if multiple users hit the page at the same time, so the script may run multiple times (maybe this can be avoided using locks or anything though).
To edit cronjobs from the shell: crontab -e
A job to run every 10 minutes: */10 * * * * curl "http://example.com/finished.php"
TheGeekStuff.com cron Examples
Use heartbeat/bot implement
ation
Cron job that runs pretty frequently or a program that starts on boot and runs continuously (maybe sleeping periodically) is the way to go. With a cron job you'll need to make sure that you don't have two running at any given time or write it such that it doesn't matter if you have more than one working at any given time. With "resident" program you'll need to figure out how to handle the case when it crashes unexpectedly.
I wouldn't rely on this mechanism to actually close the auction, though. That should be handled in your database/web site. That is, the auction has a close time and either the database constraints or your code makes it impossible to bid on a closed auction. Notifying the winner and seller, setting up the payment process, etc. are things your service/scheduled task could do.

The best way to schedule a event

is there any other option other than cron to schedule the running of a php backup script at a certain time?. I know you can use php itself to schedule things, but it will only fire if the site is getting traffic.
Are there any other options ?.
Thanks :-)
If you're talking about a database backup, then MySQL 5.1 and above has CREATE EVENT which can be used to trigger events (such as stored procedures that can dump table structure/data to file) at regular intervals or set times
Well, cron jobs is a solution. But not necessary in most cases.
If your script is doing something off the site (like sending an email or something), it must be a cron-job.
But...
I made a textbased rpg-game once where several actions were stored in the database waiting to get triggered at a specified time. I found out that it did not make any difference if the script fired at the time it should, or when the first person visiting the page after the time is beyond the timestamp. You could do these events before displaying the content of the page. (I used a file called monitor, to keep it simple).
Would you like to say more about your "event"?
Unless you feel like writing a daemon/service/etc., cron would be your best bet. If you need a job ran more often than minutely, use a lockfile solution and a looping script.
Well not really, Crons are your best bet.
Other than that call a script, and if certain parameters are met such as time elapsed then run the script.

Cron job on a large database with PHP, suggestions needed

I've heard about cron job and don't think the actual creation of it will be that hard to make but I've some concerns about how this will work with a large script.
Without going too much off-topic on my project i will stick with the basics about my situation. I need to make a script that every day performs a CURL fetch for data on a remote website and updates an database for each featured member on my website with it. In short, it's approximatively at this time 1000 times the script need to be executed but it will be a larger number as times goes by.
As you can guess, this will take a long time to preform so i'm worried about how the execution will work in a manner of not crashing in the middle of it.
My first thought was to perhaps split the users into groups and make the executions on a small amount of users each time but don't know how this is manageable ( will read on further about the topic when i got some form of confirmation on this).
So, to my question. Do you think there is any way for me to make this happen and do you perhaps have any suggestions on how to make this to work efficiently? All help i can get is appreciated. Thank you for your time.
bigger cron-jobs with php and mysql needs to be fragmented, since there is no way for you to 'nice' them, (reduce their os priority). Even if you nice the script, the mysql-requests will be executed without this concern.
From what you're describing there's two aspects to consider:
Congestion of network bandwith
Congestion of database throughput
I'd recommend a fragmented solution where you call your script from cron more often, and let the script execute only a small amount of the total job. The job should further be canceled (postponed to next run) if i/o-bandwith or cpu-usage is above any limit that may affect response-time to visitors.
regards,
/t
One Way:
I'm usually against putting logic in the database, but in this case a stored procedure might help. It will run your job faster (since it's a large one) and also you want to lock the tables as you do it. That way, if the script that calls the stored procedure gets hit by cron before the original job was over with it wont edit your database while the first one is running.
The actual time can i not give an
straight answer on but based on
previously experiences this will take
longer then the max execution time.
So solve that problem. There's a reason you can have a different php.ini for the command line interface. Then you can simply focus on processing all users in one script.
I solved this program using the files of cron job as differents cron jobs with small pieces. If you are using PHP you can set a cron job to domain/cronjob1.php, domain/cronjob2.php limiting the database lets say 10 with
$sql="SELECT * FROM table LIMIT 10";
to cronjob 1 and the rest in cronjo2

Update mysql data while not actually using it

How can I set up a program in which a certain piece of data for a user is updated every hour. One example I can give is Mafia Wars. When you obtain property, your money is incremented every set amount of time based on which property it is. I'm not asking to spit out code for me, but rather to guide me in the right direction for a solution. I tried looking into cron jobs, but that only runs a script on a set time. Different users are going to be using this, and they may have different times to update their information. So thus, cron jobs are not applicable here.
You could still have cron jobs, just lots of them (not one per user, but maybe one per minute).
Also, Mafia Wars strikes me as not very interactive, so it may be enough to just update the data (after the fact) when the user (or some other part of the system) next looks at it. So when you log in after 37 hours, you get all the updates for the last 37 hours retroactively applied. Cheap trick, but if there is no need for a consistent global view, that might work, too.
A solution that I came up with when wondering how to implement such a thing is that whenever the player saves the game, the game saves the current time. Then, when the player loads the game back up, it calculates how many minutes have passed and figures out how much money the game should give the player. Then, you could update the SQL database to reflect the changes.
Why do you dismiss cron jobs? Have a cron job that runs a script in short intervals. Within this script, include logic to check which specific updates on the database have to be done.
A cron job that runs something is your friend.
What that something is, is up to you. It could be a PHP script that runs some mysql queries or procedures, or it could straight mysql command from the command line.
Either way, Cron (and other similiar tools) are exactly the bill for these tasks. It's lightweight, on nearly every server in the land, lots of help avaliable for it, and it 99.9999% of the time, it just works!

Categories