aboutsummaryrefslogtreecommitdiffstats
path: root/task
diff options
context:
space:
mode:
authorRobinFrcd <29704178+RobinFrcd@users.noreply.github.com>2020-08-08 00:52:47 +0200
committerGitHub <noreply@github.com>2020-08-08 01:52:47 +0300
commit3a0362a6028b949016cd2a67bbeeab9b8d8bf50c (patch)
tree28be91f83e270b4ff461c39cef3047e68cd778bc /task
parentdocs: add eugenetriguba as a contributor (#17) (diff)
downloadtaskipy2-3a0362a6028b949016cd2a67bbeeab9b8d8bf50c.zip
Propagate SIGTERM to child processes (#19)
SIGTERM sent to taskipy are handed over to task subprocess
Diffstat (limited to '')
-rw-r--r--taskipy/task_runner.py23
1 files changed, 21 insertions, 2 deletions
diff --git a/taskipy/task_runner.py b/taskipy/task_runner.py
index cce472d..4c126dc 100644
--- a/taskipy/task_runner.py
+++ b/taskipy/task_runner.py
@@ -1,9 +1,13 @@
+import sys
+
+import psutil # type: ignore
+import signal
import subprocess
from pathlib import Path
from typing import List, Union
-from taskipy.task import Task
from taskipy.pyproject import PyProject
+from taskipy.task import Task
class TaskRunner:
@@ -17,11 +21,26 @@ class TaskRunner:
for command in task.commands:
if task.runner is not None:
command = f'{task.runner} {command}'
-
process = subprocess.Popen(
command, shell=True, cwd=Path(self.__pyproject_path).parent
)
+ def send_signal_to_task_process(signum: int, frame) -> None: # pylint: disable=unused-argument
+ # pylint: disable=W0640
+ psutil_process_wrapper = psutil.Process(process.pid)
+ is_direct_subprocess_a_shell_process = sys.platform != 'darwin' # pylint: disable=C0103
+
+ if is_direct_subprocess_a_shell_process:
+ # A shell is created because of Popen(..., shell=True) on linux only
+ # We want here to kill shell's child
+ sub_processes_of_taskipy_shell = psutil_process_wrapper.children()
+ for child_process in sub_processes_of_taskipy_shell:
+ child_process.send_signal(signum)
+ else:
+ process.send_signal(signum)
+
+ signal.signal(signal.SIGTERM, send_signal_to_task_process)
+
try:
process.wait()
except KeyboardInterrupt: