Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: intercept SIGINT #18

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions git_hg_sync/application.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import signal
from types import FrameType
from typing import Optional

from mozlog import get_proxy_logger

from git_hg_sync.events import Push, Tag
@@ -22,6 +26,11 @@ def __init__(
self._mappings = mappings

def run(self) -> None:
def signal_handler(sig: int, frame: Optional[FrameType]) -> None:
self._worker.shoud_stop = True
logger.info("Process stopped by user")

signal.signal(signal.SIGINT, signal_handler)
self._worker.run()

def _handle_push_event(self, push_event: Push) -> None:
23 changes: 23 additions & 0 deletions tests/test_pulse_worker.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import signal
import time
from pathlib import Path
from subprocess import PIPE, Popen

import pytest

from git_hg_sync.events import Push, Tag
from git_hg_sync.pulse_worker import EntityTypeError, PulseWorker

HERE = Path(__file__).parent


def raw_push_entity():
return {
@@ -39,3 +46,19 @@ def test_parse_entity_valid() -> None:
def test_parse_invalid_type() -> None:
with pytest.raises(EntityTypeError):
PulseWorker.parse_entity({"type": "unknown"})


def test_sigint_signal_interception() -> None:
config_file = HERE / "data" / "config.toml"
module_path = HERE.parent / "git_hg_sync" / "__main__.py"
process = Popen(
["python", module_path, "-c", config_file], shell=True, stdout=PIPE, stderr=PIPE
)
time.sleep(1)
process.send_signal(signal.SIGINT)
time.sleep(1)
try:
assert process.returncode == 0
except AssertionError as e:
process.kill()
raise e