-
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.
- Loading branch information
Showing
4 changed files
with
112 additions
and
31 deletions.
There are no files selected for viewing
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
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,41 +1,94 @@ | ||
import { Job, scheduleJob } from "node-schedule"; | ||
import { INotice } from "../models/types.js"; | ||
import { IEvent, INotice } from "../models/types.js"; | ||
import { cachingAllNotices } from "./caching.js"; | ||
import { redisClient } from "./connect.js"; | ||
import Event from "../models/events.js"; | ||
|
||
const jobArray: Job[] = []; | ||
export const setNoticeSchedule = (row: INotice) => { | ||
const { id, date } = row; | ||
const job = scheduleJob(`${id}`, getNextDay(date), async function () { | ||
console.log("run schedule:", id, date.toLocaleString(), "to 일반"); | ||
const key = `notice:${id}`; | ||
const job = scheduleJob(key, getNextDay(date), async function () { | ||
console.log("run schedule:", key, date.toLocaleString(), "to 일반"); | ||
const updatedRow = await row.update({ ...row, priority: "일반" }); | ||
await redisClient.set(`notice:${id}`, JSON.stringify(updatedRow)); | ||
await redisClient.set(key, JSON.stringify(updatedRow)); | ||
// 전체 긴급 공지 목록 캐싱 | ||
await cachingAllNotices(); | ||
jobArray.splice(existJobIndex, 1); | ||
}); | ||
console.log({jobArray}) | ||
const existJobIndex = jobArray.findIndex(j => j?.name == `${id}`); | ||
const existJobIndex = jobArray.findIndex((j) => j?.name == key); | ||
if (existJobIndex > -1) { | ||
jobArray[existJobIndex].cancel(); | ||
jobArray[existJobIndex] = job; | ||
} else { | ||
jobArray.push(job); | ||
} | ||
console.log('set schedule:', `${id}-${date.toLocaleString()}`); | ||
console.log('job list:', jobArray.map(j => j?.name)); | ||
console.log("set schedule:", `${key}-${date.toLocaleString()}`); | ||
console.log( | ||
"job list:", | ||
jobArray.map((j) => j?.name) | ||
); | ||
}; | ||
export const delNoticeSchedule = (row: INotice) => { | ||
const {id, date} = row; | ||
const existJobIndex = jobArray.findIndex(j => j.name == `${id}`); | ||
const { id, date } = row; | ||
const key = `notice:${id}`; | ||
const existJobIndex = jobArray.findIndex((j) => j.name == `${key}`); | ||
if (existJobIndex > -1) { | ||
jobArray[existJobIndex].cancel(); | ||
jobArray.splice(existJobIndex, 1); | ||
console.log('del schedule:', `${id}-${date.toLocaleString()}`); | ||
console.log('job list:', jobArray.map(j => j?.name)); | ||
console.log("del schedule:", `${key}-${date.toLocaleString()}`); | ||
console.log( | ||
"job list:", | ||
jobArray.map((j) => j?.name) | ||
); | ||
} else { | ||
console.warn(`jobArray hasn't schedule-${id}`); | ||
console.warn(`jobArray hasn't schedule-${key}`); | ||
} | ||
} | ||
|
||
export const getNextDay = (date: Date) => date.setDate(date.getDate() + 1); | ||
}; | ||
export const setEventSchedule = (row: IEvent) => { | ||
const { id, end } = row; | ||
const key = `event:${id}`; | ||
const job = scheduleJob(key, end, async function () { | ||
console.log("run schedule:", key, end.toLocaleString(), "to 행사 종료"); | ||
const updatedRow = await row.update({ ...row, expired: true }); | ||
await redisClient.set(key, JSON.stringify(updatedRow)); | ||
// 전체 행사 목록 캐싱 | ||
const allEventsFromDb = await Event.findAll({ | ||
where: { | ||
expired: false, // 진행중인 행사만 가져오기 | ||
}, | ||
order: [["start", "ASC"]], | ||
}); | ||
await redisClient.set("allEvents", JSON.stringify(allEventsFromDb)); | ||
jobArray.splice(existJobIndex, 1); | ||
}); | ||
const existJobIndex = jobArray.findIndex((j) => j?.name == key); | ||
if (existJobIndex > -1) { | ||
jobArray[existJobIndex].cancel(); | ||
jobArray[existJobIndex] = job; | ||
} else { | ||
jobArray.push(job); | ||
} | ||
console.log("set schedule:", `${key}-${end.toLocaleString()}`); | ||
console.log( | ||
"job list:", | ||
jobArray.map((j) => j?.name) | ||
); | ||
}; | ||
export const delEventSchedule = (row: IEvent) => { | ||
const { id, end } = row; | ||
const key = `event:${id}`; | ||
const existJobIndex = jobArray.findIndex((j) => j.name == `${key}`); | ||
if (existJobIndex > -1) { | ||
jobArray[existJobIndex].cancel(); | ||
jobArray.splice(existJobIndex, 1); | ||
console.log("del schedule:", `${key}-${end.toLocaleString()}`); | ||
console.log( | ||
"job list:", | ||
jobArray.map((j) => j?.name) | ||
); | ||
} else { | ||
console.warn(`jobArray hasn't schedule-${key}`); | ||
} | ||
}; | ||
export const getNextDay = (date: Date) => date.setDate(date.getDate() + 1); |