-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(sessions): Add worker to cleanup older sessions. (#14319)
* Add worker to cleanup older sessions. * chore: charts update dirty files * Fix logging. * chore: nx format:write update dirty files * Error handling. * Remove env var. * chore: charts update dirty files * Rename files. * Use shared test setup utility. * No checking for rows to delete. * Use logger. * Update apps/services/sessions/src/app/cleanup/cleanup-worker.service.spec.ts Co-authored-by: Sævar Már Atlason <[email protected]> * Add comment explaining assertion. * chore: charts update dirty files --------- Co-authored-by: andes-it <[email protected]> Co-authored-by: Sævar Már Atlason <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
bb04cf6
commit 31c5144
Showing
11 changed files
with
398 additions
and
2 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
20 changes: 20 additions & 0 deletions
20
apps/services/sessions/src/app/cleanup/cleanup-worker.module.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,20 @@ | ||
import { Module } from '@nestjs/common' | ||
import { SequelizeModule } from '@nestjs/sequelize' | ||
|
||
import { LoggingModule } from '@island.is/logging' | ||
|
||
import { SequelizeConfigService } from '../../sequelizeConfig.service' | ||
import { Session } from '../sessions/session.model' | ||
import { SessionsCleanupService } from './cleanup-worker.service' | ||
|
||
@Module({ | ||
imports: [ | ||
LoggingModule, | ||
SequelizeModule.forRootAsync({ | ||
useClass: SequelizeConfigService, | ||
}), | ||
SequelizeModule.forFeature([Session]), | ||
], | ||
providers: [SessionsCleanupService], | ||
}) | ||
export class SessionsCleanupWorkerModule {} |
65 changes: 65 additions & 0 deletions
65
apps/services/sessions/src/app/cleanup/cleanup-worker.service.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,65 @@ | ||
import { createNationalId } from '@island.is/testing/fixtures' | ||
import { setupAppWithoutAuth, TestApp } from '@island.is/testing/nest' | ||
|
||
import { FixtureFactory } from '../../../test/fixture.factory' | ||
import { SequelizeConfigService } from '../../sequelizeConfig.service' | ||
import { Session } from '../sessions/session.model' | ||
import { SessionsCleanupWorkerModule } from './cleanup-worker.module' | ||
import { SessionsCleanupService } from './cleanup-worker.service' | ||
|
||
describe('SessionsService', () => { | ||
let app: TestApp | ||
let sessionsCleanupService: SessionsCleanupService | ||
let factory: FixtureFactory | ||
|
||
beforeAll(async () => { | ||
app = await setupAppWithoutAuth({ | ||
AppModule: SessionsCleanupWorkerModule, | ||
SequelizeConfigService, | ||
dbType: 'sqlite', | ||
}) | ||
factory = new FixtureFactory(app) | ||
|
||
sessionsCleanupService = app.get(SessionsCleanupService) | ||
}) | ||
|
||
beforeEach(async () => { | ||
await factory.get(Session).destroy({ | ||
where: {}, | ||
cascade: true, | ||
truncate: true, | ||
force: true, | ||
}) | ||
}) | ||
|
||
afterAll(async () => { | ||
await app?.cleanUp() | ||
}) | ||
|
||
it('should remove old enough session records', async () => { | ||
// Arrange | ||
|
||
// Create sessions that should be deleted | ||
await factory.createDateSessions( | ||
createNationalId('person'), | ||
new Date('2023-01-01'), | ||
new Date('2023-02-01'), | ||
) | ||
|
||
// Create sessions that should remain | ||
await factory.createDateSessions( | ||
createNationalId('person'), | ||
new Date('3023-01-01'), | ||
new Date('3023-02-01'), | ||
) | ||
|
||
// Act | ||
await sessionsCleanupService.run() | ||
|
||
// Assert | ||
const sessions = await factory.get(Session).findAll() | ||
expect(sessions).toHaveLength(5) | ||
// Check that all remaining sessions are newer than the cutoff date | ||
expect(sessions.every((s) => s.timestamp > new Date('2023-02-01'))) | ||
}) | ||
}) |
50 changes: 50 additions & 0 deletions
50
apps/services/sessions/src/app/cleanup/cleanup-worker.service.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,50 @@ | ||
import { Inject } from '@nestjs/common' | ||
import { InjectModel } from '@nestjs/sequelize' | ||
import subMonths from 'date-fns/subMonths' | ||
import { Op } from 'sequelize' | ||
|
||
import { LOGGER_PROVIDER } from '@island.is/logging' | ||
|
||
import { Session } from '../sessions/session.model' | ||
|
||
import type { Logger } from '@island.is/logging' | ||
|
||
const RETENTION_IN_MONTHS = 12 | ||
export class SessionsCleanupService { | ||
constructor( | ||
@Inject(LOGGER_PROVIDER) | ||
private readonly logger: Logger, | ||
@InjectModel(Session) | ||
private readonly sessionModel: typeof Session, | ||
) {} | ||
|
||
public async run() { | ||
const timer = this.logger.startTimer() | ||
this.logger.info('Worker starting...') | ||
|
||
await this.deleteOlderSessions() | ||
|
||
this.logger.info('Worker finished.') | ||
timer.done() | ||
} | ||
|
||
private async deleteOlderSessions() { | ||
this.logger.info(`Deleting...`) | ||
|
||
const filter = { | ||
where: { | ||
timestamp: { | ||
[Op.lt]: subMonths(new Date(), RETENTION_IN_MONTHS), | ||
}, | ||
}, | ||
} | ||
|
||
const affectedRows: number = await this.sessionModel.destroy(filter) | ||
|
||
if (affectedRows > 0) { | ||
this.logger.info(`Finished deleting ${affectedRows} rows.`) | ||
} else { | ||
this.logger.info(`No rows found to delete.`) | ||
} | ||
} | ||
} |
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,23 @@ | ||
import { NestFactory } from '@nestjs/core' | ||
|
||
import { logger } from '@island.is/logging' | ||
|
||
import { SessionsCleanupWorkerModule } from './cleanup-worker.module' | ||
import { SessionsCleanupService } from './cleanup-worker.service' | ||
|
||
export const worker = async () => { | ||
try { | ||
logger.info('Sessions cleanup worker started.') | ||
const app = await NestFactory.createApplicationContext( | ||
SessionsCleanupWorkerModule, | ||
) | ||
app.enableShutdownHooks() | ||
await app.get(SessionsCleanupService).run() | ||
await app.close() | ||
logger.info('Sessions cleanup worker finished successfully.') | ||
process.exit(0) | ||
} catch (error) { | ||
logger.error('Sessions cleanup worker encountered an error:', error) | ||
process.exit(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
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.