Execute: make running commands asynchronous
This prevents deluge from hanging while it waits for a command to finish. This also prevents a deadlock from occuring (c.f. warning at http://docs.python.org/library/subprocess.html#subprocess.Popen.wait)
This commit is contained in:
parent
fd28bf8619
commit
ba75ae4ccc
|
@ -37,7 +37,7 @@ import os
|
||||||
import time
|
import time
|
||||||
import hashlib
|
import hashlib
|
||||||
import logging
|
import logging
|
||||||
from subprocess import Popen, PIPE
|
from twisted.internet.utils import getProcessValue
|
||||||
|
|
||||||
from deluge.plugins.pluginbase import CorePluginBase
|
from deluge.plugins.pluginbase import CorePluginBase
|
||||||
import deluge.component as component
|
import deluge.component as component
|
||||||
|
@ -112,15 +112,18 @@ class Core(CorePluginBase):
|
||||||
|
|
||||||
log.debug("[execute] Running commands for %s", event)
|
log.debug("[execute] Running commands for %s", event)
|
||||||
|
|
||||||
|
def log_error(exit_code, command):
|
||||||
|
if exit_code:
|
||||||
|
log.warn("[execute] command '%s' failed with exit code %d", command, exit_code)
|
||||||
|
|
||||||
# Go through and execute all the commands
|
# Go through and execute all the commands
|
||||||
for command in self.config["commands"]:
|
for command in self.config["commands"]:
|
||||||
if command[EXECUTE_EVENT] == event:
|
if command[EXECUTE_EVENT] == event:
|
||||||
command = os.path.expandvars(command[EXECUTE_COMMAND])
|
command = os.path.expandvars(command[EXECUTE_COMMAND])
|
||||||
command = os.path.expanduser(command)
|
command = os.path.expanduser(command)
|
||||||
log.debug("[execute] running %s", command)
|
log.debug("[execute] running %s", command)
|
||||||
p = Popen([command, torrent_id, torrent_name, save_path], stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
d = getProcessValue(command, (torrent_id, torrent_name, save_path), env=os.environ)
|
||||||
if p.wait() != 0:
|
d.addCallback(log_error, command)
|
||||||
log.warn("Execute command failed with exit code %d", p.returncode)
|
|
||||||
|
|
||||||
def disable(self):
|
def disable(self):
|
||||||
self.config.save()
|
self.config.save()
|
||||||
|
|
Loading…
Reference in New Issue