Page 1 of 1

A program that constantly runs and check the database.

Posted: Sun Mar 07, 2010 9:27 am
by safmaya
Hi Everyone,

I am working on a final year project that involves a website that constantly checks the database and sending emails to the user if a condition is met.

The problem is I have no idea how I should write the code or even where to put the code, this project should be able to check the database regardless of the user being logged in.


any help would be appreciated.

Re: A program that constantly runs and check the database.

Posted: Sun Mar 07, 2010 5:26 pm
by Weirdan
Create a command-line script and put it into the crontab. That's the simplest option.

Re: A program that constantly runs and check the database.

Posted: Sun Mar 07, 2010 6:34 pm
by safmaya
Hi,

Thank you for the solution, but I have to run the server on a windows machine, so use of crontab is not allowed.

tnx

Re: A program that constantly runs and check the database.

Posted: Sun Mar 07, 2010 8:26 pm
by Griven
There are Windows programs that simulate Linux's cron, although I have yet to try any of them.

Try this:
1. Make sure that you can run PHP scripts from the command line
2. Write a PHP script that checks the DB and sends emails just like how you describe, and save it somewhere
3. Create a scheduled task using Windows' built-in Task Scheduler that calls up the PHP executable with the script you wrote as an argument

I've never tried that before, but I think it may work.

Re: A program that constantly runs and check the database.

Posted: Mon Mar 08, 2010 12:43 pm
by safmaya
Thanks for your response, maybe if I explain exactly what I need might help clarifying the problem, I have a database of users that have entered their event into so that they can be notified on the specified time, so basically the job of the server is to constantly look at the database and send emails to the users or invoke skype to send text messges depending on the user's choice.

My gut's tells me not to rely on windwos scheduler, I am looking for a more robust method to do this, of course if such method doens't exist, your option seems reasonable.

Tnx

Re: A program that constantly runs and check the database.

Posted: Mon Mar 08, 2010 1:25 pm
by John Cartwright
safmaya wrote:Thanks for your response, maybe if I explain exactly what I need might help clarifying the problem, I have a database of users that have entered their event into so that they can be notified on the specified time, so basically the job of the server is to constantly look at the database and send emails to the users or invoke skype to send text messges depending on the user's choice.

My gut's tells me not to rely on windwos scheduler, I am looking for a more robust method to do this, of course if such method doens't exist, your option seems reasonable.

Tnx
The alternative is to have an inifitely looped PHP script that sleeps for smaller intervals.

Code: Select all

while (1) {
   //do stuff
 
   sleep(5); //sleep for 5 seconds
}
However, these types of daemons aren't really suited for PHP for scalability and performance reasons. I.e., if you need to trigger many events, a threaded environment is more suitable (C#, etc).

Re: A program that constantly runs and check the database.

Posted: Mon Mar 08, 2010 1:34 pm
by safmaya
John Cartwright wrote:
safmaya wrote:Thanks for your response, maybe if I explain exactly what I need might help clarifying the problem, I have a database of users that have entered their event into so that they can be notified on the specified time, so basically the job of the server is to constantly look at the database and send emails to the users or invoke skype to send text messges depending on the user's choice.

My gut's tells me not to rely on windwos scheduler, I am looking for a more robust method to do this, of course if such method doens't exist, your option seems reasonable.

Tnx
The alternative is to have an inifitely looped PHP script that sleeps for smaller intervals.

Code: Select all

while (1) {
   //do stuff
 
   sleep(5); //sleep for 5 seconds
}
However, these types of daemons aren't really suited for PHP for scalability and performance reasons. I.e., if you need to trigger many events, a threaded environment is more suitable (C#, etc).

Hi,

Your solution seems very interesting, I doubt that it will cause any problems in terms of performance as I only expect it to run a mail script and trigger skype api, what do you think?

also a very supid question is where do I put this file(script) if I put it in the server is it gonna be executed or do I need to configure anything on my server can you please shed some light on this as I am very new to the web based applications.

Thank you.

Re: A program that constantly runs and check the database.

Posted: Mon Mar 08, 2010 9:12 pm
by Griven
If you use a script that sleeps for a short period, like John described, then you will have to have it launched on the server at all times either through a browser window, or via a PHP command line script. The reason for this is that PHP is only compiled at runtime, so for it to do anything, the script needs to be active.

You've got a few options here (script with a short wait, windows task scheduler, compiled language daemon), so I recommend that you try each one (probably in the order listed) and see what works best for you.

Re: A program that constantly runs and check the database.

Posted: Sat Mar 13, 2010 9:34 am
by safmaya
Thanks All, I managed to use the Windows vista (7) task schdualer to run the script every minute, bear in mind that you cannot do this in windows XP!

The problem is solved.