Skip to content

Commit

Permalink
fix(closing): more proper closing without errors
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Jun 22, 2018
1 parent 260afc5 commit b3fa8bf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/background/p2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class p2p {
for (const client in this.clients) {
this.clients[client]._socket.destroy();
}
this.peers = []
return promise
}

Expand Down
17 changes: 13 additions & 4 deletions src/background/spider.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,17 @@ setInterval(() => {

const updateTorrentTrackers = (hash) => {
let maxSeeders = 0, maxLeechers = 0, maxCompleted = 0;
mysqlSingle.query('UPDATE torrents SET trackersChecked = ? WHERE hash = ?', [Math.floor(Date.now() / 1000), hash], function(err, result) {
mysqlSingle.query('UPDATE torrents SET trackersChecked = ? WHERE hash = ?', [Math.floor(Date.now() / 1000), hash], (err, result) => {
if(!result) {
console.error(err);
return
}

udpTrackers.forEach((tracker) => {
getPeersStatisticUDP(tracker.host, tracker.port, hash, ({seeders, completed, leechers}) => {
if(this.closing) // ignore trackers response if app is closing
return

if(seeders == 0 && completed == 0 && leechers == 0)
return;

Expand Down Expand Up @@ -769,13 +772,11 @@ setInterval(() => {
}

this.stop = async (callback) => {
this.closing = true
console.log('spider closing...')
if(upnp)
upnp.ratsUnmap()

console.log('closing p2p...')
await p2p.close()

// save feed
await feed.save()

Expand Down Expand Up @@ -840,6 +841,14 @@ setInterval(() => {
}
}

console.log('closing p2p...')
// don't listen spider peer appears
spider.removeAllListeners('peer')
await p2p.close()

// don't listen complete torrent responses
client.removeAllListeners('complete')

torrentClient.destroy(() => {
sphinx.end(() => spider.close(() => {
mysqlSingle.destroy()
Expand Down

0 comments on commit b3fa8bf

Please sign in to comment.