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

Add factories #349

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion taskiq/brokers/inmemory_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async def set_progress(
progress: TaskProgress[Any],
) -> None:
"""
Set progress of task exection.
Set progress of task execution.

:param task_id: task id
:param progress: task execution progress
Expand Down
5 changes: 4 additions & 1 deletion taskiq/cli/scheduler/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ def from_cli(cls, args: Optional[Sequence[str]] = None) -> "SchedulerArgs":
formatter_class=ArgumentDefaultsHelpFormatter,
description="Subcommand to run scheduler",
)
parser.add_argument("scheduler", help="Path to scheduler")
parser.add_argument(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need two parameters. Specify only factory. We can check if the imported scheduler is a factory after we import it.

"scheduler",
help="Path to scheduler or scheduler factory function",
)
parser.add_argument(
"modules",
help="List of modules where to look for tasks.",
Expand Down
5 changes: 5 additions & 0 deletions taskiq/cli/scheduler/run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import inspect
import sys
from datetime import datetime, timedelta
from logging import basicConfig, getLevelName, getLogger
Expand Down Expand Up @@ -189,15 +190,19 @@ async def run_scheduler(args: SchedulerArgs) -> None:
),
)
getLogger("taskiq").setLevel(level=getLevelName(args.log_level))

if isinstance(args.scheduler, str):
scheduler = import_object(args.scheduler)
if inspect.isfunction(scheduler):
scheduler = scheduler()
else:
scheduler = args.scheduler
if not isinstance(scheduler, TaskiqScheduler):
logger.error(
"Imported scheduler is not a subclass of TaskiqScheduler.",
)
sys.exit(1)

scheduler.broker.is_scheduler_process = True
import_tasks(args.modules, args.tasks_pattern, args.fs_discover)
for source in scheduler.sources:
Expand Down
2 changes: 1 addition & 1 deletion taskiq/cli/worker/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def from_cli(
parser.add_argument(
"broker",
help=(
"Where to search for broker. "
"Where to search for broker or broker factory function. "
"This string must be specified in "
"'module.module:variable' format."
),
Expand Down
10 changes: 9 additions & 1 deletion taskiq/cli/worker/run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import inspect
import logging
import os
import signal
Expand Down Expand Up @@ -121,9 +122,16 @@ def interrupt_handler(signum: int, _frame: Any) -> None:
# broker is running as a worker.
# We must set this field before importing tasks,
# so broker will remember all tasks it's related to.

broker = import_object(args.broker)
if inspect.isfunction(broker):
broker = broker()
if not isinstance(broker, AsyncBroker):
raise ValueError("Unknown broker type. Please use AsyncBroker instance.")
raise ValueError(
"Unknown broker type. Please use AsyncBroker instance "
"or pass broker factory function that returns an AsyncBroker instance.",
)

broker.is_worker_process = True
import_tasks(args.modules, args.tasks_pattern, args.fs_discover)

Expand Down