I'm trying to run a task each day at 8:00 AM in a vibe.d web app.
For the moment, I use the setTimer
function with the periodic parameter to true
. But this way, I can't control exactly the hour at which the task will be triggered. Is there an easy way to do this in vibed ?
1 回答
2
谢谢sigod,这正是我所做的。我计算直到下一个上午 8:00 的时间并调用 setTimer。这是供进一步参考的代码:
void startDailyTaskAtTime(TimeOfDay time, void delegate() task) {
// Get the current date and time
DateTime now = cast(DateTime)Clock.currTime();
// Get the next time occurrence
DateTime nextOcc = cast(DateTime)now;
if (now.timeOfDay >= time) {
nextOcc += dur!"days"(1);
}
nextOcc.timeOfDay = time;
// Get the duration before now and the next occurrence
Duration timeBeforeNextOcc = nextOcc - now;
void setDailyTask() {
// Run the task once
task();
// Run the task all subsequent days at the same time
setTimer(1.days, task, true);
}
setTimer(timeBeforeNextOcc, &setDailyTask);
}
于 2017-04-11T18:10:33.877 回答