-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: env vars to configure user error logging (#557)
- Loading branch information
Showing
13 changed files
with
253 additions
and
41 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
83 changes: 83 additions & 0 deletions
83
backend/src/schedulers/delete-user-errors-scheduler.spec.ts
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import waitFor from 'wait-for-expect'; | ||
import deleteUserErrorsJob from './delete-user-errors-scheduler'; | ||
|
||
jest.mock('../v1/services/utils-service', () => ({ | ||
utils: { | ||
delay: jest.fn(), | ||
}, | ||
})); | ||
|
||
const mockDeleteErrorsOlderThan = jest.fn(); | ||
jest.mock('../v1/services/error-service', () => ({ | ||
errorService: { | ||
deleteErrorsOlderThan: (...args) => mockDeleteErrorsOlderThan(...args), | ||
}, | ||
})); | ||
|
||
const mock_asyncRetry = jest.fn((fn) => fn()); | ||
jest.mock('async-retry', () => ({ | ||
__esModule: true, | ||
default: async (fn) => mock_asyncRetry(fn), | ||
})); | ||
|
||
const mock_generateHtmlEmail = jest.fn(); | ||
const mock_sendEmailWithRetry = jest.fn(); | ||
|
||
jest.mock('../external/services/ches', () => ({ | ||
__esModule: true, | ||
default: { | ||
generateHtmlEmail: (...args) => mock_generateHtmlEmail(...args), | ||
sendEmailWithRetry: (...args) => mock_sendEmailWithRetry(...args), | ||
}, | ||
})); | ||
|
||
jest.mock('cron', () => ({ | ||
CronJob: class MockCron { | ||
constructor( | ||
public expression: string, | ||
public cb: Function, | ||
) {} | ||
async start() { | ||
return this.cb(); | ||
} | ||
}, | ||
})); | ||
|
||
jest.mock('../config', () => ({ | ||
config: { | ||
get: (key: string) => { | ||
const settings = { | ||
'server:userErrorLogging:deleteScheduleCronTime': '121212121', | ||
'server:userErrorLogging:numMonthsOfUserErrorsToKeep': 6, | ||
'server:schedulerTimeZone': 'Canada/Vancouver', | ||
'ches:enabled': true, | ||
'ches:emailRecipients': '[email protected]', | ||
}; | ||
|
||
return settings[key]; | ||
}, | ||
}, | ||
})); | ||
|
||
const mock_tryLock = jest.fn(); | ||
const mock_unlock = jest.fn(); | ||
jest.mock('advisory-lock', () => ({ | ||
...jest.requireActual('advisory-lock'), | ||
default: () => { | ||
return () => ({ | ||
tryLock: () => mock_tryLock(), | ||
}); | ||
}, | ||
})); | ||
|
||
describe('delete-draft-service-scheduler', () => { | ||
it('should delete draft reports', async () => { | ||
mock_tryLock.mockReturnValue(mock_unlock); | ||
deleteUserErrorsJob.start(); | ||
await waitFor(async () => { | ||
expect(mock_tryLock).toHaveBeenCalledTimes(1); | ||
expect(mockDeleteErrorsOlderThan).toHaveBeenCalled(); | ||
expect(mock_unlock).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { DateTimeFormatter, LocalDate, ZoneId } from '@js-joda/core'; | ||
import advisoryLock from 'advisory-lock'; | ||
import { config } from '../config'; | ||
import { logger as log } from '../logger'; | ||
import { errorService } from '../v1/services/error-service'; | ||
import { createJob } from './create-job'; | ||
|
||
const SCHEDULER_NAME = 'DeleteUserErrors'; | ||
const mutex = advisoryLock(config.get('server:databaseUrl'))( | ||
`${SCHEDULER_NAME}-lock`, | ||
); | ||
const crontime = config.get('server:userErrorLogging:deleteScheduleCronTime'); | ||
const numMonthsOfErrorsToKeep = config.get( | ||
'server:userErrorLogging:numMonthsOfUserErrorsToKeep', | ||
); | ||
|
||
export default createJob( | ||
crontime, | ||
async () => { | ||
log.info(`Starting scheduled job '${SCHEDULER_NAME}'.`); | ||
const thresholdDate = LocalDate.now(ZoneId.UTC) | ||
.atStartOfDay(ZoneId.UTC) | ||
.minusMonths(numMonthsOfErrorsToKeep) | ||
.format(DateTimeFormatter.ISO_DATE_TIME); | ||
await errorService.deleteErrorsOlderThan(thresholdDate); | ||
log.info(`Completed scheduled job '${SCHEDULER_NAME}'.`); | ||
}, | ||
mutex, | ||
{ | ||
title: `Error in ${SCHEDULER_NAME}`, | ||
message: `Error in scheduled job: ${SCHEDULER_NAME}`, | ||
}, | ||
); |
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
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
Oops, something went wrong.