Skip to content

Commit

Permalink
fix(cronNoticeCrawling): 크론 동작 시 에러 발생하면 3번까지 동작한 후 종료되도록 설정
Browse files Browse the repository at this point in the history
만약 도중에 탈출하지 않으면 무한 루프로 인해 서버에 안좋은 영향 또는 무한 슬랙 알림을 받기에 3회까지 시도 후 종료되도록 설정
추가적으로 크론 내부에서 트랜잭션을 생성하지 않도록 변경
  • Loading branch information
pp449 committed Feb 4, 2024
1 parent 0941ac8 commit a4685c2
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/hooks/cronNoticeCrawling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,34 @@ const pushToUsers = async (pushNotiToUserLists: PushNoti) => {
if (pushedUserCount.length !== 0) notificationToSlack(pushedUserCount);
};

const cronNoticeCrawling = async () => {
const connection = await db.getConnection();
await connection.beginTransaction();
const cronNoticeCrawling = async (reTryCount = 0) => {
try {
const pushNotiToUserLists = await saveMajorNoticeToDB(connection);
const pushNotiToUserLists = await saveMajorNoticeToDB();
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth() + 1; // 월은 0부터 시작하므로 1을 더해줍니다.
const day = today.getDate();
notificationToSlack(`${year}-${month}-${day} 크롤링 완료`);
await connection.commit();
pushToUsers(pushNotiToUserLists);
} catch (error) {
await connection.rollback();
notificationToSlack(error.message);
cronNoticeCrawling();
} finally {
connection.release();
if (reTryCount >= 2) {
notificationToSlack(error.message);
return;
}
cronNoticeCrawling(reTryCount + 1);
}
};

const cronExtracurricularCrawling = async () => {
const cronExtracurricularCrawling = async (reTryCount = 0) => {
try {
await saveSchoolNoticeToDB();
await saveLanguageNoticeToDB();
await saveWhalebeToDB();
} catch (error) {
notificationToSlack(error.message);
if (reTryCount >= 2) {
notificationToSlack(error.message);
return;
}
cronExtracurricularCrawling();
}
};
Expand Down

0 comments on commit a4685c2

Please sign in to comment.