ominfra 0.0.0.dev90__py3-none-any.whl → 0.0.0.dev92__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,108 +0,0 @@
1
- # ruff: noqa: UP007
2
- import fcntl
3
- import os.path
4
- import queue # noqa
5
- import subprocess
6
- import time
7
- import typing as ta
8
-
9
- from omlish.lite.cached import cached_nullary
10
- from omlish.lite.check import check_not_none
11
- from omlish.lite.logs import log
12
- from omlish.lite.subprocesses import subprocess_shell_wrap_exec
13
-
14
- from ..threadworker import ThreadWorker
15
- from .messages import JournalctlMessage # noqa
16
- from .messages import JournalctlMessageBuilder
17
-
18
-
19
- class JournalctlTailerWorker(ThreadWorker):
20
- DEFAULT_CMD: ta.ClassVar[ta.Sequence[str]] = ['journalctl']
21
-
22
- def __init__(
23
- self,
24
- output, # type: queue.Queue[ta.Sequence[JournalctlMessage]]
25
- *,
26
- since: ta.Optional[str] = None,
27
- after_cursor: ta.Optional[str] = None,
28
-
29
- cmd: ta.Optional[ta.Sequence[str]] = None,
30
- shell_wrap: bool = False,
31
-
32
- read_size: int = 0x4000,
33
- sleep_s: float = 1.,
34
-
35
- **kwargs: ta.Any,
36
- ) -> None:
37
- super().__init__(**kwargs)
38
-
39
- self._output = output
40
-
41
- self._since = since
42
- self._after_cursor = after_cursor
43
-
44
- self._cmd = cmd or self.DEFAULT_CMD
45
- self._shell_wrap = shell_wrap
46
-
47
- self._read_size = read_size
48
- self._sleep_s = sleep_s
49
-
50
- self._mb = JournalctlMessageBuilder()
51
-
52
- self._proc: ta.Optional[subprocess.Popen] = None
53
-
54
- @cached_nullary
55
- def _full_cmd(self) -> ta.Sequence[str]:
56
- cmd = [
57
- *self._cmd,
58
- '--output', 'json',
59
- '--show-cursor',
60
- '--follow',
61
- ]
62
-
63
- if self._since is not None:
64
- cmd.extend(['--since', self._since])
65
-
66
- if self._after_cursor is not None:
67
- cmd.extend(['--after-cursor', self._after_cursor])
68
-
69
- if self._shell_wrap:
70
- cmd = list(subprocess_shell_wrap_exec(*cmd))
71
-
72
- return cmd
73
-
74
- def _run(self) -> None:
75
- with subprocess.Popen(
76
- self._full_cmd(),
77
- stdout=subprocess.PIPE,
78
- ) as self._proc:
79
- stdout = check_not_none(self._proc.stdout)
80
-
81
- fd = stdout.fileno()
82
- fl = fcntl.fcntl(fd, fcntl.F_GETFL)
83
- fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
84
-
85
- while True:
86
- if not self._heartbeat():
87
- break
88
-
89
- while stdout.readable():
90
- if not self._heartbeat():
91
- break
92
-
93
- buf = stdout.read(self._read_size)
94
- if not buf:
95
- log.debug('Journalctl empty read')
96
- break
97
-
98
- log.debug('Journalctl read buffer: %r', buf)
99
- msgs = self._mb.feed(buf)
100
- if msgs:
101
- self._output.put(msgs)
102
-
103
- if self._proc.poll() is not None:
104
- log.critical('Journalctl process terminated')
105
- break
106
-
107
- log.debug('Journalctl readable')
108
- time.sleep(self._sleep_s)