/[sudobot]/branches/7.x/src/utils/QueueEntry.ts
ViewVC logotype

Contents of /branches/7.x/src/utils/QueueEntry.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (show annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 3698 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 /*
2 * This file is part of SudoBot.
3 *
4 * Copyright (C) 2021-2023 OSN Developers.
5 *
6 * SudoBot is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * SudoBot is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 import path from "path";
21 import Queue, { QueueConstructorOptions } from "./Queue";
22 import { log, logError } from "./logger";
23
24 export default class QueueEntry {
25 timeout: NodeJS.Timeout | undefined = undefined;
26 public id: number = 0;
27 public readonly creatingRecord: Promise<void> | undefined;
28
29 constructor(public readonly options: QueueConstructorOptions) {
30 if (options.id) this.id = options.id;
31 else this.creatingRecord = this.createDatabaseRecord().catch(logError);
32
33 log(options.filePath);
34 }
35
36 async createDatabaseRecord() {
37 const { id } = await this.options.client.prisma.queue.create({
38 data: {
39 channel_id: this.options.channelId,
40 file_name: path.basename(this.options.filePath),
41 guild_id: this.options.guild.id,
42 message_id: this.options.messageId,
43 user_id: this.options.userId,
44 willRunAt: this.options.willRunAt,
45 args: this.options.args,
46 name: this.options.name
47 }
48 });
49
50 this.id = id;
51 }
52
53 async updateTime(time: Date) {
54 await this.clearTimeout(false);
55 this.options.willRunAt = time;
56
57 await this.options.client.prisma.queue.update({
58 where: {
59 id: this.options.id
60 },
61 data: {
62 willRunAt: time
63 }
64 });
65
66 this.setTimeout();
67 }
68
69 async deleteDatabaseRecord() {
70 log("Deleting", this.id);
71
72 return await this.options.client.prisma.queue
73 .delete({
74 where: {
75 id: this.id
76 }
77 })
78 .catch(logError);
79 }
80
81 async run(): Promise<any> {
82 try {
83 log("Running queue: ", this.options.filePath);
84 const { default: QueueClass }: { default: new (options: QueueConstructorOptions) => Queue } = await import(this.options.filePath);
85 const queue = new QueueClass(this.options);
86 await queue.run(...this.options.args);
87 } catch (e) {
88 logError(e);
89 logError("Error occurred during running the queue.");
90 }
91
92 this.options.client.queueManager.queues.delete(this.id.toString());
93 }
94
95 setTimeout() {
96 log("Queue timeout set: ", path.basename(this.options.filePath));
97
98 this.timeout = setTimeout(() => {
99 this.run().catch(console.error);
100 this.deleteDatabaseRecord().catch(logError);
101 }, this.options.willRunAt.getTime() - Date.now());
102 }
103
104 clearTimeout(deleteRecord: boolean = false) {
105 log(this.timeout);
106
107 if (this.timeout === undefined) return;
108
109 log("Queue timeout cleared: ", path.basename(this.options.filePath));
110
111 clearTimeout(this.timeout);
112 this.timeout = undefined;
113
114 if (deleteRecord) return this.deleteDatabaseRecord();
115 }
116 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26