-
Notifications
You must be signed in to change notification settings - Fork 79
/
job-builder.js
139 lines (121 loc) · 3.25 KB
/
job-builder.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
const { join } = require('path');
const isSANB = require('is-string-and-not-blank');
const isValidPath = require('is-valid-path');
const later = require('@breejs/later');
const { boolean } = require('boolean');
const { isSchedule, parseValue, getJobPath } = require('./job-utils');
later.date.localTime();
// eslint-disable-next-line complexity
const buildJob = (job, config) => {
if (isSANB(job)) {
const path = join(
config.root,
getJobPath(job, config.acceptedExtensions, config.defaultExtension)
);
const jobObject = {
name: job,
path,
timeout: config.timeout,
interval: config.interval
};
if (isSANB(config.timezone)) {
jobObject.timezone = config.timezone;
}
return jobObject;
}
if (typeof job === 'function') {
const path = `(${job.toString()})()`;
const jobObject = {
name: job.name,
path,
worker: { eval: true },
timeout: config.timeout,
interval: config.interval
};
if (isSANB(config.timezone)) {
jobObject.timezone = config.timezone;
}
return jobObject;
}
// Process job.path
if (typeof job.path === 'function') {
const path = `(${job.path.toString()})()`;
job.path = path;
job.worker = {
eval: true,
...job.worker
};
} else {
const path = isSANB(job.path)
? job.path
: join(
config.root,
getJobPath(
job.name,
config.acceptedExtensions,
config.defaultExtension
)
);
if (isValidPath(path)) {
job.path = path;
} else {
// Assume that it's a transformed eval string
job.worker = {
eval: true,
...job.worker
};
}
}
if (typeof job.timeout !== 'undefined') {
job.timeout = parseValue(job.timeout);
}
if (typeof job.interval !== 'undefined') {
job.interval = parseValue(job.interval);
}
// Build cron
if (typeof job.cron !== 'undefined') {
if (isSchedule(job.cron)) {
job.interval = job.cron;
// Delete job.cron;
} else {
job.interval = later.parse.cron(
job.cron,
boolean(
typeof job.hasSeconds === 'undefined'
? config.hasSeconds
: job.hasSeconds
)
);
}
}
// If timeout was undefined, cron was undefined,
// and date was undefined then set the default
// (as long as the default timeout is >= 0)
if (
Number.isFinite(config.timeout) &&
config.timeout >= 0 &&
typeof job.timeout === 'undefined' &&
typeof job.cron === 'undefined' &&
typeof job.date === 'undefined' &&
typeof job.interval === 'undefined'
) {
job.timeout = config.timeout;
}
// If interval was undefined, cron was undefined,
// and date was undefined then set the default
// (as long as the default interval is > 0, or it was a schedule, or it was valid)
if (
((Number.isFinite(config.interval) && config.interval > 0) ||
isSchedule(config.interval)) &&
typeof job.interval === 'undefined' &&
typeof job.cron === 'undefined' &&
typeof job.date === 'undefined'
) {
job.interval = config.interval;
}
if (isSANB(config.timezone) && !job.timezone) {
job.timezone = config.timezone;
}
return job;
};
module.exports = buildJob;