mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
fix: expiryCheck error handling
This commit is contained in:
1 parent
cd03277fd4
commit
3cebcddc91
3 files changed
+31
-18
No files matched your search
@@ -35,10 +35,12 @@ const Session = model(
|
|||||||
user: {
|
user: {
|
||||||
type: Schema.ObjectId,
|
type: Schema.ObjectId,
|
||||||
ref: 'User',
|
ref: 'User',
|
||||||
|
required: true,
|
||||||
},
|
},
|
||||||
quest: {
|
quest: {
|
||||||
type: String,
|
type: String,
|
||||||
ref: 'Quest',
|
ref: 'Quest',
|
||||||
|
required: true,
|
||||||
},
|
},
|
||||||
containerId: String,
|
containerId: String,
|
||||||
status: {
|
status: {
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ export async function deleteContainer(id) {
|
|||||||
try {
|
try {
|
||||||
await container.stop()
|
await container.stop()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error)
|
console.warn('Failed to stop container', id)
|
||||||
}
|
}
|
||||||
await container.remove()
|
await container.remove()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,28 @@ import { Session } from '@linux-odyssey/models'
|
|||||||
import config from '../config.js'
|
import config from '../config.js'
|
||||||
import { deleteContainer } from './docker.js'
|
import { deleteContainer } from './docker.js'
|
||||||
|
|
||||||
|
async function disableSession(session) {
|
||||||
|
try {
|
||||||
|
await deleteContainer(session.containerId)
|
||||||
|
} catch (err) {
|
||||||
|
// If the container doesn't exist, we can ignore the error
|
||||||
|
if (err.statusCode !== 404) {
|
||||||
|
throw new Error(
|
||||||
|
`Failed to delete container ${session.containerId} for session ${session._id}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (session.status === 'active') {
|
||||||
|
session.status = 'inactive'
|
||||||
|
}
|
||||||
|
session.containerId = null
|
||||||
|
try {
|
||||||
|
await session.save()
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(`Failed to save session ${session._id}\n${err}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function removeExpired() {
|
export async function removeExpired() {
|
||||||
// Find all sessions that are containerId is not null and lastActivityAt is
|
// Find all sessions that are containerId is not null and lastActivityAt is
|
||||||
// less than the current time minus the expiry time
|
// less than the current time minus the expiry time
|
||||||
@@ -13,25 +35,14 @@ export async function removeExpired() {
|
|||||||
lastActivityAt: { $lt: new Date(Date.now() - config.containerExpiry) },
|
lastActivityAt: { $lt: new Date(Date.now() - config.containerExpiry) },
|
||||||
})
|
})
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
sessions.map(async (session) => {
|
sessions.map((session) =>
|
||||||
console.log('Expiring session: ', session._id, session.containerId)
|
disableSession(session).catch((err) => {
|
||||||
try {
|
throw new Error(`Failed to expire session: ${session._id}\n${err}`)
|
||||||
await deleteContainer(session.containerId)
|
})
|
||||||
} catch (err) {
|
)
|
||||||
if (err.statusCode !== 404) {
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (session.status === 'active') {
|
|
||||||
session.status = 'inactive'
|
|
||||||
}
|
|
||||||
session.containerId = null
|
|
||||||
await session.save()
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
console.log('Done checking for expired sessions')
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error('Remove expiring failed:', err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user