@cleverbrush/scheduler 1.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/CloneReadableStream.d.ts +9 -0
- package/dist/CloneReadableStream.d.ts.map +1 -0
- package/dist/CloneReadableStream.js +25 -0
- package/dist/CloneReadableStream.js.map +1 -0
- package/dist/ScheduleCalculator.d.ts +10 -0
- package/dist/ScheduleCalculator.d.ts.map +1 -0
- package/dist/ScheduleCalculator.js +208 -0
- package/dist/ScheduleCalculator.js.map +1 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +307 -0
- package/dist/index.js.map +1 -0
- package/dist/jobRepository.d.ts +32 -0
- package/dist/jobRepository.d.ts.map +1 -0
- package/dist/jobRepository.js +84 -0
- package/dist/jobRepository.js.map +1 -0
- package/dist/reports.d.ts +0 -0
- package/dist/reports.js +231 -0
- package/dist/types.d.ts +577 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +122 -0
- package/dist/types.js.map +1 -0
- package/package.json +23 -0
- package/src/ScheduleCalculator.test.ts +482 -0
- package/src/ScheduleCalculator.ts +355 -0
- package/src/index.ts +419 -0
- package/src/jobRepository.ts +135 -0
- package/src/types.ts +219 -0
- package/tsconfig.json +17 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { InferType, SchemaRegistry } from '@cleverbrush/schema';
|
|
2
|
+
|
|
3
|
+
import { IJobRepository } from './jobRepository.js';
|
|
4
|
+
|
|
5
|
+
const registry = new SchemaRegistry()
|
|
6
|
+
.addPreprocessor('StringToDate', (value) => {
|
|
7
|
+
if (typeof value === 'undefined') return value;
|
|
8
|
+
if (typeof value === 'string') {
|
|
9
|
+
const time = Date.parse(value);
|
|
10
|
+
if (Number.isNaN(time)) return value;
|
|
11
|
+
return new Date(time);
|
|
12
|
+
}
|
|
13
|
+
return value;
|
|
14
|
+
})
|
|
15
|
+
.addSchema('Common.Date', ({ object }) =>
|
|
16
|
+
object()
|
|
17
|
+
.mapToType<Date>()
|
|
18
|
+
.addValidator((value) =>
|
|
19
|
+
value instanceof Date && !Number.isNaN(value)
|
|
20
|
+
? {
|
|
21
|
+
valid: true
|
|
22
|
+
}
|
|
23
|
+
: {
|
|
24
|
+
valid: false,
|
|
25
|
+
errors: ['should be a valid Date object']
|
|
26
|
+
}
|
|
27
|
+
)
|
|
28
|
+
)
|
|
29
|
+
.addSchema('Templates.Schedule', ({ object, number, alias }) =>
|
|
30
|
+
object({
|
|
31
|
+
/** Number of days between repeats */
|
|
32
|
+
interval: number().min(1).max(356),
|
|
33
|
+
/** Hour (0-23) */
|
|
34
|
+
hour: number().min(0).max(23).optional(),
|
|
35
|
+
/** Minute (0-59) */
|
|
36
|
+
minute: number().min(0).max(59).optional(),
|
|
37
|
+
/** Do not start earlier than this date */
|
|
38
|
+
startsOn: alias('Common.Date').optional(),
|
|
39
|
+
/** Do not repeat after this date */
|
|
40
|
+
endsOn: alias('Common.Date').optional(),
|
|
41
|
+
/** Max number of repeats (min 1) */
|
|
42
|
+
maxOccurences: number().min(1).optional(),
|
|
43
|
+
/** Skip this number of repeats - 1. Min value is 1. */
|
|
44
|
+
startingFromIndex: number().min(1).optional()
|
|
45
|
+
})
|
|
46
|
+
.setPropPreprocessor('endsOn', 'StringToDate')
|
|
47
|
+
.addValidator((val) => {
|
|
48
|
+
if (
|
|
49
|
+
'endsOn' in val &&
|
|
50
|
+
'maxOccurences' in val &&
|
|
51
|
+
// TODO: remove this clause when object validator is fixed in a new version
|
|
52
|
+
typeof val.endsOf !== 'undefined'
|
|
53
|
+
) {
|
|
54
|
+
return {
|
|
55
|
+
valid: false,
|
|
56
|
+
errors: ['either endsOn or maxOccurences is required']
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return { valid: true };
|
|
60
|
+
})
|
|
61
|
+
)
|
|
62
|
+
.addSchemaFrom(
|
|
63
|
+
'Templates.Schedule',
|
|
64
|
+
'Models.TaskScheduleMinute',
|
|
65
|
+
({ string, schema }) =>
|
|
66
|
+
schema
|
|
67
|
+
.removeProp('hour')
|
|
68
|
+
.removeProp('minute')
|
|
69
|
+
.addProps({
|
|
70
|
+
/** Repeat every minute */
|
|
71
|
+
every: string('minute')
|
|
72
|
+
})
|
|
73
|
+
)
|
|
74
|
+
.addSchemaFrom(
|
|
75
|
+
'Templates.Schedule',
|
|
76
|
+
'Models.TaskScheduleDay',
|
|
77
|
+
({ string, schema }) =>
|
|
78
|
+
schema.addProps({
|
|
79
|
+
/** Repeat every day */
|
|
80
|
+
every: string('day')
|
|
81
|
+
})
|
|
82
|
+
)
|
|
83
|
+
.addSchemaFrom(
|
|
84
|
+
'Templates.Schedule',
|
|
85
|
+
'Models.TaskScheduleWeek',
|
|
86
|
+
({ schema, number, string, array }) =>
|
|
87
|
+
schema.addProps({
|
|
88
|
+
/** Repeat every week */
|
|
89
|
+
every: string('week'),
|
|
90
|
+
/** Day of week: array of no more than 7 numbers numbers (from 1 to 7 where 1 is Monday). */
|
|
91
|
+
dayOfWeek: array()
|
|
92
|
+
.ofType(number().min(1).max(7))
|
|
93
|
+
.minLength(1)
|
|
94
|
+
.maxLength(7)
|
|
95
|
+
.addValidator((val) => {
|
|
96
|
+
const map = {};
|
|
97
|
+
for (let i = 0; i < val.length; i++) {
|
|
98
|
+
if (map[val[i]]) {
|
|
99
|
+
return {
|
|
100
|
+
valid: false,
|
|
101
|
+
errors: ['no duplicates allowed']
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
map[val[i]] = true;
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
valid: true
|
|
108
|
+
};
|
|
109
|
+
})
|
|
110
|
+
})
|
|
111
|
+
)
|
|
112
|
+
.addSchemaFrom(
|
|
113
|
+
'Templates.Schedule',
|
|
114
|
+
'Models.TaskScheduleMonth',
|
|
115
|
+
({ schema, string, number, union }) =>
|
|
116
|
+
schema.addProps({
|
|
117
|
+
/** Repeat every month */
|
|
118
|
+
every: string('month'),
|
|
119
|
+
/** Day - 'last' or number from 1 to 28 */
|
|
120
|
+
day: union(string('last'), number().min(1).max(28))
|
|
121
|
+
})
|
|
122
|
+
)
|
|
123
|
+
.addSchemaFrom(
|
|
124
|
+
'Templates.Schedule',
|
|
125
|
+
'Models.TaskScheduleYear',
|
|
126
|
+
({ schema, string, number, union }) =>
|
|
127
|
+
schema.addProps({
|
|
128
|
+
/** Repeat every year */
|
|
129
|
+
every: string('year'),
|
|
130
|
+
/** Day - 'last' or number from 1 to 28 */
|
|
131
|
+
day: union(string('last')).or(number().min(1).max(28)),
|
|
132
|
+
/** Month - number from 1 to 12 */
|
|
133
|
+
month: number().min(1).max(12)
|
|
134
|
+
})
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
export const schemaRegistry = registry
|
|
138
|
+
.addSchema('Models.Schedule', ({ alias, union }) =>
|
|
139
|
+
union(
|
|
140
|
+
alias('Models.TaskScheduleMinute'),
|
|
141
|
+
alias('Models.TaskScheduleDay'),
|
|
142
|
+
alias('Models.TaskScheduleWeek'),
|
|
143
|
+
alias('Models.TaskScheduleMonth'),
|
|
144
|
+
alias('Models.TaskScheduleYear')
|
|
145
|
+
)
|
|
146
|
+
)
|
|
147
|
+
.addSchema(
|
|
148
|
+
'Models.CreateJobRequest',
|
|
149
|
+
({ object, number, string, alias, union, func }) =>
|
|
150
|
+
object({
|
|
151
|
+
/** Id of job, must be uniq */
|
|
152
|
+
id: string(),
|
|
153
|
+
/** Path to js file (relative to root folder) */
|
|
154
|
+
path: string().minLength(1),
|
|
155
|
+
/** Job's schedule */
|
|
156
|
+
schedule: alias('Models.Schedule'),
|
|
157
|
+
/** Timeout for job (in milliseconds) */
|
|
158
|
+
timeout: number().min(0).optional(),
|
|
159
|
+
/** Arbitrary props for job (can be a callback returning props or Promise<props>) */
|
|
160
|
+
props: union(object().canHaveUnknownProps(), func()).optional(),
|
|
161
|
+
/** Job will be considered as disabled when more than that count of runs fails consequently */
|
|
162
|
+
maxConsequentFails: number().min(0).optional()
|
|
163
|
+
})
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
export type Schedule = InferType<
|
|
167
|
+
typeof schemaRegistry.schemas.Models.Schedule.schema
|
|
168
|
+
>;
|
|
169
|
+
|
|
170
|
+
export type CreateJobRequest = InferType<
|
|
171
|
+
typeof schemaRegistry.schemas.Models.CreateJobRequest.schema
|
|
172
|
+
>;
|
|
173
|
+
|
|
174
|
+
export const Schemas = schemaRegistry;
|
|
175
|
+
|
|
176
|
+
export type Job = {
|
|
177
|
+
id: string;
|
|
178
|
+
status: JobStatus;
|
|
179
|
+
schedule: Schedule;
|
|
180
|
+
path: string;
|
|
181
|
+
timeout: number;
|
|
182
|
+
createdAt: Date;
|
|
183
|
+
startedAt?: Date;
|
|
184
|
+
firstInstanceEndedAt?: Date;
|
|
185
|
+
timesRunned?: number;
|
|
186
|
+
successfullTimesRunned?: number;
|
|
187
|
+
consequentFailsCount: number;
|
|
188
|
+
maxConsequentFails: number;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
export type JobInstance = {
|
|
192
|
+
id: number;
|
|
193
|
+
jobId: string;
|
|
194
|
+
index: number;
|
|
195
|
+
status: JobInstanceStatus;
|
|
196
|
+
timeout: number;
|
|
197
|
+
scheduledTo: Date;
|
|
198
|
+
startDate?: Date;
|
|
199
|
+
endDate?: Date;
|
|
200
|
+
stdOut?: string;
|
|
201
|
+
stdErr?: string;
|
|
202
|
+
exitCode?: number;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
export type JobSchedulerProps = {
|
|
206
|
+
rootFolder: string;
|
|
207
|
+
defaultTimeZone?: string;
|
|
208
|
+
persistRepository?: IJobRepository;
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
export type JobStatus = 'active' | 'disabled' | 'finished';
|
|
212
|
+
export type SchedulerStatus = 'started' | 'stopped';
|
|
213
|
+
export type JobInstanceStatus =
|
|
214
|
+
| 'running'
|
|
215
|
+
| 'errored'
|
|
216
|
+
| 'succeeded'
|
|
217
|
+
| 'scheduled'
|
|
218
|
+
| 'timedout'
|
|
219
|
+
| 'canceled';
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"rootDir": "./src",
|
|
4
|
+
"outDir": "./dist",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"target": "esnext",
|
|
7
|
+
"module": "CommonJS",
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"moduleResolution": "Node",
|
|
10
|
+
"allowSyntheticDefaultImports": true,
|
|
11
|
+
"sourceMap": true
|
|
12
|
+
},
|
|
13
|
+
"files": ["src/index.ts"],
|
|
14
|
+
"watchOptions": {
|
|
15
|
+
"excludeDirectories": ["./dist"]
|
|
16
|
+
}
|
|
17
|
+
}
|