获取下一个计划任务

获取下一个计划任务

本文介绍了ColdFusion - 由于运行,获取下一个计划任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此线程在查找预定任务的下一个运行时时非常有用。



函数=http://www.cflib.org =nofollow> http://www.cflib.org )。



Rules:




  • 返回一个包含名称和剩余时间的结构,直到下一个任务。如果没有找到任务, result.task 是一个空字符串。

  • 排除暂停的任务



用法:

  result = new TaskUtil()。getNextTask(); 
WriteDump(result);

CFC

 组件{
public struct function getNextTask(){
//从工厂获取即将到来的任务列表(UNDOCUMENTED)
local.scheduler = createObject java,coldfusion.server.ServiceFactory)。getSchedulerService();
local.taskField = local.scheduler.getClass()。getDeclaredField(_ tasks);
local.taskField.setAccessible(true);
local.taskList = local.taskField.get(local.scheduler);

// taskList还包含系统作业,因此我们必须通过任务迭代
//来查找下一个计划任务
local.nextTask =;
local.tasks = local.taskList.iterator();

while(local.tasks.hasNext()){
local.currTask = local.tasks.next();
local.className = local.currTask.getRunnable()。getClass()。name;

//当我们发现一个未暂停的计划任务时退出
if(local.className eqcoldfusion.scheduling.CronTabEntry
&&!local .currTask.getRunnable()。paused){
local.nextTask = local.currTask;
break;
}
}

//如果我们找到一个任务,计算多少天,小时等等
//直到下一个运行时
local .details = {task =,remaining = {}};

if(isObject(local.nextTask)){
local.secondsToGo =(local.nextTask.getWhen() - now()。getTime())/ 1000;
local.details.task = local.nextTask.getRunnable()。task;
local.details.remaining = createTimeStruct(local.secondsToGo);
local.details.nextDate = dateAdd(s,local.nextTask.getWhen()/ 1000
,January 1 1970 00:00:00);
}
return local.details;
}

/ **
* Dave Pomerance的CreateTimeStruct的缩写版本
*参见http://www.cflib.org/index.cfm?event= page.udfbyid& udfid = 421
*
* @param timespan要转换的时间跨度。
* @return返回一个结构。
* @author Dave Pomerance
* @version 1,2002年1月7日
* /
public struct function CreateTimeStruct(required numeric timespan){
var timruct = StructNew ();
var mask =s;

//只有4个允许的掩码值 - 如果不是其中之一,返回空白struct
if(ListFind(d,h,m,s,mask)){
//计算秒
if(mask eqs){
timruct.s =(timespan mod 60)+(timespan-Int(timespan));
timespan = int(timespan / 60);
mask =m;
} else timestruct.s = 0;
//计算分钟
if(mask eqm){
timruct.m = timespan mod 60;
timespan = int(timespan / 60);
mask =h;
} else timruct.m = 0;
//计算小时,天
if(mask eqh){
timruct.h = timespan mod 24;
timestruct.d = int(timespan / 24);
} else {
timruct.h = 0;
timestruct.d = timespan;
}
}

return timruct;
}

}


This thread was useful in finding out the next run-time for a scheduled task.

How do I find out the next run time for a Scheduled Task?

But, is there also a way to simply get the next scheduled task due to run?

If I can get the date and name of the next task due to run, I can plug that date into a jQuery countdown timer, which will display a countdown to the next scheduled task, something like:

TaskABC due to run in:
12    03     20
hrs   min    sec

. This is for an admin interface in case you're wondering how geeky can people get:-)

EDIT

解决方案

I had the same thought as Bill. But was curious if there was another way.

I poked around and apparently the internal Scheduler class maintains a list of upcoming tasks. The list is private, but you can use the same reflection technique to access it. Interestingly the list also includes system tasks like the mail spooler, session/application trackers, watchers, etecetera. So you must iterate through it until you find a "scheduled task" ie CronTabEntry

Below is a very lightly tested function that seems to do the trick in CF9. (Note, includes the CreateTimeStruct function from http://www.cflib.org).

Rules:

  • Returns a structure containing the name and time remaining until the next task. If no tasks were found, result.task is an empty string.
  • Excludes paused tasks

Usage:

result = new TaskUtil().getNextTask();
WriteDump(result);

CFC

component {
     public struct function getNextTask() {
        // get list of upcoming tasks from factory (UNDOCUMENTED)
        local.scheduler = createObject("java", "coldfusion.server.ServiceFactory").getSchedulerService();
        local.taskField = local.scheduler.getClass().getDeclaredField("_tasks");
        local.taskField.setAccessible( true );
        local.taskList = local.taskField.get(local.scheduler);

        // taskList contains system jobs too, so we must iterate
        // through the tasks to find the next "scheduled task"
        local.nextTask = "";
        local.tasks = local.taskList.iterator();

        while ( local.tasks.hasNext() ) {
            local.currTask = local.tasks.next();
            local.className = local.currTask.getRunnable().getClass().name;

            // exit as soon as we find a scheduled task that is NOT paused
            if (local.className eq "coldfusion.scheduling.CronTabEntry"
                    && !local.currTask.getRunnable().paused) {
                local.nextTask = local.currTask;
                break;
            }
        }

        // if we found a task, calculate how many days, hours, etcetera
        // until its next run time
        local.details = { task="", remaining={} };

        if ( isObject(local.nextTask) ) {
            local.secondsToGo  = (local.nextTask.getWhen() - now().getTime()) / 1000;
            local.details.task = local.nextTask.getRunnable().task;
            local.details.remaining = createTimeStruct(local.secondsToGo);
            local.details.nextDate = dateAdd("s", local.nextTask.getWhen() / 1000
                                               , "January 1 1970 00:00:00"  );
        }
        return local.details;
     }

    /**
     * Abbreviated version of CreateTimeStruct by Dave Pomerance
     * See http://www.cflib.org/index.cfm?event=page.udfbyid&udfid=421
     *
     * @param timespan   The timespan to convert.
     * @return Returns a structure.
     * @author Dave Pomerance
     * @version 1, January 7, 2002
     */
    public struct function CreateTimeStruct(required numeric timespan) {
        var timestruct = StructNew();
        var mask = "s";

        // only 4 allowed values for mask - if not one of those, return blank struct
        if (ListFind("d,h,m,s", mask)) {
            // compute seconds
            if (mask eq "s") {
                timestruct.s = (timespan mod 60) + (timespan - Int(timespan));
                timespan = int(timespan/60);
                mask = "m";
            } else timestruct.s = 0;
            // compute minutes
            if (mask eq "m") {
                timestruct.m = timespan mod 60;
                timespan = int(timespan/60);
                mask = "h";
            } else timestruct.m = 0;
            // compute hours, days
            if (mask eq "h") {
                timestruct.h = timespan mod 24;
                timestruct.d = int(timespan/24);
            } else {
                timestruct.h = 0;
                timestruct.d = timespan;
            }
        }

        return timestruct;
    }

}

这篇关于ColdFusion - 由于运行,获取下一个计划任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:34