-
Notifications
You must be signed in to change notification settings - Fork 6
/
builder.ts
128 lines (105 loc) · 3.45 KB
/
builder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import {
EventReceiver,
DispatchableEvent,
TaskDispatcherEvent,
} from "../../events";
import { RunnableTask } from ".";
import { TimeUnit, toSeconds } from "../../utils/time-converter";
import { Logger, getLogger } from "../../utils/logger";
import { TaskParams } from "../task";
import { TaskPlanner } from "../planner";
import { PlannedTask } from "../planner/planned-task";
export interface ReadyRunnableTaskBuilder extends EventReceiver {
build(): RunnableTask;
plan(dispatchableEvent?: DispatchableEvent): Promise<PlannedTask>;
}
interface ScheduledRunnableTaskBuilder extends ReadyRunnableTaskBuilder {
cancelOn(eventName: string): ReadyRunnableTaskBuilder;
}
interface DelayedRunnableTaskBuilder extends ScheduledRunnableTaskBuilder {
every(time: number, timeUnit?: TimeUnit): ScheduledRunnableTaskBuilder;
}
export interface RunnableTaskBuilder extends ReadyRunnableTaskBuilder {
now(): ReadyRunnableTaskBuilder;
every(time: number, timeUnit?: TimeUnit): ScheduledRunnableTaskBuilder;
in(time: number, timeUnit?: TimeUnit): ScheduledRunnableTaskBuilder;
at(date: Date): DelayedRunnableTaskBuilder;
}
export class RunnableTaskBuilderImpl implements RunnableTaskBuilder {
private startAt: number;
private interval: number;
private recurrent: boolean;
private cancelEvent: string;
private logger: Logger;
constructor(
private taskName: string,
private params: TaskParams,
private taskPlanner?: TaskPlanner
) {
this.startAt = -1;
this.interval = 0;
this.recurrent = false;
this.cancelEvent = TaskDispatcherEvent.DefaultCancelEvent;
this.logger = getLogger("RunnableTaskBuilder");
}
now(): ReadyRunnableTaskBuilder {
this.startAt = -1;
this.interval = 0;
this.recurrent = false;
return this;
}
every(time: number, timeUnit?: TimeUnit): ScheduledRunnableTaskBuilder {
const seconds = timeUnit ? toSeconds(time, timeUnit) : time;
this.interval = seconds;
this.recurrent = true;
return this;
}
// TODO: Perhaps now it makes more sense for in to use startAt instead of interval.
// Will allow combining in with every, thus allowing running deferred recurrent tasks.
in(time: number, timeUnit?: TimeUnit): ScheduledRunnableTaskBuilder {
const seconds = timeUnit ? toSeconds(time, timeUnit) : time;
this.interval = seconds;
this.recurrent = false;
return this;
}
at(date: Date): DelayedRunnableTaskBuilder {
if (date > new Date()) {
this.startAt = date.getTime();
}
this.recurrent = false;
return this;
}
cancelOn(eventName: string): ReadyRunnableTaskBuilder {
this.cancelEvent = eventName;
return this;
}
build(): RunnableTask {
return {
name: this.taskName,
startAt: this.startAt,
interval: this.interval,
recurrent: this.recurrent,
cancelEvent: this.cancelEvent,
params: this.params,
};
}
async plan(dispatchableEvent?: DispatchableEvent): Promise<PlannedTask> {
const runnableTask = this.build();
try {
return this.taskPlanner.plan(runnableTask, dispatchableEvent);
} catch (err) {
throw new Error(
`Could not plan (${JSON.stringify(runnableTask)}). Reason: ${err}`
);
}
}
onReceive(dispatchableEvent: DispatchableEvent) {
this.plan(dispatchableEvent)
.then((plannedTask) => {
this.logger.info(`Task planned: ${JSON.stringify(plannedTask)}`);
})
.catch((err) => {
this.logger.error(err);
});
}
}