Created
October 6, 2020 19:27
-
-
Save bonny1992/3bed65f4bcdc372b833b65442995c71a to your computer and use it in GitHub Desktop.
Rtorrent + rtcontrol space management
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Start torrents when diskspace is enough | |
schedule = start_when_disk_enough,30,60,"execute.nothrow=/usr/bin/python,/config/scripts/space.py" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from __future__ import print_function | |
import os | |
import shutil | |
import subprocess | |
import logging | |
import re | |
from logging.handlers import RotatingFileHandler | |
from logging import handlers | |
import sys | |
import getpass | |
gb = 10 ** 9 | |
LOGFILE = os.getenv('LOGFILE','/config/scripts/logs/space.log') | |
MIN_SPACE_GB = os.getenv('MIN_SPACE_GB', 150) # This has to be the same as the one you set up in your Pyrotorque config.py file | |
PYROCORE_CONFIG_DIR = os.getenv('PYROCORE_CONFIG_DIR', '/config/pyrocore') | |
DOWNLOAD_DIR = os.getenv('DOWNLOAD_DIR','/downloads') | |
DRY_RUN = os.getenv('DRY_RUN', False) | |
_COMMAND = 'rtcontrol' | |
_COMMAND_DIR = '--config-dir=' + PYROCORE_CONFIG_DIR | |
_COMMAND_ARGS_SWITCH = '-V' | |
if DRY_RUN: | |
_COMMAND_ARGS_SWITCH = '-nV' | |
_COMMAND_ARGS_ACTIVE = 'is_active=no' | |
_COMMAND_ARGS_COMPLETE = 'is_complete=no' | |
_COMMAND_ARGS_IGNORED = 'is_ignored=no' | |
_COMMAND_ARGS_HAS_ROOM = 'has_room=yes' | |
_COMMAND_ARGS_ACTION = '--start' | |
log = logging.getLogger('') | |
log.setLevel(logging.DEBUG) | |
format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") | |
ch = logging.StreamHandler(sys.stdout) | |
ch.setFormatter(format) | |
log.addHandler(ch) | |
fh = handlers.RotatingFileHandler(LOGFILE, maxBytes=(1048576*5), backupCount=7) | |
fh.setFormatter(format) | |
log.addHandler(fh) | |
log.debug("User: %s", getpass.getuser()) | |
total, used, free = shutil.disk_usage(DOWNLOAD_DIR) | |
free_gb = free / gb | |
log.info("Free space: %s", free_gb) | |
if free_gb > MIN_SPACE_GB: | |
log.info('Starting torrents...') | |
else: | |
log.info('Not enough space left on device.') | |
command_start = [_COMMAND, | |
_COMMAND_DIR, | |
_COMMAND_ARGS_SWITCH, | |
_COMMAND_ARGS_ACTIVE, | |
_COMMAND_ARGS_COMPLETE, | |
_COMMAND_ARGS_IGNORED, | |
_COMMAND_ARGS_HAS_ROOM, | |
'--start'] | |
command_stop = [_COMMAND, | |
_COMMAND_DIR, | |
_COMMAND_ARGS_SWITCH, | |
_COMMAND_ARGS_ACTIVE, | |
_COMMAND_ARGS_COMPLETE, | |
_COMMAND_ARGS_IGNORED, | |
_COMMAND_ARGS_HAS_ROOM, | |
'--stop'] | |
log.debug('Command: %s', command_start) | |
process = subprocess.run(command_start, stdout=subprocess.PIPE) | |
process = subprocess.run(command_stop, stdout=subprocess.PIPE) | |
process = subprocess.run(command_start, stdout=subprocess.PIPE) | |
output = process.stdout.decode('utf-8') | |
pattern = r'START (\d+) out of (\d+) torrents.' | |
match = re.search(pattern, output) | |
changed = match.group(1) | |
total = match.group(2) | |
log.info('Started %s of %s torrents.%s', changed, total, ' [SIMULATED]' if DRY_RUN else '') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Starting, then stopping, then starting the torrents again made the status "stopped because of low space" go away.