Skip to content

Instantly share code, notes, and snippets.

@yannickcr
Created January 16, 2013 20:59
Show Gist options
  • Save yannickcr/4550894 to your computer and use it in GitHub Desktop.
Save yannickcr/4550894 to your computer and use it in GitHub Desktop.
Small script to check the actives hosts on the network and, according to the results, start/stop the torrents. NB: The high priority torrents remains untouched.
// Small script to check the actives hosts on the network and, according to the results, start/stop the torrents.
// NB: The high priority torrents remains untouched.
//
// cron task to exec it every 5min: */5 * * * * root node /volume1/web/tasks/transmission/transmission.js > /dev/null
var Transmission = require('transmission'); // node-transmission - https://github.com/FLYBYME/node-transmission
var exec = require('child_process').exec;
// Log in to transmission
var transmission = new Transmission({
username: 'username',
password: 'password'
});
var
hosts = ['192.168.0.10'], // List of the hosts to check
hostsL = hosts.length,
hostsI = 0,
hostActive = false,
torrents = []
;
hosts.forEach(function(host) {
exec('ping -w 1 ' + host, function (error, stdout, stderr) {
if (!error) hostActive = true;
if (++hostsI !== hostsL) return;
// List all torrents
transmission.get(function(err, arg) {
if(err) return console.error(err);
// Find the normal/low prority torrents
arg.torrents.forEach(function(torrent) {
if (
torrent.bandwidthPriority !== 1 && ( // Add only the low/normal priority torrents
hostActive && torrent.status > 0 || // Add only the started torrents if one host is active or
!hostActive && torrent.status === 0 // Add only the stopped torrents if no hosts are active
)
) torrents.push(torrent.id);
});
// Process the queue
transmission[hostActive ? 'stop' : 'start'](torrents);
console.log((hostActive ? 'Stopping ' : 'Starting ') + torrents.length + ' torrent(s).');
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment