-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasker.ts
More file actions
44 lines (41 loc) · 1.18 KB
/
tasker.ts
File metadata and controls
44 lines (41 loc) · 1.18 KB
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
import { sleep } from "https://deno.land/x/sleep/mod.ts";
import { Connection } from "./connection.ts";
import { WorkerClass } from "./workerClass.ts";
export class Tasker {
queue: string;
connection: Connection;
workerClasses: typeof WorkerClass[];
constructor(
connection: Connection,
queue: string,
workerClasses: typeof WorkerClass[],
) {
this.queue = queue;
this.connection = connection;
this.workerClasses = workerClasses;
}
async run() {
while (true) {
const taskDetails = await this.connection.redis.lpop(this.queue);
if (taskDetails) {
try {
const decodedTask = JSON.parse(taskDetails);
const className = decodedTask.klass;
const params: Object = decodedTask.params;
const klass = this.workerClasses.find(
(workerClass: typeof WorkerClass) => workerClass.name === className,
);
if (klass) {
const instance: WorkerClass = new klass();
instance.perform(params);
}
} catch (e) {
console.log("Error occured. Skipping job");
console.log(e);
}
} else {
await sleep(5);
}
}
}
}