/[sudobot]/trunk/src/services/QueueManager.ts
ViewVC logotype

Diff of /trunk/src/services/QueueManager.ts

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 423 by rakin, Mon Jul 29 17:30:09 2024 UTC revision 431 by rakin, Mon Jul 29 17:30:12 2024 UTC
# Line 1  Line 1 
1    /**
2    * This file is part of SudoBot.
3    *
4    * Copyright (C) 2021-2022 OSN Inc.
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 { Collection } from "discord.js";  import { Collection } from "discord.js";
21  import DiscordClient from "../client/Client";  import DiscordClient from "../client/Client";
22  import QueuedJob from "../models/QueuedJob";  import QueuedJob from "../models/QueuedJob";
23  import Queue, { QueueOptions } from "../utils/structures/Queue";  import Queue, { QueueOptions } from "../utils/structures/Queue";
24  import Service from "../utils/structures/Service";  import Service from "../utils/structures/Service";
 import { v4 as uuid } from "uuid";  
25  import path from "path";  import path from "path";
26    import { generate as randomstring } from 'randomstring';
27    
28  export type QueueCreateOptions = {  export type QueueCreateOptions = {
29      data?: { [key: string | number]: any };      data?: { [key: string | number]: any };
30      runAt?: Date;      runAt?: Date;
31      runAfter?: number;      runAfter?: number;
32        guild?: string;
33  };  };
34    
35  export default class QueueManager extends Service {  export default class QueueManager extends Service {
36      protected readonly queues: Collection<string, Queue> = new Collection();      public readonly queues: Collection<string, Queue> = new Collection();
37    
38      async loadQueues() {      async loadQueues() {
39          const models = await QueuedJob.find();          const models = await QueuedJob.find();
# Line 34  export default class QueueManager extend Line 54  export default class QueueManager extend
54          return this.queues.get(id)?.cancel();          return this.queues.get(id)?.cancel();
55      }      }
56    
57      async addQueue(queueClass: new (client: DiscordClient, queueOptions: QueueOptions) => Queue, { data, runAt, runAfter }: QueueCreateOptions) {      async addQueue(queueClass: new (client: DiscordClient, queueOptions: QueueOptions) => Queue, { data, runAt, runAfter, guild }: QueueCreateOptions) {
58          if (runAfter !== 0 && runAfter !== 0 && !runAfter && !runAt) {          if (runAfter !== 0 && runAfter !== 0 && !runAfter && !runAt) {
59              throw new Error("One of runAfter or runAt must be specified for creating a queue");              throw new Error("One of runAfter or runAt must be specified for creating a queue");
60          }          }
61    
62          const id = uuid();          const id = randomstring(7);
63          const model = await QueuedJob.create({ uuid: id, data, runOn: runAfter ? new Date(Date.now() + runAfter) : runAt!, createdAt: new Date(), className: queueClass.name });          const model = await QueuedJob.create({ uuid: id, data, runOn: runAfter ? new Date(Date.now() + runAfter) : runAt!, createdAt: new Date(), className: queueClass.name, guild });
64          const queue = new queueClass(this.client, { model, id, runAt, runAfter });          const queue = new queueClass(this.client, { model, id, runAt, runAfter });
65          this.setQueue(queue);          this.setQueue(queue);
66            return queue;
67      }      }
68    
69      runQueue(id: string) {      runQueue(id: string) {

Legend:
Removed from v.423  
changed lines
  Added in v.431

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26