Skip to content

Commit 26485ff

Browse files
authored
Add --reload-dir argument (#459)
1 parent 2620fc3 commit 26485ff

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

taskiq/cli/watcher.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ class FileWatcher: # pragma: no cover
1414
def __init__(
1515
self,
1616
callback: Callable[..., None],
17+
path: Path,
1718
use_gitignore: bool = True,
1819
**callback_kwargs: Any,
1920
) -> None:
2021
self.callback = callback
2122
self.gitignore = None
22-
gpath = Path("./.gitignore")
23+
gpath = path / ".gitignore"
2324
if use_gitignore and gpath.exists():
2425
self.gitignore = parse_gitignore(gpath)
2526
self.callback_kwargs = callback_kwargs

taskiq/cli/worker/args.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class WorkerArgs:
3939
no_parse: bool = False
4040
shutdown_timeout: float = 5
4141
reload: bool = False
42+
reload_dirs: List[str] = field(default_factory=list)
4243
no_gitignore: bool = False
4344
max_async_tasks: int = 100
4445
receiver: str = "taskiq.receiver:Receiver"
@@ -172,6 +173,16 @@ def from_cli(
172173
help="Reload workers if file is changed. "
173174
"`reload` extra is required for this option.",
174175
)
176+
parser.add_argument(
177+
"--reload-dir",
178+
action="append",
179+
dest="reload_dirs",
180+
default=[],
181+
help=(
182+
"Specify a directory to watch for changes. Can be specified "
183+
"multiple times. Defaults to the current working directory."
184+
),
185+
)
175186
parser.add_argument(
176187
"--do-not-use-gitignore",
177188
action="store_true",

taskiq/cli/worker/process_manager.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from dataclasses import dataclass
77
from multiprocessing import Event, Process, Queue, current_process
88
from multiprocessing.synchronize import Event as EventType
9+
from pathlib import Path
910
from time import sleep
1011
from typing import Any, Callable, List, Optional
1112

@@ -163,15 +164,19 @@ def __init__(
163164
self.action_queue: "Queue[ProcessActionBase]" = Queue(-1)
164165
self.args = args
165166
if args.reload and observer is not None:
166-
observer.schedule(
167-
FileWatcher(
168-
callback=schedule_workers_reload,
169-
use_gitignore=not args.no_gitignore,
170-
action_queue=self.action_queue,
171-
),
172-
path=".",
173-
recursive=True,
174-
)
167+
watch_paths = args.reload_dirs if args.reload_dirs else ["."]
168+
for path_to_watch in watch_paths:
169+
logger.debug(f"Watching directory: {path_to_watch}")
170+
observer.schedule(
171+
FileWatcher(
172+
callback=schedule_workers_reload,
173+
path=Path(path_to_watch),
174+
use_gitignore=not args.no_gitignore,
175+
action_queue=self.action_queue,
176+
),
177+
path=path_to_watch,
178+
recursive=True,
179+
)
175180

176181
shutdown_handler = get_signal_handler(self.action_queue, ShutdownAction())
177182
signal.signal(signal.SIGINT, shutdown_handler)

0 commit comments

Comments
 (0)