dongzhiyong8577 2012-06-09 08:55
浏览 154
已采纳

Javascript替代CronJob

Is there any javascript alternative to Cronjob?

The thing is, my boss doesn't want to use CronJob anymore for daily executions and told me that if we can do it with javascript instead of CronJob.

I wrote a php+javascript code. It basically collects the daily tasks data (which .php file to execute, what is the time interval etc.) from the database and put them in an object.

Then,

<script>
    function mainFunc(){    
        for(var i=0; i<sizeOfJobs(); i++){ //traverse in the object
            currentDate = new Date();
            //if any of the jobs execution time has come, execute it           
            if(jobs[i]['next_run'] <= currentDate){ 
                $.ajax({
                    url: jobs[i]['file_location'] ,
                    async: false, //this is another question, look below please
                    success: function(data){
                        //after the finish, set next_run and last_run                   
                        currentDate = new Date();                    
                        jobs[i]['last_run'] = currentDate;
                        var nextRun = new Date();
                        nextRun.setTime(currentDate.getTime() + (jobs[i]['interval'] * 60 * 1000));  
                        jobs[i]['next_run'] = nextRun;                        

                    }
                });            
            }                    
        }
        //repeat

        //currently 10 sec but it will increase according to jobs max runtime        
        setTimeout(mainFunc,10000); 
    } 


        $(document).ready(function(){
            setTimeout(mainFunc,10000);        
        })
</script>

So, I use this code. It works fine for basic jobs but there will be a huge jobs which will take 10+ min to finish (like deleting and refilling a db table with thousands of rows)

  • Is it safe?
  • Should I set "async" value to false or not?
  • I'm aware of the situation that there could be jobs which has to execute at the same time and if I set async false, every job needs to wait to finish the previous job etc. so I need to set setTimeout value to total max runtime of all the jobs.
  • If I set it true what it could be happen? My fear is, if a job can't finish before setTimeout interval, it's next_run will not be set and it will automatically re-execute. so should I set next_run value before the ajax call?

Back to the main topic, should I need to do all of this? Is there a better solution or library? (I googled but couldn't find anything useful.)

Thank you

  • 写回答

1条回答 默认 最新

  • doukezi4606 2012-06-15 13:01
    关注

    First of all, in my professional opinion, your boss is crazy :)

    Having said that, here's some changes you can make to address your fears:

    1. Create an array of job_execution_slots with the same length as your jobs array; initialize to null values.

    2. Before you do $.ajax() you check whether the job_execution_slots[i] is "taken" (not null).

    3. When the slot is empty, you do job_execution_slots[i] = $.ajax({...}) and make sure it's set to async: true; keeping the reference also allows you to "kill" a job by stopping the AJAX request.

    4. In your AJAX completion handler you do job_execution_slots[i] = null;

    This basically serializes the execution per job.

    Let me know if this makes sense to you; I can supply further details upon demand :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services