Do not try to clean objects that are missing from IPFS

parent 827f2dc4
...@@ -85,13 +85,20 @@ class Cleaner { ...@@ -85,13 +85,20 @@ class Cleaner {
run() { run() {
let timestamp = Date.now() - this.expire * 1000; let timestamp = Date.now() - this.expire * 1000;
let date = new Date(timestamp).toUTCString(); let date = new Date(timestamp).toUTCString();
const ipfs = new Ipfs();
logging.info(`Performing cleanup of attachments uploaded earlier logging.info(`Performing cleanup of attachments uploaded earlier
than '${date}'`); than '${date}'`);
logging.info(`Loading list of local attachments (ipfs pin ls). Please be patient, this can take a lot of time`);
return ipfs.getLocalObjects().then(hashes => {
const hashSet = new Set(hashes);
logging.info('Looking for outdated attachments...');
return redis.zrangebyscoreAsync(this.index, [ return redis.zrangebyscoreAsync(this.index, [
'-inf', timestamp, '-inf', timestamp,
]).then((list) => { ]).then(list => {
list = list.filter(hash => hashSet.has(hash));
if (list.length <= 0) { if (list.length <= 0) {
logging.info('Nothing to clean'); logging.info('Nothing to clean');
return Promise.resolve(list.length); return Promise.resolve(list.length);
...@@ -99,17 +106,15 @@ class Cleaner { ...@@ -99,17 +106,15 @@ class Cleaner {
logging.info(`Deleting ${list.length} attachments`); logging.info(`Deleting ${list.length} attachments`);
let ipfs = new Ipfs(); return Promise.map(list, (hash) => {
let metadata = new Metadata(); return this.delAttachment(hash);
}).then(() => {
return Promise.mapSeries(list, (hash) =>
this.delAttachment(hash)
).then(() => {
logging.info(`Successfully deleted ${list.length} attachments`); logging.info(`Successfully deleted ${list.length} attachments`);
}); });
}).catch((error) => { }).catch((error) => {
logging.error(error); logging.error(error);
}); });
});
} }
} }
......
...@@ -68,6 +68,9 @@ class IpfsStorage { ...@@ -68,6 +68,9 @@ class IpfsStorage {
}); });
} }
getLocalObjects() {
return this.ipfs.pin.ls();
}
/** /**
* Pins a file by hash. * Pins a file by hash.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment