Do not try to clean objects that are missing from IPFS

parent 827f2dc4
...@@ -85,30 +85,35 @@ class Cleaner { ...@@ -85,30 +85,35 @@ 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}'`);
return redis.zrangebyscoreAsync(this.index, [ logging.info(`Loading list of local attachments (ipfs pin ls). Please be patient, this can take a lot of time`);
'-inf', timestamp,
]).then((list) => { return ipfs.getLocalObjects().then(hashes => {
if (list.length <= 0) { const hashSet = new Set(hashes);
logging.info('Nothing to clean'); logging.info('Looking for outdated attachments...');
return Promise.resolve(list.length); return redis.zrangebyscoreAsync(this.index, [
} '-inf', timestamp,
]).then(list => {
logging.info(`Deleting ${list.length} attachments`); list = list.filter(hash => hashSet.has(hash));
if (list.length <= 0) {
let ipfs = new Ipfs(); logging.info('Nothing to clean');
let metadata = new Metadata(); return Promise.resolve(list.length);
}
return Promise.mapSeries(list, (hash) =>
this.delAttachment(hash) logging.info(`Deleting ${list.length} attachments`);
).then(() => {
logging.info(`Successfully deleted ${list.length} attachments`); return Promise.map(list, (hash) => {
return this.delAttachment(hash);
}).then(() => {
logging.info(`Successfully deleted ${list.length} attachments`);
});
}).catch((error) => {
logging.error(error);
}); });
}).catch((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