Skip to content

Commit 9c22acf

Browse files
committed
refactor(graphql-api): Added on error for torrents
So if a torrent errors that one is set on failed instead of having the whole cleint error
1 parent 0f689ea commit 9c22acf

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

Diff for: apps/graphql-api/src/shared/torrent/torrent.service.ts

+38-18
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class TorrentService {
102102
*/
103103
private setupWebTorrent(wasCrash = false) {
104104
this.webTorrent = new WebTorrent({
105-
maxConns: 55, // Is the default
105+
maxConns: 55 // Is the default
106106
})
107107

108108
this.webTorrent.on('error', (error) => {
@@ -311,7 +311,7 @@ export class TorrentService {
311311
// Add a unique download location for this item
312312
path: this.getDownloadLocation(download),
313313
maxWebConns: 5,
314-
announce: this.trackers,
314+
announce: this.trackers
315315
},
316316
this.handleTorrent(resolve, item, download, magnet)
317317
)
@@ -375,27 +375,47 @@ export class TorrentService {
375375
// Keep track if we are currently updating the model, prevents updating same item twice at the same time
376376
let updatingModel = false
377377

378+
torrent.on('error', async (err) => {
379+
this.logger.error(`[${download._id}]: Torrent error`, err)
380+
381+
await this.updateOne(item, {
382+
download: {
383+
downloadStatus: TorrentService.STATUS_FAILED,
384+
downloading: false
385+
}
386+
})
387+
388+
// Remove from torrents
389+
this.removeFromTorrents(download)
390+
391+
// Also cleanup this download
392+
await this.cleanUpDownload(download)
393+
394+
this.removeFromWebTorrent(magnet)
395+
396+
// Resolve instead of reject as no try catch is around the method
397+
resolve()
398+
})
399+
378400
torrent.on('noPeers', async (announceType) => {
379-
if (announceType === 'dht') {
380-
this.logger.warn(`[${download._id}]: No peers found`)
381-
await this.updateOne(item, {
382-
download: {
383-
downloadStatus: TorrentService.STATUS_FAILED,
384-
downloading: false
385-
}
386-
})
401+
this.logger.warn(`[${download._id}]: No peers found, announce type: ${announceType}`)
402+
await this.updateOne(item, {
403+
download: {
404+
downloadStatus: TorrentService.STATUS_FAILED,
405+
downloading: false
406+
}
407+
})
387408

388-
// Remove from torrents
389-
this.removeFromTorrents(download)
409+
// Remove from torrents
410+
this.removeFromTorrents(download)
390411

391-
// Also cleanup this download
392-
await this.cleanUpDownload(download)
412+
// Also cleanup this download
413+
await this.cleanUpDownload(download)
393414

394-
this.removeFromWebTorrent(magnet)
415+
this.removeFromWebTorrent(magnet)
395416

396-
// Resolve instead of reject as no try catch is around the method
397-
resolve()
398-
}
417+
// Resolve instead of reject as no try catch is around the method
418+
resolve()
399419
})
400420

401421
torrent.on('download', async () => {

0 commit comments

Comments
 (0)