|
17 | 17 | @contextmanager
|
18 | 18 | def watcher_external(records: HTTPRecords) -> Generator[HTTPRecords, None, None]:
|
19 | 19 | remove_environment_variable = False
|
20 |
| - try: |
21 |
| - watcher = None |
22 | 20 |
|
23 |
| - if HTTPDBG_MULTIPROCESS_DIR not in os.environ: |
24 |
| - with tempfile.TemporaryDirectory( |
25 |
| - prefix="httpdbg" |
26 |
| - ) as httpdbg_multiprocess_dir: |
| 21 | + if HTTPDBG_MULTIPROCESS_DIR not in os.environ: |
| 22 | + with tempfile.TemporaryDirectory(prefix="httpdbg") as httpdbg_multiprocess_dir: |
27 | 23 |
|
28 |
| - logger().info(f"watcher_external {httpdbg_multiprocess_dir}") |
29 |
| - remove_environment_variable = True |
30 |
| - os.environ[HTTPDBG_MULTIPROCESS_DIR] = httpdbg_multiprocess_dir |
| 24 | + logger().info(f"watcher_external {httpdbg_multiprocess_dir}") |
| 25 | + remove_environment_variable = True |
| 26 | + os.environ[HTTPDBG_MULTIPROCESS_DIR] = httpdbg_multiprocess_dir |
31 | 27 |
|
32 |
| - # we use a custom sitecustomize.py script to record the request in the subprocesses. |
33 |
| - # It doesn't work if the subprocess is created using the "fork" method |
34 |
| - # instead of the "spawn" method. |
35 |
| - template = os.path.join( |
36 |
| - os.path.dirname(os.path.abspath(__file__)), |
37 |
| - "template_sitecustomize.py", |
38 |
| - ) |
39 |
| - sitecustomize = os.path.join( |
40 |
| - httpdbg_multiprocess_dir, "sitecustomize.py" |
41 |
| - ) |
| 28 | + # we use a custom sitecustomize.py script to record the request in the subprocesses. |
| 29 | + # It doesn't work if the subprocess is created using the "fork" method |
| 30 | + # instead of the "spawn" method. |
| 31 | + template = os.path.join( |
| 32 | + os.path.dirname(os.path.abspath(__file__)), |
| 33 | + "template_sitecustomize.py", |
| 34 | + ) |
| 35 | + sitecustomize = os.path.join(httpdbg_multiprocess_dir, "sitecustomize.py") |
42 | 36 |
|
43 |
| - shutil.copy(template, sitecustomize) |
| 37 | + shutil.copy(template, sitecustomize) |
44 | 38 |
|
45 |
| - if "PYTHONPATH" in os.environ: |
46 |
| - os.environ["PYTHONPATH"] = ( |
47 |
| - f"{httpdbg_multiprocess_dir}:{os.environ['PYTHONPATH']}" |
48 |
| - ) |
49 |
| - else: |
50 |
| - os.environ["PYTHONPATH"] = httpdbg_multiprocess_dir |
| 39 | + if "PYTHONPATH" in os.environ: |
| 40 | + os.environ["PYTHONPATH"] = ( |
| 41 | + f"{httpdbg_multiprocess_dir}:{os.environ['PYTHONPATH']}" |
| 42 | + ) |
| 43 | + else: |
| 44 | + os.environ["PYTHONPATH"] = httpdbg_multiprocess_dir |
51 | 45 |
|
52 |
| - watcher = WatcherSubprocessDirThread(records) |
| 46 | + try: |
| 47 | + watcher = WatcherSubprocessDirThread( |
| 48 | + records, httpdbg_multiprocess_dir, 1.0 |
| 49 | + ) |
53 | 50 | watcher.start()
|
54 | 51 |
|
55 | 52 | yield records
|
56 |
| - |
| 53 | + finally: |
57 | 54 | watcher.shutdown()
|
58 |
| - watcher.join() |
59 |
| - else: |
60 |
| - yield records |
61 |
| - except Exception as ex: |
62 |
| - if watcher: |
63 |
| - watcher.shutdown() |
64 |
| - raise ex |
65 |
| - finally: |
66 |
| - if remove_environment_variable and (HTTPDBG_MULTIPROCESS_DIR in os.environ): |
67 |
| - del os.environ[HTTPDBG_MULTIPROCESS_DIR] |
| 55 | + if remove_environment_variable and ( |
| 56 | + HTTPDBG_MULTIPROCESS_DIR in os.environ |
| 57 | + ): |
| 58 | + del os.environ[HTTPDBG_MULTIPROCESS_DIR] |
| 59 | + else: |
| 60 | + yield records |
68 | 61 |
|
69 | 62 |
|
70 | 63 | class WatcherSubprocessDirThread(threading.Thread):
|
71 |
| - def __init__(self, records: HTTPRecords, delay: int = 1) -> None: |
72 |
| - self.stop = False |
73 |
| - self.records = records |
74 |
| - self.delay = delay |
| 64 | + def __init__(self, records: HTTPRecords, directory: str, delay: float) -> None: |
| 65 | + self.records: HTTPRecords = records |
| 66 | + self.directory: str = directory |
| 67 | + self.delay: float = delay |
| 68 | + self._running: bool = True |
75 | 69 | threading.Thread.__init__(self)
|
76 | 70 |
|
77 | 71 | def load_dump(self):
|
78 |
| - for dump in glob.glob( |
79 |
| - f"{os.environ[HTTPDBG_MULTIPROCESS_DIR]}/*.httpdbgrecords" |
80 |
| - ): |
| 72 | + for dump in glob.glob(f"{self.directory}/*.httpdbgrecords"): |
81 | 73 | with open(dump, "rb") as dumpfile:
|
82 | 74 | newrecords: HTTPRecords = pickle.load(dumpfile)
|
83 | 75 | self.records.requests.update(newrecords.requests)
|
84 | 76 | self.records.initiators.update(newrecords.initiators)
|
85 | 77 | self.records.groups.update(newrecords.groups)
|
86 | 78 |
|
87 | 79 | def run(self):
|
88 |
| - while not self.stop: |
89 |
| - time.sleep(self.delay) |
| 80 | + while self._running and (HTTPDBG_MULTIPROCESS_DIR in os.environ): |
90 | 81 | self.load_dump()
|
| 82 | + time.sleep(self.delay) |
91 | 83 |
|
92 | 84 | def shutdown(self):
|
93 |
| - self.stop = True |
| 85 | + self._running = False |
| 86 | + self.join(timeout=5) |
| 87 | + self.load_dump() |
0 commit comments