This repository has been archived by the owner on Oct 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minicron.js
executable file
·204 lines (176 loc) · 4.17 KB
/
minicron.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env node
import { stdin, stdout } from "process";
import * as readline from "readline";
// polyfills
Array.prototype.contains = function (element) {
return this.indexOf(element) !== -1;
};
// constants
const TIME_ARG_VALIDATE_REGEX =
/^(\d|[01]\d|[2][0123]):(\d|[0-5]\d)|24:00$/;
const JOB_VALIDATE_REGEX = /^(\d|[012345]\d|\*) (\d|[01]\d|[2][0123]|\*) (.*)$/;
/**
* @typedef Time
* @prop {number} hours
* @prop {number} minutes
*
* @typedef Job
* @prop {string} hourOfDay
* @prop {string} minuteOfHour
* @prop {string} command
*/
// utility functions
/**
* returns time parsed from arg or current time (if not correct arg provided).
* @returns {Time} time
*/
const getSimulatedTime = () => {
const input = process.argv[2];
if (input) {
if (!TIME_ARG_VALIDATE_REGEX.test(input)) {
throw new Error("Invalid simulated time argument");
}
const [hours, minutes] = input.split(":");
return {
hours: parseInt(hours),
minutes: parseInt(minutes),
};
}
const now = new Date();
return {
hours: now.getHours(),
minutes: now.getMinutes(),
};
};
/**
* @param {string} str
* @returns {Job} job
*/
const parseJob = (str) => {
if (!JOB_VALIDATE_REGEX.test(str)) {
throw new Error("Invalid input");
}
const [minutes, hours, ...command] = str.trim().split(/\s+/);
return {
hourOfDay: hours,
minuteOfHour: minutes,
command: command.join(" "),
};
};
/**
* @param {number} int
*/
const formatTimeNumber = (int) =>
int.toLocaleString(undefined, {
maximumFractionDigits: 0,
minimumIntegerDigits: 2,
});
/**
*
* @param {Date} time
* @returns {string}
*/
const formatTimeFromDate = (date) => {
return `${formatTimeNumber(date.getHours())}:${formatTimeNumber(
date.getMinutes(),
)}`;
};
// Next run calculation functions
const _findNextRunForHourlyJob = (now, job) => {
const minuteOfHour = parseInt(job.minuteOfHour);
return {
...now,
minutes: minuteOfHour < now.minutes ? 59 + minuteOfHour : minuteOfHour,
};
};
const _findNextRunForMinutelyJob = (now, job) => {
const hourOfDay = parseInt(job.hourOfDay);
if (hourOfDay < now.hours) {
return {
hours: 23 + hourOfDay,
minutes: 0,
};
} else {
return {
hours: hourOfDay,
minutes: hourOfDay === now.hours ? now.minutes : 0,
};
}
};
/**
* @param {Time} now
* @param {Job} job
* @returns {Time}
*/
const findNextRun = (now, job) => {
const isEachHourRun = job.hourOfDay === "*";
const isEachMinuteRun = job.minuteOfHour === "*";
if (isEachHourRun && isEachMinuteRun) {
return { ...now };
}
// handle hourly job;
if (isEachHourRun) {
return _findNextRunForHourlyJob(now, job);
}
// every minute during defined hour
if (isEachMinuteRun) {
return _findNextRunForMinutelyJob(now, job);
}
// find next run for daily job
const minuteOfHour = parseInt(job.minuteOfHour);
const hourOfDay = parseInt(job.hourOfDay);
return {
hours: hourOfDay < now.hours ? hourOfDay + 24 : hourOfDay,
minutes: minuteOfHour,
};
};
/**
* @param {Time} time
* @param {string} jobStr
* @returns
*/
const processJobString = (time, jobStr) => {
const job = parseJob(jobStr);
const nextRun = findNextRun(time, job);
const date = new Date();
const currentDay = date.getDate();
date.setHours(nextRun.hours, nextRun.minutes);
const isNextRunTomorrow = currentDay !== date.getDate();
return `${formatTimeFromDate(date)} ${
isNextRunTomorrow ? "tomorrow" : "today"
} ${job.command}`;
};
// entry point
const main = () => {
/** @type {Time} */
let simulatedTime;
try {
simulatedTime = getSimulatedTime();
} catch (error) {
console.error(error);
process.exit(-1);
}
const rl = readline.createInterface(stdin);
rl.on("line", (input) => {
try {
const output = processJobString(simulatedTime, input);
stdout.write(`${output}\n`);
} catch (error) {
console.error(error);
process.exit(-1);
}
});
};
// handle help arg
if (process.argv.contains("--help") || process.argv.contains("-h")) {
console.log(`Usage: cat input.txt | node minicron.js [time]`);
console.log(`* time must be in 24h format (16:10)`);
process.exit(0);
}
// exit in case no input stream attached.
if (stdin.isTTY) {
console.error("Input stream not attached");
process.exit(-1);
}
// start program
main();