Last active
December 2, 2018 22:14
-
-
Save lennym/b2c5e86553ee0eaf4a90b156c5f9316b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
require('env2')('.env'); | |
const AWS = require('aws-sdk'); | |
const async = require('async'); | |
const Progress = require('cli-progress'); | |
AWS.config.region = 'eu-west-1'; | |
const s3 = new AWS.S3(); | |
function fetchObjects (startToken, callback) { | |
const params = { | |
Bucket: 'numo-taggy', | |
Prefix: 'ci' | |
}; | |
if (startToken) { | |
params.ContinuationToken = startToken; | |
} | |
s3.listObjectsV2(params, (err, data) => { | |
if (err) { return callback(err); } | |
const tiles = data.Contents; | |
if (data.IsTruncated) { | |
fetchObjects(data.NextContinuationToken, (err, nextSet) => { | |
callback(err, tiles.concat(nextSet)); | |
}); | |
} else { | |
callback(null, tiles); | |
} | |
}); | |
} | |
function touch (key, callback) { | |
s3.copyObject({ | |
Bucket: 'numo-taggy', | |
CopySource: `numo-taggy/${key}`, | |
Key: key, | |
MetadataDirective: 'REPLACE' | |
}, callback); | |
} | |
function reindex (condition, callback) { | |
condition = condition || (_ => true); | |
fetchObjects(null, (err, tiles) => { | |
if (err) { return callback(err); } | |
const keys = tiles | |
.map(tile => tile.Key) | |
.filter(key => key.match(/\.json$/)); | |
const touched = []; | |
const bar = new Progress.Bar({ etaBuffer: 100 }); | |
bar.start(keys.length, 0); | |
function updateProgress () { | |
bar.update(bar.value + 1); | |
} | |
async.eachLimit(keys, 10, (tile, callback) => { | |
condition(tile, (err, shouldTouch) => { | |
if (err) { return callback(err); } | |
if (shouldTouch) { | |
touched.push(tile); | |
touch(tile, (err) => { | |
updateProgress(); | |
callback(err); | |
}); | |
} else { | |
updateProgress(); | |
callback(); | |
} | |
}); | |
}, (err) => { | |
bar.stop(); | |
if (err) { return callback(err); } | |
callback(err, touched); | |
}); | |
}); | |
} | |
/** | |
* Re-index tiles which are marked as inactive | |
*/ | |
reindex((key, callback) => { | |
s3.getObject({ | |
Bucket: 'numo-taggy', | |
Key: key | |
}, (err, data) => { | |
if (err) { return callback(err); } | |
let body; | |
try { | |
body = JSON.parse(data.Body.toString()); | |
} catch (e) { | |
return callback(e); | |
} | |
callback(null, body.active === false); | |
}); | |
}, (err, touched) => { | |
if (err) { | |
console.error(err); | |
return process.exit(1); | |
} | |
console.log(`${touched.length} inactive tiles found`); | |
console.log(touched.join('\n')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment