此线程可用于查找计划任务的下一个运行时。
How do I find out the next run time for a Scheduled Task?
但是,还有一种方法可以简单地运行下一个计划任务吗?
如果我可以获得由于运行而导致的下一个任务的日期和名称,我可以将该日期插入到jQuery倒数计时器中,该计时器将显示下一个计划任务的倒计时,例如:
TaskABC due to run in:
12 03 20
hrs min sec
。这是一个管理界面,以防你想知道人们如何获得令人讨厌: - )
答案 0 :(得分:3)
我和比尔有同样的想法。但是,如果有另一种方式,我很好奇。
我戳了一下,显然内部Scheduler
类维护着即将到来的任务列表。该列表是私有的,但您可以使用相同的反射技术来访问它。有趣的是,该列表还包括系统任务,如邮件假脱机程序,会话/应用程序跟踪器,观察者,etecetera。因此,您必须遍历它,直到找到“计划任务”,即CronTabEntry
下面是一个非常轻微测试的功能,似乎在CF9中做了这个技巧。 (注意,包括CreateTimeStruct中的http://www.cflib.org函数)。
<强>规则:强>
result.task
为空字符串。<强>用法:强>
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;
}
}
答案 1 :(得分:2)
我的第一个想法是在任务集合上迭代Leigh的getNextRunTime(string taskName)
函数。您可以使用taskArray = createobject("java","coldfusion.server.ServiceFactory").getCronService().listAll();
包含任务名称的struct中的键是“task”。因此,您可以将所有任务名称提取为数组,例如,在每个元素上运行Leigh函数,并确定下一个元素将运行。