darkwire.io/server/src/inactive_rooms.js
Alan Friedman be81d7420c
Revert "Revert "Allow to launch server without Redis"" (#134)
* Revert "Revert "Allow to launch server without Redis (#119)" (#133)"

This reverts commit 18065f965215c32b4b66e13246a6a54977088518.

* Default to REDIS_URL env var for store host
2020-05-05 10:26:21 -04:00

26 lines
830 B
JavaScript

import getStore from './store';
export async function pollForInactiveRooms() {
const store = getStore();
console.log('Checking for inactive rooms...');
const rooms = (await store.getAll('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 store.del('rooms', roomId);
}
});
setTimeout(pollForInactiveRooms, 1000 * 60 * 60 * 12); // every 12 hours
}