-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from BingusBoingus-Developer-Team/refactor-cr…
…onScheduler Refactor Cron scheduler
- Loading branch information
Showing
9 changed files
with
104 additions
and
6,579 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Client, TextChannel } from 'discord.js'; | ||
import BirthdayShoutoutTask from './tasks/birthday-shoutout.task'; | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import WakeUpTask from './tasks/wake-up.task'; | ||
import * as cron from 'node-cron'; | ||
import { BirthdayEntryService } from '../models/birthday/service/birthday-entry.service'; | ||
import { TaskEntry } from './interfaces/task-entry.interface'; | ||
import { DiscordService } from '../discord/discord.service'; | ||
|
||
@Injectable() | ||
export class CronService { | ||
private static instance: CronService; | ||
private tasks: TaskEntry[]; | ||
|
||
constructor( | ||
@Inject(BirthdayEntryService) | ||
private readonly birthdayService: BirthdayEntryService, | ||
@Inject(DiscordService) | ||
private readonly discordService: DiscordService, | ||
) { | ||
if (CronService.instance) { | ||
throw new Error(`ERROR: An instance has already been created.`); | ||
} | ||
|
||
CronService.instance = this; | ||
} | ||
|
||
static getInstance(): CronService { | ||
return CronService.instance; | ||
} | ||
|
||
public init() { | ||
this.tasks = [ | ||
{ | ||
name: 'birthday-shoutout', | ||
schedule: '0 10 * * *', | ||
task: new BirthdayShoutoutTask( | ||
this.discordService.client.channels.cache.find( | ||
(channel) => channel.id === '447554141724737548', | ||
) as TextChannel, | ||
this.birthdayService, | ||
), | ||
}, | ||
{ | ||
name: 'first-of-the-month', | ||
schedule: '0 12 1 * *', | ||
task: new WakeUpTask( | ||
this.discordService.client.channels.cache.find( | ||
(channel) => channel.id === '447554141724737548', | ||
) as TextChannel, | ||
), | ||
}, | ||
]; | ||
this.registerTasks(); | ||
} | ||
|
||
registerTasks(): void { | ||
this.tasks.forEach((task) => { | ||
if (!cron.validate(task.schedule)) { | ||
throw new Error( | ||
`ERROR: Invalid cron schedule expression for task '${task.name}'`, | ||
); | ||
} | ||
|
||
cron | ||
.schedule(task.schedule, async () => { | ||
await task.task.execute(); | ||
}) | ||
.start(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ITask } from '../tasks/interfaces/task.interface'; | ||
|
||
export interface TaskEntry { | ||
name: string; | ||
schedule: string; | ||
task: ITask; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { BirthdayEntryModule } from "../models/birthday/module/birthday-entry.module"; | ||
|
||
import { Module } from '@nestjs/common'; | ||
import { BirthdayEntryModule } from '../models/birthday/module/birthday-entry.module'; | ||
import { CronService } from './cron.service'; | ||
import { DiscordModule } from '../discord/discord.module'; | ||
|
||
@Module({ | ||
imports: [ | ||
BirthdayEntryModule, | ||
], | ||
providers: [], | ||
exports: [], | ||
imports: [DiscordModule, BirthdayEntryModule], | ||
providers: [CronService], | ||
exports: [CronService], | ||
}) | ||
export class TaskModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,19 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Client, GatewayIntentBits } from 'discord.js'; | ||
import { AppConfigService } from '../../config/config.service'; | ||
import { CronScheduler } from '../cron-tasks/cron-scheduler'; | ||
import { BirthdayEntryService } from '../models/birthday/service/birthday-entry.service'; | ||
|
||
@Injectable() | ||
export class DiscordService { | ||
public readonly client: Client<boolean>; | ||
public cronScheduler: CronScheduler; | ||
|
||
constructor(configService: AppConfigService, private readonly birthdayService: BirthdayEntryService) { | ||
constructor(configService: AppConfigService) { | ||
this.client = new Client({ | ||
intents: [ | ||
GatewayIntentBits.Guilds, | ||
GatewayIntentBits.GuildMessages, | ||
GatewayIntentBits.MessageContent, | ||
], | ||
}); | ||
|
||
this.client.login(configService.botToken); | ||
this.client.on('ready', () => { | ||
this.cronScheduler = new CronScheduler(this.client, birthdayService); | ||
this.cronScheduler.registerTasks(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ClientEvents, Events } from 'discord.js'; | ||
import { AEvent } from '../event.abstract'; | ||
import { CronService } from '../../cron-tasks/cron.service'; | ||
|
||
@Injectable() | ||
export class ClientReady extends AEvent { | ||
constructor(private readonly cronService: CronService) { | ||
super(); | ||
} | ||
|
||
event: keyof ClientEvents = Events.ClientReady; | ||
once: boolean = true; | ||
|
||
async execute(args: ClientEvents[Events.ClientReady]) { | ||
console.log('Successfully connected to Discord'); | ||
console.log(`logged in as ${args[0].user.username}`); | ||
|
||
this.cronService.init(); | ||
} | ||
} |