Skip to content

Commit

Permalink
Execute extraction in separate thread
Browse files Browse the repository at this point in the history
Ensure we don't break the UI while extraction takes place.
  • Loading branch information
d8ahazard committed May 22, 2022
1 parent 7d80a1c commit 980f14b
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions deluge_simpleextractor/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import subprocess
import traceback
from shutil import which
from threading import Thread

import deluge.component as component
import deluge.configmanager
Expand Down Expand Up @@ -215,29 +216,34 @@ def _on_torrent_finished(self, torrent_id):
cmd = EXTRACT_COMMANDS[file_ext]
# Append file path
cmd.append(fpath)

# Do it!
try:
log.debug('Extracting with command: "%s" from working dir "%s"', " ".join(cmd), str(dest))
process = subprocess.run(cmd, cwd=dest, capture_output=True)

if process.returncode == 0:
log.info('Extract successful!')
else:
log.error(
'Extract failed: %s with code %s', fpath, process.returncode
)
except Exception as ex:
log.error("Exception:", traceback.format_exc())

# Don't mark an extracting torrent complete until callback is fired.
tid.is_finished = True
log.info("Torrent extraction/handling complete.")
log.debug("Creating thread...")
thread = Thread(target=self.do_extract, args=(cmd, dest, torrent_id, fpath))
thread.start()

else:
tid.is_finished = True
log.info("Torrent extraction/handling complete.")

def do_extract(self, cmd, destination, torrent_id, path):
log.debug("DO EXTRACT.")
torrent = component.get('TorrentManager').torrents[torrent_id]
try:
log.debug('Extracting with command: "%s" from working dir "%s"', " ".join(cmd), str(destination))
process = subprocess.run(cmd, cwd=destination, capture_output=True)

if process.returncode == 0:
log.info('Extract successful!')
else:
log.error(
'Extract failed: %s with code %s', path, process.returncode
)
except Exception:
log.error("Extract Exception:", traceback.format_exc())

# Don't mark an extracting torrent complete until callback is fired.
torrent.is_finished = True
log.info("Torrent extraction/handling complete.")

@staticmethod
def get_labels(torrent_id):
"""
Expand Down

0 comments on commit 980f14b

Please sign in to comment.