How to automatically run a function every 5 seconds in Yii?

The reason I need to run a function every 5 seconds is:

I need to send an activation email, but sending email in the registration form submission function takes too much time (more than 3 - 8 seconds).

So, I want to just save the registration info in the database, and send a batch of email activation every 5 seconds.

Do you think this is the best solution? (I am feeling my solution is awkward. ) If so, how to automatically run a function every 5 seconds? Please help!

Thank you!

Nicolas Xu

Hi nettrinity,

You can use javascript setTimeIntervel with a counter to calculate the number of your unsend member left. Every 5 sec trigger an ajax function to call controller to do send mail and update counter.

Or use an unique ajax call and inside the request use sleep php function to wait few seconds.

I tend to agree. But if that really is the road you want to pursue, you should probably look into cron jobs. An ajax call / PHP sleep function is not reliable. What happens if the user moves away from the page within those 5 seconds? No e-mail and (depending on your registration procedure) therefore no activation link is being received by the user.

You are probably best of trying to debug your submission function to find out what the bottleneck is. If you really cannot shave some time off I would opt for a cron job, but not every 5 seconds. That is just way too much unneeded load on the server.

Or write a console commad which is run on every minute under linux crontab or widows scheduled tasks.Within your script, manage intervals of 5 seconds.

Thanks, guys!

+1 for using crontab. It’s the best way, I think. Also you can implement your own scheduler in the application. By integrating this script, for example: http://www.phpjobscheduler.co.uk/