1
0
Fork 0

prepare to schedule multiple jobs

This commit is contained in:
Sean Sube 2022-12-29 20:57:52 -06:00
parent d97bd8fb61
commit 35f1fcdb32
4 changed files with 23 additions and 6 deletions

View File

@ -97,6 +97,7 @@ async function messageFromDiscord(client: Client, message: DiscordMessage): Prom
async function getChannel(client: Client, name: string): Promise<Channel> {
const channel = await client.channels.fetch(name);
if (doesExist(channel)) {
// TODO: find a nice way of allowing more channel types
if (channel.type === ChannelType.GuildText || channel.type === ChannelType.DM) {
return channelFromDiscord(client, channel);
} else {

View File

@ -27,6 +27,7 @@ export interface BaseChannel {
export interface TextChannel extends BaseChannel {
server: string;
topic: string;
type: 'text';
edit(options: {
name?: string;
@ -34,8 +35,9 @@ export interface TextChannel extends BaseChannel {
}): Promise<Channel>;
}
// anything unique?
export type DirectChannel = BaseChannel;
export interface DirectChannel extends BaseChannel {
type: 'dm';
}
export type Channel = DirectChannel | TextChannel;

View File

@ -20,6 +20,10 @@ export interface JobContext {
rcon: RconClient;
}
export const JOBS: Array<[string, (context: JobContext) => Promise<void>]> = [
['*/15 * * * *', playerCountJob],
];
export async function playerCountJob(context: JobContext): Promise<void> {
const { args, bot, logger, rcon } = context;
logger.info('player count job');
@ -42,9 +46,13 @@ export async function playerCountJob(context: JobContext): Promise<void> {
export async function startCrons(context: JobContext): Promise<Job> {
const { logger } = context;
const job = scheduleJob('test', '*/1 * * * *', () => playerCountJob(context));
logger.debug({ next: job.nextInvocation() }, 'next cron');
for (const [schedule, fn] of JOBS) {
const job = scheduleJob(fn.name, schedule, () => fn(context));
logger.debug({
job: fn.name,
next: job.nextInvocation(),
}, 'scheduled cron job');
}
return {
destroy() {

View File

@ -69,7 +69,13 @@ export async function main(argv: Array<string>): Promise<ExitCode> {
}
logger.debug('starting cron jobs');
const cron = await startCrons(args, logger, bots[0], rcon);
const cron = await startCrons({
args,
bot: bots[0],
data,
logger,
rcon,
});
await signal(SIGNAL_RESET);