1 module scheduled.schedules.one_time;
2 
3 import std.datetime;
4 import std.typecons;
5 import scheduled.schedule;
6 
7 /** 
8  * Schedule which defines a job to be executed once at a fixed time.
9  *
10  * Note that if a job is scheduled with this schedule, and the execution time
11  * is set to a time in the past, the job will be executed immediately.
12  */
13 public class OneTimeSchedule : JobSchedule {
14     /**
15      * The time at which the job will be executed.
16      */
17     private SysTime executionTime;
18 
19     /** 
20      * Simple flag to indicate whether the job has already been executed. This
21      * is useful because 
22      */
23     private bool executed = false;
24 
25     /** 
26      * Constructs a one-time schedule to execute at the specified time.
27      * Params:
28      *   executionTime = The time to execute the job at.
29      */
30     this(SysTime executionTime) {
31         this.executionTime = executionTime;
32     }
33 
34     /** 
35      * Constructs a one-time schedule to execute after the given duration has
36      * passed, from the current system time.
37      * Params:
38      *   timeUntilExecution = The time from now until execution.
39      */
40     this(Duration timeUntilExecution) {
41         this(Clock.currTime + timeUntilExecution);
42     }
43 
44     /** 
45      * Gets the next execution time. If this one-time schedule has already
46      * executed once, it will always return null.
47      * Params:
48      *   currentTime = The current system time.
49      * Returns: The time at which jobs with this schedule should next be
50      * executed, or null if there is no known next execution time.
51      */
52     Nullable!SysTime getNextExecutionTime(SysTime currentTime) const {
53         if (this.executed) {
54             return Nullable!SysTime.init;
55         } else {
56             return nullable!SysTime(this.executionTime);
57         }
58     }
59 
60     /** 
61      * Marks this schedule as having been executed. Once this is called, no
62      * more jobs with this schedule will be executed.
63      * Params:
64      *   executionTime = The time at which this schedule's job was executed.
65      */
66     void markExecuted(SysTime executionTime) {
67         this.executed = true;
68     }
69 
70     /** 
71      * Tells the scheduler that this schedule is never repeating.
72      * Returns: Always false.
73      */
74     bool isRepeating() const {
75         return false;
76     }
77 }