2019-04-10 00:16:01 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const debug = require('debug')('>');
|
|
|
|
debug.enabled = true;
|
|
|
|
const { promisify } = require('util');
|
|
|
|
const RedisPool = require('redis-mpool');
|
|
|
|
const cartodbRedis = require('cartodb-redis');
|
|
|
|
const env = process.env.NODE_ENV || 'development';
|
|
|
|
const config = require(`../../config/environments/${env}`);
|
|
|
|
|
|
|
|
debug(`using "${env}" environment`);
|
|
|
|
debug(`redis "${config.redis_host}:${config.redis_port}"`);
|
|
|
|
|
|
|
|
const redisPool = new RedisPool({
|
|
|
|
name: 'remove-old-batch-jobs',
|
|
|
|
host: config.redis_host,
|
|
|
|
port: config.redis_port,
|
|
|
|
max: config.redisPool,
|
|
|
|
idleTimeoutMillis: config.redisIdleTimeoutMillis,
|
|
|
|
reapIntervalMillis: config.redisReapIntervalMillis
|
|
|
|
});
|
|
|
|
const metadata = cartodbRedis({ pool: redisPool });
|
|
|
|
const JOBS = {
|
|
|
|
DB: 5,
|
|
|
|
PREFIX: 'batch:jobs'
|
|
|
|
};
|
|
|
|
|
|
|
|
const TWO_DAYS_IN_MILLISECONDS = 48 * 3600 * 1000;
|
|
|
|
|
|
|
|
async function redisCmd (db, command, params) {
|
|
|
|
const cmd = promisify(metadata.redisCmd.bind(metadata));
|
|
|
|
const result = await cmd(db, command, params);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function scan () {
|
|
|
|
const jobs = [];
|
|
|
|
const initialCursor = '0';
|
|
|
|
|
2019-12-27 01:12:47 +08:00
|
|
|
return _scan(initialCursor, jobs);
|
2019-04-10 00:16:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
async function _scan (cursor, jobs) {
|
|
|
|
const redisParams = [cursor, 'MATCH', `${JOBS.PREFIX}:*`];
|
2019-12-24 01:19:08 +08:00
|
|
|
const [_cursor, _jobs] = await redisCmd(JOBS.DB, 'SCAN', redisParams);
|
2019-04-10 00:16:01 +08:00
|
|
|
|
|
|
|
if (_jobs && _jobs.length) {
|
|
|
|
jobs = jobs.concat(_jobs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_cursor === '0') {
|
|
|
|
return jobs;
|
|
|
|
}
|
|
|
|
|
2019-12-27 01:12:47 +08:00
|
|
|
return _scan(_cursor, jobs);
|
2019-04-10 00:16:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getJob (key) {
|
|
|
|
const props = [
|
|
|
|
'user',
|
|
|
|
'status',
|
|
|
|
'created_at',
|
|
|
|
'updated_at'
|
|
|
|
];
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
const redisParams = [`${key}`, ...props];
|
2019-04-10 00:16:01 +08:00
|
|
|
|
|
|
|
const values = await redisCmd(JOBS.DB, 'HMGET', redisParams);
|
|
|
|
|
|
|
|
const job = {};
|
2019-12-24 01:19:08 +08:00
|
|
|
for (const [i, v] of values.entries()) {
|
2019-04-10 00:16:01 +08:00
|
|
|
job[props[i]] = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function removeJob (key) {
|
2019-12-24 01:19:08 +08:00
|
|
|
const redisParams = [key];
|
2019-04-10 00:16:01 +08:00
|
|
|
|
|
|
|
const done = await redisCmd(JOBS.DB, 'DEL', redisParams);
|
|
|
|
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main () {
|
|
|
|
const summary = {
|
|
|
|
found: 0,
|
|
|
|
removed: 0
|
2019-12-24 01:19:08 +08:00
|
|
|
};
|
2019-04-10 00:16:01 +08:00
|
|
|
try {
|
2019-12-24 01:19:08 +08:00
|
|
|
debug('going to scan jobs');
|
2019-04-10 00:16:01 +08:00
|
|
|
const jobKeys = await scan();
|
|
|
|
|
|
|
|
summary.found = jobKeys.length;
|
|
|
|
debug(`found "${jobKeys.length}" jobs`);
|
2019-12-24 01:19:08 +08:00
|
|
|
debug('--------------------------------------------------');
|
2019-04-10 00:16:01 +08:00
|
|
|
|
|
|
|
for (const key of jobKeys) {
|
|
|
|
debug(`fetching job "${key}"`);
|
|
|
|
const job = await getJob(key);
|
|
|
|
|
|
|
|
debug(`job "${key}" found (user:${job.user}, status: ${job.status}, created_at: ${job.created_at}, updated_at: ${job.updated_at})`);
|
|
|
|
|
|
|
|
const updatedAt = new Date(job.updated_at).getTime();
|
|
|
|
const now = Date.now();
|
|
|
|
const elapsed = now - updatedAt;
|
|
|
|
|
|
|
|
if (elapsed > TWO_DAYS_IN_MILLISECONDS) {
|
|
|
|
debug(`job ${key} is older than two days, removing it`);
|
|
|
|
const removed = await removeJob(key);
|
|
|
|
debug(removed ? `job "${key}" removed` : `job "${key}" was not removed`);
|
|
|
|
summary.removed += removed ? 1 : 0;
|
|
|
|
} else {
|
|
|
|
debug(`job "${key}" is younger than two days, keeping it`);
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
debug('--------------------------------------------------');
|
2019-04-10 00:16:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
debug('summary:', summary);
|
|
|
|
debug('done');
|
|
|
|
} catch (err) {
|
|
|
|
debug(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main().then(() => process.exit(0));
|