开发者

How to set up website periodic tasks?

开发者 https://www.devze.com 2022-12-18 03:28 出处:网络
I\'m not sure if the topic is appropiate to the question... Anyway, suppose I\'ve a website, done with PHP or JSP and a cheap hosting with limited functionalities. More precisely I don\'t own the serv

I'm not sure if the topic is appropiate to the question... Anyway, suppose I've a website, done with PHP or JSP and a cheap hosting with limited functionalities. More precisely I don't own the server and I can't run daemons or services at my will. Now I want to run periodic (say every minute or two) tasks to do certain statistics. They are likely to be time consuming so I just can't repeat the calculation for every user accessing a page. I could do the calculation once when a user loads a page and I calculate that enough time has passed, but in this situation if the calculation gets very long the response time may be excessive and timeout (it's not likely I don't mean to run such long task, but I'm considering the worst case).

So given these costraints, what solutio开发者_如何学运维ns would you suggest?


Each cheap hosting will have support of crontabs. Check out the hosting packages first. If not, load in the page, and launch the task by AJAX. This way your response time doesn't suffer and you do in a different thread the work.


If you choose to use crontab, you're going to have to know a bit more to execute your PHP scripts from them. Depending on if your PHP executes as CGI or an apache module has an effect. There's a good article on how to do this at:

http://www.htmlcenter.com/blog/running-php-scripts-with-cron/

If you don't have access to crontab on your hosting provider (find a new one) there are other options. For example:

http://www.setcronjob.com/

Will call a script on your site, remotely every X period .. you have to renew it once a month I think. If you take their paid service ($5/year according to the front page) I think the jobs you set up last until you cancel them or your paid term runs out.


In Java, you could just create a Timer. It can create a background thread that will perform a given function every so often.

My preference would be to start the Timer object in a ContextListener.contextInitialized() method, but you may have a more appropriate place.

0

精彩评论

暂无评论...
验证码 换一张
取 消