Skip to content

Commit

Permalink
[WIP] Remove GridFS media chunks as well
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenixeliot committed Mar 23, 2017
1 parent 28e2ec6 commit d21cb7c
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions scripts/mongodb/removeOldAnonymousUsers.js
Expand Up @@ -18,24 +18,29 @@ const Promise = require('bluebird');
const co = require('co');
const User = require('../../server/models/User')
const LevelSession = require('../../server/models/LevelSession')
const Grid = require('gridfs-stream')

console.log("Started script");
co(function*(){
"use strict";
yield mongoose.connect(program.mainMongoConnUrl);
const connection = mongoose.connection;
Grid.gfs = Grid(mongoose.connection.db, mongoose.mongo)

console.log("Counting old users...");
const numOldUsers = yield User.count({
anonymous: true,
dateCreated: {
$lt: new Date(new Date() - 1000*60*60*24*90)
}
})
const batchSize = 1000;
// const numOldUsers = yield User.count({
// anonymous: true,
// dateCreated: {
// $lt: new Date(new Date() - 1000*60*60*24*90)
// }
// })
const numOldUsers = 18176411;
const batchSize = 100000;
const numBatches = Math.ceil(numOldUsers / batchSize);
console.log(`Found ${numOldUsers} old users to delete (${numBatches} batches of ${batchSize})`);

var totalLevelSessions = 0;
var totalMedia = 0;
for(var batchNumber = 0; batchNumber < numBatches; batchNumber++) {
const oldUsers = yield User.find({
anonymous: true,
Expand All @@ -50,7 +55,19 @@ co(function*(){
const levelSessions = yield LevelSession.find({
creator: {$in: oldUserIds}
})
console.log("found", levelSessions.length, "level sessions");
totalLevelSessions += levelSessions.length;
console.log(`found ${levelSessions.length} level sessions (${totalLevelSessions} total)`);

Grid.gfs.collection('media').find({ "metadata.creator": {$in: oldUserIds} }, (err, media) => {
co(function*(){
media = yield media.toArray();
console.log(`found ${media.length} media chunks`);
totalMedia += media.length;
console.log(media);
})
});

console.log(`Found ${totalMedia} media chunks so far.`);

if(!program.dryRun) {
console.log("Removing users!");
Expand Down

0 comments on commit d21cb7c

Please sign in to comment.