diff --git a/app/modules/seeder.js b/app/modules/seeder.js index bcf9f19..e0b297e 100644 --- a/app/modules/seeder.js +++ b/app/modules/seeder.js @@ -54,7 +54,31 @@ Seeder.prototype.seed = function () { function (result, cb) { torrentHash = result.torrentHash.toString('hex') logger.debug('Added file to BitTorrent network, torrentHash: ', torrentHash, ' - #', index) - self.handler.shareMetadata(torrentHash, cb) + try { + self.handler.shareMetadata(torrentHash, function (err, result) { + if (err) { + // Try to remove metadata that might have already been inserted + try { + self.handler.removeMetadata(torrentHash, function dummy() { + }); + } + catch(err) {} + cb(err); + } + else { + cb(null, result); + } + }) + } + catch (err) { + // Try to remove metadata that might have already been inserted + try { + self.handler.removeMetadata(torrentHash, function dummy() { + }); + } + catch(err) {} + cb(err); + } }, function (result, cb) { logger.debug('Started seeding, torrentHash: ', torrentHash, ' - #', index) @@ -63,7 +87,12 @@ Seeder.prototype.seed = function () { function (cb) { logger.debug('Remove torrent, torrentHash: ', torrentHash, ' - #', index) // stop seeding current files (otherwise they'll keep open I\O connections and re-announce) - self.handler.removeMetadata(torrentHash, cb) + try { + self.handler.removeMetadata(torrentHash, cb) + } + catch (err) { + cb(err); + } } ], function (err) { diff --git a/package.json b/package.json index 388646e..20e1542 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "async": "^2.1.4", "aws-sdk": "^2.1.16", "casimircore": "^0.11.3", - "cc-metadata-handler": "^0.6.0", + "cc-metadata-handler": "git://github.com/blockchainofthings/Metadata-Handler.git#BCoTv0.6.2", "graceful-fs": "^4.1.2", "jsonfile": "^2.0.0", "jwt-simple": "^0.3.1", diff --git a/startup.js b/startup.js index 9d22c24..2cf44d3 100644 --- a/startup.js +++ b/startup.js @@ -53,6 +53,12 @@ folders.forEach(function (dir) { } }) +// Trap unhandled exceptions to avoid application from aborting its processing +process.on('uncaughtException', (err) => { + logger.error('Unhandled exception.', err); + logger.debug('Continue processing...'); +}); + server.http_server.listen(server.port, function (err) { if (err) { logger.info('Critical Error so killing server - ' + err)