-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
executable file
·61 lines (55 loc) · 2.14 KB
/
main.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
const aws = require('aws-sdk');
const config = {};
if (process.env.DEV === 'true') {
const endpoint = process.env.AWS_ENDPOINT;
Object.assign(config, {endpoint, s3ForcePathStyle: true, logger: console});
}
const s3 = new aws.S3(config);
const CronJob = require('cron').CronJob;
const mysql = require('mysql2/promise');
let db;
async function deleteReplay(replay) {
try {
// process.stdout.write(' ');
await s3.deleteObject({Bucket: 'hotsapi', Key: `${replay.filename}.StormReplay`}).promise();
// process.stdout.write(',');
await db.query('update replays set deleted = 1 where id = ? limit 1', [replay.id]);
// process.stdout.write('.');
} catch (err) {
// console.log('');
console.log(err);
}
}
async function main() {
db = await mysql.createPool({
host: process.env.DB_HOST,
port: process.env.DB_PORT || '3306',
user: process.env.DB_USER || 'root',
database: process.env.DB_DATABASE || 'hotsapi',
password: process.env.DB_PASSWORD,
connectionLimit: 100
});
while(true) {
console.log('Getting next chunk');
console.time('Query time');
const [rows, _] = await db.query(`
select id, filename from replays where processed = 1 and deleted = 0 and not (
game_type = 'HeroLeague' and game_date > now() - interval 90 day
or game_type = 'TeamLeague' and game_date > now() - interval 90 day
or game_type = 'StormLeague' and game_date > now() - interval 90 day
or game_date > now() - interval 30 day
or created_at > now() - interval 7 day)
or processed = -1 and deleted = 0 and created_at > now() - interval 30 day
limit 30000`);
console.timeEnd('Query time');
console.log(`Got ${rows.length} results`);
if (rows.length < 100) {
console.log('Done');
break;
}
console.log('Deleting...');
await Promise.all(rows.map(r => deleteReplay(r)));
}
}
const cronTime = process.env.CRON_TIME || '* */60 * * * *';
new CronJob({cronTime, onTick: main, runOnInit: true}).start();