diff --git a/controller/torrent.js b/controller/torrent.js index 84ed4df..6b175a5 100644 --- a/controller/torrent.js +++ b/controller/torrent.js @@ -5,9 +5,19 @@ const {Torrent} = require('>/models'); console.log('here!!!!!!!!!!!!!') +let statusLock = false; setInterval(async function(){ - ps.publish('torrent:server:status', await Torrent.trClient.sessionStats()) -}, 3000); + if(statusLock) return; + statusLock = true; + try{ + ps.publish('torrent:server:status', await Torrent.trClient.sessionStats()); + }catch(error){ + ps.publish('torrent:server:status:down') + // if(error.code === 'ECONNREFUSED') throw new Error('TorrentGatewayDown'); + // console.error('status interval error', error) + } + statusLock = false +}, 10000, statusLock); // ps.subscribe(/./g, function(...args){ diff --git a/models/sql/migrations/20240101174644-create-torrent.js b/models/sql/migrations/20240101174644-create-torrent.js index 16f6bc3..a423528 100644 --- a/models/sql/migrations/20240101174644-create-torrent.js +++ b/models/sql/migrations/20240101174644-create-torrent.js @@ -9,9 +9,6 @@ module.exports = { primaryKey: true, type: Sequelize.INTEGER }, - torrent_id: { - type: Sequelize.STRING - }, hashString: { type: Sequelize.STRING }, diff --git a/models/sql/torrent.js b/models/sql/torrent.js index eeb35e0..7d60f9a 100644 --- a/models/sql/torrent.js +++ b/models/sql/torrent.js @@ -6,14 +6,14 @@ const conf = require('>/conf'); const tr_client = new Transmission(conf.transmission) const statusMap = [ - 'STOPPED', - 'CHECK_WAIT', - 'CHECK', - 'DOWNLOAD_WAIT', - 'DOWNLOAD', - 'SEED_WAIT', - 'SEED', - 'ISOLATED', + 'STOPPED', // 0 + 'CHECK_WAIT', // 1 + 'CHECK', // 2 + 'DOWNLOAD_WAIT', // 3 + 'DOWNLOAD', // 4 + 'SEED_WAIT', // 5 + 'SEED', // 6 + 'ISOLATED', // 7 ]; module.exports = (sequelize, DataTypes, Model) => { @@ -41,7 +41,6 @@ module.exports = (sequelize, DataTypes, Model) => { return await super.create({ magnetLink: data.magnetLink, - torrent_id: res.id, hashString: res.hashString, name: res.name, added_by: data.added_by, @@ -49,14 +48,14 @@ module.exports = (sequelize, DataTypes, Model) => { percentDone: 0, }, args); }catch (error){ - // console.log('Torrent create error', error); + console.log('Torrent create error', error); throw error; } } async getTorrentData(noUpdate){ try{ - let res = ( await tr_client.get(Number(this.torrent_id), [ + let res = ( await tr_client.get(this.hashString, [ "eta", "percentDone", "status", "rateDownload", "errorString", "hashString", 'name', 'downloadDir', @@ -69,17 +68,38 @@ module.exports = (sequelize, DataTypes, Model) => { 'sizeWhenDone', ]) ).torrents[0]; + if(this.percentDone === 1) return this.dataValues await this.update(res); if(noUpdate) await this.save(); return {...res, ...this.dataValues}; }catch(error){ - console.error(`Torrent ${this.id} getTorrentData error`, error); + if(error.code === 'ECONNREFUSED'){ + let e = new Error('TorrentGatewayDown') + e.status = 555 + throw e + } + // console.error(`Torrent ${this.hashString} getTorrentData error`, error); throw error; } } + + async stop(){ + return await this.constructor.trClient.stop(this.hashString); + } + + async start(force){ + if(force) return await this.constructor.trClient.startNow(this.hashString); + let res = await this.constructor.trClient.start(this.hashString); + console.log('start', res); + return res; + } + + async destroy(){ + await await this.constructor.trClient.remove(this.hashString, true); + return await super.destroy() + } } Torrent.init({ - torrent_id: DataTypes.STRING, magnetLink: { type: DataTypes.STRING, allowNull: false, diff --git a/public/js/app.js b/public/js/app.js index 066d6fd..915c35f 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -64,7 +64,7 @@ app.socket = (function(app){ if(data.__noSocket) return; // console.log('local_pubs 2', data, topic) - socket.emit('P2PSub', { topic, data }) + socket.emit('P2PSub', { topic, data }); }); }) @@ -75,7 +75,14 @@ app.socket = (function(app){ app.api = (function(app){ var baseURL = '/__api/' + + $( document ).on( "ajaxError", function(event, res, req) { + console.log('bad!', `app:api:error:`, res.status); + app.publish(`app:api:error:${res.status}`, {res, res}); + } ); + function post(url, data, callback){ + callback = callback || app.util.emptyFuction; $.ajax({ type: 'POST', url: baseURL+url, diff --git a/public/lib/js/jq-repeat.js b/public/lib/js/jq-repeat.js index 62cb3b2..48b9897 100644 --- a/public/lib/js/jq-repeat.js +++ b/public/lib/js/jq-repeat.js @@ -95,7 +95,7 @@ //figure out new elements index var key = I + index; // apply values to template - var render = Mustache.render( this.__rq_template, toAdd[I] ); + var render = Mustache.render( this.__rq_template, {__id:I, ...toAdd[I]} ); //set call name and index keys to DOM element var $render = $( render ).addClass( 'jq-repeat-'+ this.__repeatId ).attr( 'jq-repeat-index', key ); diff --git a/public/partial/header.html b/public/partial/header.html index 2a9a5bf..2730ad2 100644 --- a/public/partial/header.html +++ b/public/partial/header.html @@ -110,7 +110,10 @@ padding-left: 0; margin-bottom: 0; } - + #tbp_proxy_torrent_dialog li{ + padding-left: 1em; + padding-right: 1em; + }