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 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真