Repository navigation
Expand file tree
/
Copy pathargs.py
More file actions
221 lines (211 loc) · 7.02 KB
/
Copy pathargs.py
File metadata and controls
221 lines (211 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
from dataclasses import dataclass, field
from typing import List, Optional, Sequence, Tuple
from taskiq.acks import AcknowledgeType
from taskiq.cli.common_args import LogLevel
def receiver_arg_type(string: str) -> Tuple[str, str]:
"""
Parse cli --receiver_arg argument value.
:param string: cli argument value in format key=value.
:raises ValueError: if value not in format.
:return: (key, value) pair.
"""
args = string.split("=", 1)
if len(args) != 2:
raise ValueError(f"Invalid value: {string}")
return args[0], args[1]
@dataclass
class WorkerArgs:
"""Taskiq worker CLI arguments."""
broker: str
modules: List[str]
tasks_pattern: Sequence[str] = ("**/tasks.py",)
fs_discover: bool = False
configure_logging: bool = True
log_level: LogLevel = LogLevel.INFO
workers: int = 2
max_threadpool_threads: int = 10
no_parse: bool = False
shutdown_timeout: float = 5
reload: bool = False
no_gitignore: bool = False
max_async_tasks: int = 100
receiver: str = "taskiq.receiver:Receiver"
receiver_arg: List[Tuple[str, str]] = field(default_factory=list)
max_prefetch: int = 0
no_propagate_errors: bool = False
max_fails: int = -1
ack_type: AcknowledgeType = AcknowledgeType.WHEN_SAVED
max_tasks_per_child: Optional[int] = None
wait_tasks_timeout: Optional[float] = None
@classmethod
def from_cli(
cls,
args: Optional[Sequence[str]] = None,
) -> "WorkerArgs":
"""
Construct TaskiqArgs instanc from CLI arguments.
:param args: list of args as for cli.
:return: TaskiqArgs instance.
"""
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument(
"broker",
help=(
"Where to search for broker. "
"This string must be specified in "
"'module.module:variable' format."
),
)
parser.add_argument(
"--receiver",
default="taskiq.receiver:Receiver",
help=(
"Where to search for receiver. "
"This string must be specified in "
"'module.module:variable' format."
),
)
parser.add_argument(
"--receiver_arg",
action="append",
type=receiver_arg_type,
default=[],
help=(
"List of args for receiver. "
"This string must be specified in "
"`key=value` format."
),
)
parser.add_argument(
"--tasks-pattern",
"-tp",
default=["**/tasks.py"],
action="append",
help="Glob patterns of files in which taskiq will try to find the tasks.",
)
parser.add_argument(
"modules",
help="List of modules where to look for tasks.",
nargs="*",
)
parser.add_argument(
"--fs-discover",
"-fsd",
action="store_true",
help=(
"If this option is on, "
"taskiq will try to find tasks modules "
"in current directory recursively. Name of file to search for "
"can be configured using `--tasks-pattern` option."
),
)
parser.add_argument(
"--log-level",
default="INFO",
choices=[level.name for level in LogLevel],
help="worker log level",
)
parser.add_argument(
"--workers",
"-w",
type=int,
default=2,
help="Number of worker child processes",
)
parser.add_argument(
"--no-parse",
action="store_true",
help=(
"If this parameter is on,"
" taskiq doesn't parse incoming parameters "
" with pydantic."
),
)
parser.add_argument(
"--no-propagate-errors",
action="store_true",
dest="no_propagate_errors",
help=(
"If this parameter is on,"
" all errors that happen in tasks "
" won't be propagated to generator dependencies."
),
)
parser.add_argument(
"--max-threadpool-threads",
type=int,
help="Maximum number of threads for executing sync functions.",
)
parser.add_argument(
"--shutdown-timeout",
type=float,
default=5,
help="Maximum amount of time for graceful broker's shutdown is seconds.",
)
parser.add_argument(
"--reload",
"-r",
action="store_true",
help="Reload workers if file is changed. "
"`reload` extra is required for this option.",
)
parser.add_argument(
"--do-not-use-gitignore",
action="store_true",
dest="no_gitignore",
help="Do not use gitignore to check for updated files.",
)
parser.add_argument(
"--max-async-tasks",
type=int,
dest="max_async_tasks",
default=100,
help="Maximum simultaneous async tasks per worker process. ",
)
parser.add_argument(
"--max-prefetch",
type=int,
dest="max_prefetch",
default=0,
help="Maximum prefetched tasks per worker process. ",
)
parser.add_argument(
"--no-configure-logging",
action="store_false",
dest="configure_logging",
help="Use this parameter if your application configures custom logging.",
)
parser.add_argument(
"--max-fails",
type=int,
dest="max_fails",
default=-1,
help="Maximum number of child process exits.",
)
parser.add_argument(
"--ack-type",
type=lambda value: AcknowledgeType(value.lower()),
default=AcknowledgeType.WHEN_SAVED,
choices=[ack_type.name.lower() for ack_type in AcknowledgeType],
help="When to acknowledge message.",
)
parser.add_argument(
"--max-tasks-per-child",
type=int,
default=None,
help="Maximum number of tasks to execute per child process.",
)
parser.add_argument(
"--wait-tasks-timeout",
type=float,
default=None,
help="Maximum time to wait for all current tasks "
"to finish before exiting.",
)
namespace = parser.parse_args(args)
# If there are any patterns specified, remove default.
# This is an argparse limitation.
if len(namespace.tasks_pattern) > 1:
namespace.tasks_pattern.pop(0)
return WorkerArgs(**namespace.__dict__)