mirror of
https://github.com/darkwire/darkwire.io.git
synced 2025-07-18 18:54:52 +00:00
Poll for and delete inactive rooms #77
This commit is contained in:
parent
a70efc5674
commit
e4c4176cf1
21
server/src/inactive_rooms.js
Normal file
21
server/src/inactive_rooms.js
Normal file
@ -0,0 +1,21 @@
|
||||
import { getRedis } from './index'
|
||||
|
||||
export async function pollForInactiveRooms() {
|
||||
const redis = getRedis();
|
||||
|
||||
console.log('Checking for inactive rooms...');
|
||||
const rooms = await redis.hgetallAsync('rooms') || {};
|
||||
console.log(`${Object.keys(rooms).length} rooms found`);
|
||||
|
||||
Object.keys(rooms).forEach(async roomId => {
|
||||
const room = JSON.parse(rooms[roomId]);
|
||||
const timeSinceUpdatedInSeconds = (Date.now() - room.updatedAt) / 1000;
|
||||
const timeSinceUpdatedInDays = Math.round(timeSinceUpdatedInSeconds / 60 / 60 / 24);
|
||||
if (timeSinceUpdatedInDays > 7) {
|
||||
console.log(`Deleting roomId ${roomId} which hasn't been used in ${timeSinceUpdatedInDays} days`);
|
||||
await redis.hdelAsync('rooms', roomId);
|
||||
}
|
||||
})
|
||||
|
||||
setTimeout(pollForInactiveRooms, (1000 * 60 * 60 * 12)); // every 12 hours
|
||||
}
|
@ -14,6 +14,7 @@ import crypto from 'crypto'
|
||||
import mailer from './utils/mailer';
|
||||
import koaStatic from 'koa-static';
|
||||
import koaSend from 'koa-send';
|
||||
import {pollForInactiveRooms} from './inactive_rooms';
|
||||
|
||||
bluebird.promisifyAll(Redis.RedisClient.prototype);
|
||||
bluebird.promisifyAll(Redis.Multi.prototype);
|
||||
@ -146,6 +147,8 @@ const init = async () => {
|
||||
server.listen(PORT, () => {
|
||||
console.log(`Darkwire is online at port ${PORT}`);
|
||||
})
|
||||
|
||||
pollForInactiveRooms();
|
||||
}
|
||||
|
||||
init()
|
||||
|
Loading…
x
Reference in New Issue
Block a user