-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrun-daily-games-new.js
87 lines (71 loc) · 2.21 KB
/
run-daily-games-new.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
var Q = require('q');
Q.longStackSupport = true;
var secrets = require('./secrets.js');
var SafeMongoConnection = require('./helpers/safe-mongo-connection.js');
var completeAllGames = require('./game_logic/complete-all-games.js');
var updateLeaderboard = require('./stats/update-leaderboard.js');
var startStopContainers = require('./docker/container_interaction/start-stop-containers.js');
var mongoOptions = {
server: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 30000
},
auto_reconnect: true
},
replset: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 30000
}
}
};
// Run and save all games and all user stats
var runDailyGames = function() {
console.log('Running daily games')
// Connect to mongo database
var mongoConnection = new SafeMongoConnection(secrets.mongoKey, mongoOptions);
mongoConnection.connect()
.then(function() {
// Get all users
return mongoConnection.safeInvoke('users', 'find')
.then(function(response) {
return Q.ninvoke(response, 'toArray');
})
.then(function(users) {
// Run all the games, then update the leaderboard
return completeAllGames(users, mongoConnection)
.then(function() {
console.log('All games completed!');
console.log('Updating leaderboard...');
return updateLeaderboard(users, mongoConnection);
})
.then(function() {
console.log('Leaderboard updated successfully!');
});
})
// Clean up after everything finishes (regardless of errors)
.finally(function() {
console.log('Closing database connection before the script ends.');
return mongoConnection.disconnect()
.then(function() {
console.log('Database closed successfully.')
console.log('Shutting down all containers before the script ends.');
return startStopContainers.shutDownAllContainers()
})
.then(function() {
console.log('Containers closed successfully');
});
});
})
.done(
function() {
console.log('Script completed successfully!');
},
function(err) {
console.log('There was an uncaught error in the script:');
throw err;
}
);
};
runDailyGames();