Skip to content

Commit

Permalink
Handle updater restart
Browse files Browse the repository at this point in the history
  • Loading branch information
Keskimaki committed Dec 21, 2023
1 parent 73ebb9a commit e21f19d
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions src/updater/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const {
} = require('./updateCoursesAndTeacherFeedbackTargets')
const {
updateStudentFeedbackTargets,
updateNewEnrolments,
} = require('./updateStudentFeedbackTargets')
const { updateFeedbackTargetCounts } = require('./updateFeedbackTargetCounts')
const { synchronizeInterimFeedbacks } = require('./synchronizeInterimFeedbacks')
Expand All @@ -26,29 +25,6 @@ const runUpdater = async () => {
await updateStudentFeedbackTargets()
await updateFeedbackTargetCounts()
await synchronizeInterimFeedbacks()
await updateNewEnrolments()
}

const checkStatusOnStartup = async () => {
const statuses = await UpdaterStatus.findAll({
where: {
status: 'RUNNING',
},
})

if (!inProduction && statuses.length === 1) {
logger.info(`Server had a restart while updater was running, continuing ${statuses[0].jobType}`)
await runUpdater()
} else {
for (const status of statuses) {
status.status = 'INTERRUPTED'
status.finishedAt = new Date()
await status.save()
const msg = `Server had a restart while updater was running, interrupting ${status.jobType}`
Sentry.captureMessage(msg)
logger.error(`[UPDATER] ${msg}`)
}
}
}

const run = async () => {
Expand Down Expand Up @@ -78,6 +54,27 @@ const run = async () => {
return logger.info('[UPDATER] Finished updating')
}

const continueRun = async (status) => {
logger.info('[UPDATER] Continuing interrupted updater run')

try {
await runUpdater()
} catch (error) {
Sentry.captureException(error)
Sentry.captureMessage('Updater run failed!')
status.status = 'FAILURE'
status.finishedAt = new Date()
await status.save()
return logger.error('[UPDATER] finished with error', error)
}

status.status = 'FINISHED'
status.finishedAt = new Date()
await status.save()

return logger.info('[UPDATER] Finished updating')
}

const start = async () => {
if (!(inProduction || inStaging)) {
logger.info('Starting development updater run')
Expand All @@ -92,6 +89,29 @@ const start = async () => {
logger.info('Running updater according to cron', { cron: cronTime })
}

const checkStatusOnStartup = async () => {
const statuses = await UpdaterStatus.findAll({
where: {
status: 'RUNNING',
},
})

if (inProduction && statuses.length === 1) {
const status = statuses[0]
logger.info(`Server had a restart while updater was running, continuing ${status.jobType}`)
await continueRun(status)
} else {
for (const status of statuses) {
status.status = 'INTERRUPTED'
status.finishedAt = new Date()
await status.save()
const msg = `Server had a restart while updater was running, interrupting ${status.jobType}`
Sentry.captureMessage(msg)
logger.error(`[UPDATER] ${msg}`)
}
}
}

const updater = {
start,
run,
Expand Down

0 comments on commit e21f19d

Please sign in to comment.