initial commit
[browser-rts.git] / src / tasks / task.ts
1 import Bull from "bull";
2
3 type TaskHandler<T> = (task: Task<T>, job: Bull.Job<T>) => void;
4
5 export class Task<T> {
6     name: string;
7     queue: Bull.Queue;
8     handler: TaskHandler<T>;
9     constructor(name: string, handler: TaskHandler<T>) {
10         this.name = name;
11         this.queue = new Bull(this.name);
12
13         this.onJob(handler);
14     }
15
16     onJob(handler: TaskHandler<T>) {
17         this.queue.process(job => {
18             handler(this, job);
19         });
20     }
21
22     trigger(data: T, opts?: Bull.JobOptions) {
23         this.queue.add(data, opts);
24     }
25 }