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

LEAN-3814: delete migration backups that are older than 30 days at the start of every month #157

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
wip
mbetamony committed Oct 8, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit d4a68dbb26cf1f997c5e0e6211e99cdfabc9d124
11 changes: 11 additions & 0 deletions src/DataAccess/DocumentExtender.ts
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@ export class DocumentExtender {
updateDocument: this.updateDocument,
deleteDocument: this.deleteDocument,
findHistory: this.findHistory,
deleteBackupsOlderThan: this.deleteBackupsOlderThan,
}
}

@@ -158,4 +159,14 @@ export class DocumentExtender {
throw error
}
}

private deleteBackupsOlderThan = async (lt: Date) => {
await this.prisma.migrationBackup.deleteMany({
where: {
createdAt: {
lt,
},
},
})
}
}
6 changes: 6 additions & 0 deletions src/DomainServices/DocumentService.ts
Original file line number Diff line number Diff line change
@@ -89,6 +89,12 @@ export class DocumentService {
return manuscript
}

public async deleteOldBackups(date: Date) {
const dateToCompare = new Date(date)
dateToCompare.setDate(date.getDate() - 30)
await DIContainer.sharedContainer.documentClient.deleteBackupsOlderThan(dateToCompare)
}

public async handleUpgrade(
wss: WebSocketServer,
request: IncomingMessage,
12 changes: 12 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -17,6 +17,8 @@
import { defineGlobals } from './define-globals'
defineGlobals()

import { schedule } from 'node-cron'

import { config } from './Config/Config'
import { ServerStatus } from './Controller/V2/ServerStatus/ServerStatus'
import { DIContainer } from './DIContainer/DIContainer'
@@ -38,6 +40,16 @@ function main() {
.then(() => {
log.info(`Manuscripts.io ${ServerStatus.version} started 🚀`)
})

// eslint-disable-next-line promise/always-return
.then(() => {
schedule('* * 1 * *', async (date) => {
if (date instanceof Date) {
log.info('Deleting 30 day old backups')
await DIContainer.sharedContainer.documentService.deleteOldBackups(date)
}
})
})
.catch((error) => {
log.error('An error occurred while bootstrapping app:', error)
process.exit(-1)