1 |
import DiscordClient from "../../client/Client"; |
import DiscordClient from "../../client/Client"; |
2 |
import { v4 as uuid } from 'uuid'; |
import { generate as randomstring } from 'randomstring'; |
3 |
import QueuedJob, { IQueuedJob } from "../../models/QueuedJob"; |
import { IQueuedJob } from "../../models/QueuedJob"; |
4 |
|
|
5 |
export interface QueueOptions { |
export interface QueueOptions { |
6 |
runAfter?: number; |
runAfter?: number; |
11 |
|
|
12 |
export default abstract class Queue { |
export default abstract class Queue { |
13 |
protected completed = false; |
protected completed = false; |
14 |
protected runOn: number = 0; |
public readonly runOn: number = 0; |
15 |
public readonly id: string; |
public readonly id: string; |
16 |
protected readonly model: IQueuedJob; |
public readonly model: IQueuedJob; |
17 |
protected readonly timeout: NodeJS.Timeout; |
protected readonly timeout: NodeJS.Timeout; |
18 |
|
public readonly guild: string | undefined; |
19 |
|
|
20 |
constructor(protected client: DiscordClient, { runAfter, id, runAt, model }: QueueOptions) { |
constructor(protected client: DiscordClient, { runAfter, id, runAt, model }: QueueOptions) { |
21 |
if (runAfter !== 0 && runAfter !== 0 && !runAfter && !runAt) { |
if (runAfter !== 0 && runAfter !== 0 && !runAfter && !runAt) { |
23 |
} |
} |
24 |
|
|
25 |
this.runOn = runAfter ? Date.now() + runAfter : runAt!.getTime(); |
this.runOn = runAfter ? Date.now() + runAfter : runAt!.getTime(); |
26 |
this.id = id ?? uuid(); |
this.id = id ?? randomstring(7); |
27 |
this.model = model; |
this.model = model; |
28 |
|
this.guild = model.guild; |
29 |
|
|
30 |
const ms = this.runOn - Date.now(); |
const ms = this.runOn - Date.now(); |
31 |
|
|
37 |
console.log('Queue created: ', this.constructor.name, this.id); |
console.log('Queue created: ', this.constructor.name, this.id); |
38 |
} |
} |
39 |
|
|
40 |
|
get data() { |
41 |
|
return this.model.data; |
42 |
|
} |
43 |
|
|
44 |
async finish() { |
async finish() { |
45 |
this.client.queueManager.removeQueue(this); |
this.client.queueManager.removeQueue(this); |
46 |
console.log("Job complete: ", this.constructor.name); |
console.log("Job complete: ", this.constructor.name); |
49 |
async cancel() { |
async cancel() { |
50 |
clearTimeout(this.timeout); |
clearTimeout(this.timeout); |
51 |
await this.model.delete(); |
await this.model.delete(); |
52 |
|
this.client.queueManager.removeQueue(this); |
53 |
} |
} |
54 |
|
|
55 |
abstract execute(data?: { [key: string | number]: any }): Promise<any>; |
abstract execute(data?: { [key: string | number]: any }): Promise<any>; |