-
-
Notifications
You must be signed in to change notification settings - Fork 262
/
tasks.ts
177 lines (155 loc) · 5.03 KB
/
tasks.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
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
import * as glob from "glob";
import * as path from "path";
import { Plugin } from "node-resque";
import * as TaskModule from "./../modules/task";
import { api, log, utils, task, Initializer } from "../index";
const taskModule = task;
export interface TaskApi {
tasks: { [key: string]: any };
jobs: { [key: string]: any };
middleware: { [key: string]: TaskModule.task.TaskMiddleware };
globalMiddleware: Array<string>;
loadFile?: Function;
jobWrapper?: Function;
loadTasks?: Function;
}
/**
* Tools for enqueuing and inspecting the task system (delayed jobs).
*/
export class Tasks extends Initializer {
constructor() {
super();
this.name = "tasks";
this.loadPriority = 699;
this.startPriority = 900;
}
async initialize(config) {
api.tasks = {
tasks: {},
jobs: {},
middleware: {},
globalMiddleware: [],
};
api.tasks.loadFile = (fullFilePath: string, reload: boolean = false) => {
let task;
let collection = require(fullFilePath);
for (const i in collection) {
const TaskClass = collection[i];
task = new TaskClass();
task.validate();
if (api.tasks.tasks[task.name] && !reload) {
log(
`an existing task with the same name \`${task.name}\` will be overridden by the file ${fullFilePath}`,
"crit"
);
}
api.tasks.tasks[task.name] = task;
api.tasks.jobs[task.name] = api.tasks.jobWrapper(task.name);
log(
`task ${reload ? "(re)" : ""} loaded: ${task.name}, ${fullFilePath}`,
"debug"
);
}
};
api.tasks.jobWrapper = (taskName: string) => {
const task = api.tasks.tasks[taskName];
const middleware = task.middleware || [];
const plugins = task.plugins || [];
const pluginOptions = task.pluginOptions || {};
if (task.frequency > 0) {
if (plugins.indexOf("JobLock") < 0) {
plugins.push("JobLock");
}
if (plugins.indexOf("QueueLock") < 0) {
plugins.push("QueueLock");
}
if (plugins.indexOf("DelayQueueLock") < 0) {
plugins.push("DelayQueueLock");
}
}
// load middleware into plugins
const processMiddleware = (m) => {
if (api.tasks.middleware[m]) {
//@ts-ignore
class NodeResquePlugin extends Plugin {
constructor(...args) {
//@ts-ignore
super(...args);
if (api.tasks.middleware[m].preProcessor) {
//@ts-ignore
this.beforePerform = api.tasks.middleware[m].preProcessor;
}
if (api.tasks.middleware[m].postProcessor) {
//@ts-ignore
this.afterPerform = api.tasks.middleware[m].postProcessor;
}
if (api.tasks.middleware[m].preEnqueue) {
//@ts-ignore
this.beforeEnqueue = api.tasks.middleware[m].preEnqueue;
}
if (api.tasks.middleware[m].postEnqueue) {
//@ts-ignore
this.afterEnqueue = api.tasks.middleware[m].postEnqueue;
}
}
}
plugins.push(NodeResquePlugin);
}
};
api.tasks.globalMiddleware.forEach(processMiddleware);
middleware.forEach(processMiddleware);
return {
plugins,
pluginOptions,
perform: async function () {
const combinedArgs = [].concat(Array.prototype.slice.call(arguments));
combinedArgs.push(this);
let response = null;
try {
response = await task.run.apply(task, combinedArgs);
await taskModule.enqueueRecurrentTask(taskName);
} catch (error) {
if (task.frequency > 0 && task.reEnqueuePeriodicTaskIfException) {
await taskModule.enqueueRecurrentTask(taskName);
}
throw error;
}
return response;
},
};
};
api.tasks.loadTasks = (reload) => {
config.general.paths.task.forEach((p) => {
utils
.ensureNoTsHeaderFiles(
glob.sync(path.join(p, "**", "**/*(*.js|*.ts)"))
)
.forEach((f) => {
api.tasks.loadFile(f, reload);
});
});
for (const pluginName in config.plugins) {
if (config.plugins[pluginName].tasks !== false) {
const pluginPath = config.plugins[pluginName].path;
// old style at the root of the project
let files = glob.sync(path.join(pluginPath, "tasks", "**", "*.js"));
files = files.concat(
glob.sync(path.join(pluginPath, "dist", "tasks", "**", "*.js"))
);
utils.ensureNoTsHeaderFiles(files).forEach((f) => {
api.tasks.loadFile(f, reload);
});
}
}
};
api.tasks.loadTasks(false);
}
async start(config) {
if (config.redis.enabled === false) {
return;
}
if (config.tasks.scheduler === true) {
await taskModule.enqueueAllRecurrentTasks();
}
}
}