diff options
| -rw-r--r-- | poetry.lock | 30 | ||||
| -rw-r--r-- | pyproject.toml | 1 | ||||
| -rw-r--r-- | taskipy/task_runner.py | 23 | ||||
| -rw-r--r-- | tests/fixtures/project_with_tasks_that_handle_interrupts/loop_with_sigint_handler.py | 12 | ||||
| -rw-r--r-- | tests/fixtures/project_with_tasks_that_handle_sigterm/loop_with_sigterm_handler.py | 18 | ||||
| -rw-r--r-- | tests/fixtures/project_with_tasks_that_handle_sigterm/pyproject.toml | 6 | ||||
| -rw-r--r-- | tests/test_taskipy.py | 14 |
7 files changed, 86 insertions, 18 deletions
diff --git a/poetry.lock b/poetry.lock index 80e2038..d261ab2 100644 --- a/poetry.lock +++ b/poetry.lock @@ -79,15 +79,15 @@ python-versions = "*" version = "0.4.3" [[package]] -category = "dev" +category = "main" description = "Cross-platform lib for process and system monitoring in Python." name = "psutil" optional = false python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "5.7.0" +version = "5.7.2" [package.extras] -enum = ["enum34"] +test = ["ipaddress", "mock", "unittest2", "enum34", "pywin32", "wmi"] [[package]] category = "dev" @@ -153,7 +153,7 @@ python-versions = "*" version = "1.12.1" [metadata] -content-hash = "ca2a7360aa941db1e31c0e7f0ede9debe2394f0575d89b0f9a69546763449019" +content-hash = "c6ae36b0ee0970891c89a2a4b5a46cba6bffc0f758710a074e5f442f46a46189" python-versions = "^3.6" [metadata.files] @@ -217,17 +217,17 @@ mypy-extensions = [ {file = "mypy_extensions-0.4.3.tar.gz", hash = "sha256:2d82818f5bb3e369420cb3c4060a7970edba416647068eb4c5343488a6c604a8"}, ] psutil = [ - {file = "psutil-5.7.0-cp27-none-win32.whl", hash = "sha256:298af2f14b635c3c7118fd9183843f4e73e681bb6f01e12284d4d70d48a60953"}, - {file = "psutil-5.7.0-cp27-none-win_amd64.whl", hash = "sha256:75e22717d4dbc7ca529ec5063000b2b294fc9a367f9c9ede1f65846c7955fd38"}, - {file = "psutil-5.7.0-cp35-cp35m-win32.whl", hash = "sha256:f344ca230dd8e8d5eee16827596f1c22ec0876127c28e800d7ae20ed44c4b310"}, - {file = "psutil-5.7.0-cp35-cp35m-win_amd64.whl", hash = "sha256:e2d0c5b07c6fe5a87fa27b7855017edb0d52ee73b71e6ee368fae268605cc3f5"}, - {file = "psutil-5.7.0-cp36-cp36m-win32.whl", hash = "sha256:a02f4ac50d4a23253b68233b07e7cdb567bd025b982d5cf0ee78296990c22d9e"}, - {file = "psutil-5.7.0-cp36-cp36m-win_amd64.whl", hash = "sha256:1413f4158eb50e110777c4f15d7c759521703bd6beb58926f1d562da40180058"}, - {file = "psutil-5.7.0-cp37-cp37m-win32.whl", hash = "sha256:d008ddc00c6906ec80040d26dc2d3e3962109e40ad07fd8a12d0284ce5e0e4f8"}, - {file = "psutil-5.7.0-cp37-cp37m-win_amd64.whl", hash = "sha256:73f35ab66c6c7a9ce82ba44b1e9b1050be2a80cd4dcc3352cc108656b115c74f"}, - {file = "psutil-5.7.0-cp38-cp38-win32.whl", hash = "sha256:60b86f327c198561f101a92be1995f9ae0399736b6eced8f24af41ec64fb88d4"}, - {file = "psutil-5.7.0-cp38-cp38-win_amd64.whl", hash = "sha256:d84029b190c8a66a946e28b4d3934d2ca1528ec94764b180f7d6ea57b0e75e26"}, - {file = "psutil-5.7.0.tar.gz", hash = "sha256:685ec16ca14d079455892f25bd124df26ff9137664af445563c1bd36629b5e0e"}, + {file = "psutil-5.7.2-cp27-none-win32.whl", hash = "sha256:f2018461733b23f308c298653c8903d32aaad7873d25e1d228765e91ae42c3f2"}, + {file = "psutil-5.7.2-cp27-none-win_amd64.whl", hash = "sha256:66c18ca7680a31bf16ee22b1d21b6397869dda8059dbdb57d9f27efa6615f195"}, + {file = "psutil-5.7.2-cp35-cp35m-win32.whl", hash = "sha256:5e9d0f26d4194479a13d5f4b3798260c20cecf9ac9a461e718eb59ea520a360c"}, + {file = "psutil-5.7.2-cp35-cp35m-win_amd64.whl", hash = "sha256:4080869ed93cce662905b029a1770fe89c98787e543fa7347f075ade761b19d6"}, + {file = "psutil-5.7.2-cp36-cp36m-win32.whl", hash = "sha256:d8a82162f23c53b8525cf5f14a355f5d1eea86fa8edde27287dd3a98399e4fdf"}, + {file = "psutil-5.7.2-cp36-cp36m-win_amd64.whl", hash = "sha256:0ee3c36428f160d2d8fce3c583a0353e848abb7de9732c50cf3356dd49ad63f8"}, + {file = "psutil-5.7.2-cp37-cp37m-win32.whl", hash = "sha256:ff1977ba1a5f71f89166d5145c3da1cea89a0fdb044075a12c720ee9123ec818"}, + {file = "psutil-5.7.2-cp37-cp37m-win_amd64.whl", hash = "sha256:a5b120bb3c0c71dfe27551f9da2f3209a8257a178ed6c628a819037a8df487f1"}, + {file = "psutil-5.7.2-cp38-cp38-win32.whl", hash = "sha256:10512b46c95b02842c225f58fa00385c08fa00c68bac7da2d9a58ebe2c517498"}, + {file = "psutil-5.7.2-cp38-cp38-win_amd64.whl", hash = "sha256:68d36986ded5dac7c2dcd42f2682af1db80d4bce3faa126a6145c1637e1b559f"}, + {file = "psutil-5.7.2.tar.gz", hash = "sha256:90990af1c3c67195c44c9a889184f84f5b2320dce3ee3acbd054e3ba0b4a7beb"}, ] pylint = [ {file = "pylint-2.5.3-py3-none-any.whl", hash = "sha256:d0ece7d223fe422088b0e8f13fa0a1e8eb745ebffcb8ed53d3e95394b6101a1c"}, diff --git a/pyproject.toml b/pyproject.toml index 65953d3..95432cd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,6 +22,7 @@ task = 'taskipy.cli:main' [tool.poetry.dependencies] python = "^3.6" toml = "^0.10.0" +psutil = "^5.7.2" [tool.poetry.dev-dependencies] pylint = "^2.4.4" 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: diff --git a/tests/fixtures/project_with_tasks_that_handle_interrupts/loop_with_sigint_handler.py b/tests/fixtures/project_with_tasks_that_handle_interrupts/loop_with_sigint_handler.py index 80f7b23..50ed139 100644 --- a/tests/fixtures/project_with_tasks_that_handle_interrupts/loop_with_sigint_handler.py +++ b/tests/fixtures/project_with_tasks_that_handle_interrupts/loop_with_sigint_handler.py @@ -1,11 +1,21 @@ +import signal +import sys import time + def main(): try: while True: - time.sleep(.1) + time.sleep(0.1) except KeyboardInterrupt: print('failing gracefully...') + +def sigterm_handler(sig, frame): + print('Shutdown gracefully with sigterm...') + sys.exit(123) + + if __name__ == '__main__': + signal.signal(signal.SIGTERM, sigterm_handler) main() diff --git a/tests/fixtures/project_with_tasks_that_handle_sigterm/loop_with_sigterm_handler.py b/tests/fixtures/project_with_tasks_that_handle_sigterm/loop_with_sigterm_handler.py new file mode 100644 index 0000000..508c17f --- /dev/null +++ b/tests/fixtures/project_with_tasks_that_handle_sigterm/loop_with_sigterm_handler.py @@ -0,0 +1,18 @@ +import signal +import sys +import time + + +def main(): + while True: + time.sleep(0.1) + + +def sigterm_handler(sig, frame): + print('Shutdown gracefully with sigterm...') + sys.exit(123) + + +if __name__ == '__main__': + signal.signal(signal.SIGTERM, sigterm_handler) + main() diff --git a/tests/fixtures/project_with_tasks_that_handle_sigterm/pyproject.toml b/tests/fixtures/project_with_tasks_that_handle_sigterm/pyproject.toml new file mode 100644 index 0000000..1790d9b --- /dev/null +++ b/tests/fixtures/project_with_tasks_that_handle_sigterm/pyproject.toml @@ -0,0 +1,6 @@ +[tool.poetry] +name = "taskipy" +description = "tasks runner for python projects" + +[tool.taskipy.tasks] +run_loop_with_sigterm_handling = "python3 loop_with_sigterm_handler.py" diff --git a/tests/test_taskipy.py b/tests/test_taskipy.py index 5733006..37b322d 100644 --- a/tests/test_taskipy.py +++ b/tests/test_taskipy.py @@ -247,6 +247,20 @@ class InterruptingTaskTestCase(TaskipyTestCase): self.assertEqual(exit_code, 130) + def test_sigterm_should_be_sent_to_subprocess(self): + cwd = self.create_test_dir_from_fixture('project_with_tasks_that_handle_sigterm') + process = self.start_taskipy_process('run_loop_with_sigterm_handling', cwd=cwd) + + time.sleep(.2) + + process.send_signal(signal.SIGTERM) + + exit_code = process.wait() + stdout, _ = process.communicate() + + self.assertEqual(exit_code, 123) + self.assertSubstr('sigterm', str(stdout)) + class CustomRunnerTestCase(TaskipyTestCase): def test_running_command_with_custom_runner(self): |