Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporary session storage cleanup job #1100

Merged
merged 5 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions app/controllers/jobs.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
*/

const ExportService = require('../services/jobs/export/export.service.js')
const ProcessTimeLimitedLicencesService = require('../services/jobs/time-limited/process-time-limited-licences.service.js')
const ProcessLicenceUpdates = require('../services/jobs/licence-updates/process-licence-updates.js')
const ProcessSessionStorageCleanupService = require('../services/jobs/session-cleanup/process-session-storage-cleanup.service.js')
const ProcessTimeLimitedLicencesService = require('../services/jobs/time-limited/process-time-limited-licences.service.js')

/**
* Triggers export of all relevant tables to CSV and then uploads them to S3
Expand All @@ -20,20 +21,27 @@ async function exportDb (_request, h) {
return h.response().code(204)
}

async function timeLimited (_request, h) {
ProcessTimeLimitedLicencesService.go()
async function licenceUpdates (_request, h) {
ProcessLicenceUpdates.go()

return h.response().code(204)
}

async function licenceUpdates (_request, h) {
ProcessLicenceUpdates.go()
async function sessionCleanup (_request, h) {
ProcessSessionStorageCleanupService.go()

return h.response().code(204)
}

async function timeLimited (_request, h) {
ProcessTimeLimitedLicencesService.go()

return h.response().code(204)
}

module.exports = {
exportDb,
licenceUpdates,
sessionCleanup,
timeLimited
}
11 changes: 11 additions & 0 deletions app/routes/jobs.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ const routes = [
auth: false
}
},
{
method: 'POST',
path: '/jobs/session-cleanup',
handler: JobsController.sessionCleanup,
options: {
app: {
plainOutput: true
},
auth: false
}
},
{
method: 'POST',
path: '/jobs/time-limited',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

/**
* Deletes any temporary session records where the `created_at` date is more than 1 day ago
* @module ProcessSessionStorageCleanupService
*/

const { calculateAndLogTimeTaken, currentTimeInNanoseconds } = require('../../../lib/general.lib.js')
const SessionModel = require('../../../models/session.model.js')

/**
* Deletes any temporary session records where the `created_at` date is more than 1 day ago
*/
async function go () {
const startTime = currentTimeInNanoseconds()

const numberOfRowsDeleted = await _deleteSessionRecords()

calculateAndLogTimeTaken(startTime, 'Session storage cleanup job complete', { rowsDeleted: numberOfRowsDeleted })
}

async function _deleteSessionRecords () {
const maxAgeInDays = 1
const maxSessionAge = new Date(new Date().setDate(new Date().getDate() - maxAgeInDays)).toISOString()

const numberOfRowsDeleted = await SessionModel.query()
.delete()
.where('created_at', '<', maxSessionAge)

return numberOfRowsDeleted
}

module.exports = {
go
}
21 changes: 21 additions & 0 deletions test/controllers/jobs.controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { expect } = Code
// Things we need to stub
const ExportService = require('../../app/services/jobs/export/export.service.js')
const ProcessLicenceUpdatesService = require('../../app/services/jobs/licence-updates/process-licence-updates.js')
const ProcessSessionStorageCleanupService = require('../../app/services/jobs/session-cleanup/process-session-storage-cleanup.service.js')
const ProcessTimeLimitedLicencesService = require('../../app/services/jobs/time-limited/process-time-limited-licences.service.js')

// For running our service
Expand Down Expand Up @@ -76,6 +77,26 @@ describe('Jobs controller', () => {
})
})

describe('/jobs/session-cleanup', () => {
describe('POST', () => {
beforeEach(() => {
options = { method: 'POST', url: '/jobs/session-cleanup' }
})

describe('when the request succeeds', () => {
beforeEach(async () => {
Sinon.stub(ProcessSessionStorageCleanupService, 'go').resolves()
})

it('returns a 204 response', async () => {
const response = await server.inject(options)

expect(response.statusCode).to.equal(204)
})
})
})
})

describe('/jobs/time-limited', () => {
describe('POST', () => {
beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script()
const { expect } = Code

// Test helpers
const DatabaseSupport = require('../../../support/database.js')
const SessionHelper = require('../../../support/helpers/session.helper.js')
const SessionModel = require('../../../../app/models/session.model.js')

// Thing under test
const ProcessSessionStorageCleanupService = require('../../../../app/services/jobs/session-cleanup/process-session-storage-cleanup.service.js')

describe('Process Session Storage Cleanup service', () => {
const todayMinusOneDay = new Date(new Date().setDate(new Date().getDate() - 1)).toISOString()

let notifierStub

beforeEach(async () => {
await DatabaseSupport.clean()

// The service depends on GlobalNotifier to have been set. This happens in app/plugins/global-notifier.plugin.js
// when the app starts up and the plugin is registered. As we're not creating an instance of Hapi server in this
// test we recreate the condition by setting it directly with our own stub
notifierStub = { omg: Sinon.stub() }
global.GlobalNotifier = notifierStub
})

afterEach(() => {
Sinon.restore()
})

describe('when there is session data created more than 1 day ago', () => {
beforeEach(async () => {
await SessionHelper.add({ createdAt: todayMinusOneDay })
})

it('removes the session data created more than 1 day ago', async () => {
await ProcessSessionStorageCleanupService.go()

const results = await SessionModel.query()

expect(results).to.have.length(0)
})

it('logs the time taken in milliseconds and seconds and number of records deleted', async () => {
await ProcessSessionStorageCleanupService.go()

const logDataArg = notifierStub.omg.firstCall.args[1]

expect(notifierStub.omg.calledWith('Session storage cleanup job complete')).to.be.true()
expect(logDataArg.timeTakenMs).to.exist()
expect(logDataArg.timeTakenSs).to.exist()
expect(logDataArg.rowsDeleted).to.equal(1)
})
})

describe('when there is session data created less than 1 day ago (today)', () => {
beforeEach(async () => {
await SessionHelper.add()
})

it('does not remove the session data created less than 1 day ago', async () => {
await ProcessSessionStorageCleanupService.go()

const results = await SessionModel.query()

expect(results).to.have.length(1)
})

it('logs the time taken in milliseconds and seconds and number of records deleted', async () => {
await ProcessSessionStorageCleanupService.go()

const logDataArg = notifierStub.omg.firstCall.args[1]

expect(notifierStub.omg.calledWith('Session storage cleanup job complete')).to.be.true()
expect(logDataArg.timeTakenMs).to.exist()
expect(logDataArg.timeTakenSs).to.exist()
expect(logDataArg.rowsDeleted).to.equal(0)
})
})
})
Loading