From 2ff2213849e2ce62a9e969c34391004dbcab2d07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cla=CC=81udio=20Sa=CC=81=20de=20Castro?= Date: Thu, 28 Sep 2017 09:14:39 -0300 Subject: [PATCH] =?UTF-8?q?Improvements=20to=20seeding=20procedure:=20=20-?= =?UTF-8?q?=20better=20error=20handling=20on=20shareMetadata=20and=20remov?= =?UTF-8?q?eMetadata=20steps;=20=20-=20reference=20new=20improved=20versio?= =?UTF-8?q?n=20of=20cc-metadata-handler=20package=20from=20Blockchain=20of?= =?UTF-8?q?=20Things=E2=80=99=20repository;=20=20-=20as=20an=20additional?= =?UTF-8?q?=20precaution,=20code=20to=20trap=20unhandled=20exceptions=20ha?= =?UTF-8?q?s=20been=20added.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/modules/seeder.js | 33 +++++++++++++++++++++++++++++++-- package.json | 2 +- startup.js | 6 ++++++ 3 files changed, 38 insertions(+), 3 deletions(-) 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)