1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-03 09:49:20 +02:00

Optimize updating token activity

This commit is contained in:
Chocobozzz 2025-09-05 10:34:47 +02:00
parent 448bc823ef
commit 12c9825658
No known key found for this signature in database
GPG key ID: 583A612D890159BE
4 changed files with 34 additions and 15 deletions

View file

@ -1,28 +1,45 @@
import { MOAuthToken } from '@server/types/models/index.js'
import { OAuthTokenModel } from '@server/models/oauth/oauth-token.js'
import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants.js'
import { AbstractScheduler } from './abstract-scheduler.js'
type UpdatePayload = {
id: number
lastActivityDate: Date
lastActivityIP: string
lastActivityDevice: string
}
export class UpdateTokenSessionScheduler extends AbstractScheduler {
private static instance: UpdateTokenSessionScheduler
protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.UPDATE_TOKEN_SESSION
private readonly toUpdate = new Set<MOAuthToken>()
private readonly toUpdate = new Set<UpdatePayload>()
private constructor () {
super()
}
addToUpdate (token: MOAuthToken) {
this.toUpdate.add(token)
addToUpdate (payload: UpdatePayload) {
this.toUpdate.add(payload)
}
protected async internalExecute () {
const toUpdate = Array.from(this.toUpdate)
this.toUpdate.clear()
for (const token of toUpdate) {
await token.save()
for (const payload of toUpdate) {
await OAuthTokenModel.update({
lastActivityDate: payload.lastActivityDate,
lastActivityIP: payload.lastActivityIP,
lastActivityDevice: payload.lastActivityDevice
}, {
where: {
id: payload.id
},
// Prevent tokens cache invalidation, we don't update fields that are meaningful for this cache
hooks: false
})
}
}