Skip to content

Commit

Permalink
Update: Improved scheduler (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbackas committed Nov 20, 2023
1 parent 1c97930 commit 168dffb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 38 deletions.
42 changes: 16 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ export class App {
void setRandomShowActivity(user)
})

schedule.scheduleJob('lifecycle:10min:announceEpisodes', '5-55/10 * * * *', async () => {
void scheduleAiringMessages(this)
})

schedule.scheduleJob('lifecycle:4hours:fetchEpisoded', '0 */4 * * *', async () => {
setTVDBLoadingActivity(user)
await pruneUnsubscribedShows()
await checkForAiringEpisodes()
await scheduleAiringMessages(this)
void setRandomShowActivity(user)
})

Expand Down
28 changes: 17 additions & 11 deletions src/lib/episodeNotifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,34 @@ export interface NotificationPayload {
episodeNumbers: number[]
}

type PayloadCollection = Collection<string, NotificationPayload>

/**
* look through episodes in the db and schedule notifications to the defined destinations
* @param app instance of the main app
*/
export async function scheduleAiringMessages (app: App): Promise<void> {
const nowUtc = moment.utc()
const tenMinutesFromNow = nowUtc.add(10, 'minutes')

const showsWithEpisodes = await client.show.findMany({
where: {
episodes: {
some: {
messageSent: false
messageSent: false,
airDate: {
lte: tenMinutesFromNow.toDate()
}
}
}
}
})

// convert the shows into a collection of notification payloads
const payloadCollection = showsWithEpisodes.reduce(reduceEpisodes, new Collection<string, NotificationPayload>())
const payloadCollection: PayloadCollection = showsWithEpisodes.reduce(reduceEpisodes, new Collection<string, NotificationPayload>())

// grab the discord client and global destinations for the schedule jobs to use
const discord = app.getClient()
const settingsManager = app.getSettingsManager()
// const globalDestinations = app.getSettings()?.allEpisodes ?? []

for (const payload of payloadCollection.values()) {
await scheduleJob(payload, discord, settingsManager)
Expand All @@ -55,20 +61,20 @@ export async function scheduleAiringMessages (app: App): Promise<void> {
* @param show current show to process
* @returns collection of notification payloads
*/
export function reduceEpisodes (acc: Collection<string, NotificationPayload>, show: Show): Collection<string, NotificationPayload> {
function reduceEpisodes (
acc: Collection<string,
NotificationPayload>,
show: Show
): Collection<string, NotificationPayload> {
const momentUTC = moment.utc(new Date())

for (const e of show.episodes) {
const airDate = moment.utc(e.airDate)
const inTimeWindow = airDate.isSameOrAfter(momentUTC) && airDate.isSameOrBefore(momentUTC.clone().add('1', 'day'))
const inTimeWindow = airDate.isSameOrAfter(momentUTC) && airDate.isSameOrBefore(momentUTC.clone().add(10, 'minutes'))

if (!inTimeWindow) continue

const airDateString = moment.utc(e.airDate)
.tz(process.env.TZ ?? 'America/Chicago')
.format('YYYY-MM-DD@HH:mm')

const key = `announceEpisodes:${airDateString}:${show.imdbId}:S${addLeadingZeros(e.season, 2)}`
const key = `announceEpisodes:${show.imdbId}:S${addLeadingZeros(e.season, 2)}`

// define the default payload to use if one doesn't exist in the collection
const defaultPayload: NotificationPayload = {
Expand Down

0 comments on commit 168dffb

Please sign in to comment.