Skip to content

BaseStarter - Пишем свой Стартер

Base starter.

BaseStarter

Bases: Generic[TAsyncFlag], ABC

BaseStarter - An abstract class that is the foundation for Starters.

Example

from qtasks import QueueTasks
from qtasks.starters.base import BaseStarter

class MyStarter(BaseStarter):
    def __init__(self, name: str = None, broker = None, worker = None):
        super().__init__(name=name, broker = None, worker = None)
        pass
Source code in src/qtasks/starters/base.py
 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
class BaseStarter(Generic[TAsyncFlag], ABC):
    """
    `BaseStarter` - An abstract class that is the foundation for Starters.

    ## Example

    ```python
    from qtasks import QueueTasks
    from qtasks.starters.base import BaseStarter

    class MyStarter(BaseStarter):
        def __init__(self, name: str = None, broker = None, worker = None):
            super().__init__(name=name, broker = None, worker = None)
            pass
    ```
    """

    def __init__(
        self,
        name: Annotated[
            str | None,
            Doc("""
                    Project name. This name can be used for tags for Starters.

                    Default: `None`.
                    """),
        ] = None,
        broker: Annotated[
            Optional[BaseBroker],
            Doc("""
                    Broker.

                    Default: `None`.
                    """),
        ] = None,
        worker: Annotated[
            Optional[BaseWorker],
            Doc("""
                    Worker.

                    Default: `None`.
                    """),
        ] = None,
        log: Annotated[
            Logger | None,
            Doc("""
                    Logger.

                    Default: `qtasks.logs.Logger`.
                    """),
        ] = None,
        config: Annotated[
            QueueConfig | None,
            Doc("""
                    Config.

                    Default: `qtasks.configs.config.QueueConfig`.
                    """),
        ] = None,
        events: Annotated[
            Optional[BaseEvents],
            Doc("""
                    Events.

                    Default: `None`.
                    """),
        ] = None,
    ):
        """
        Basic starter initialization.

        Args:
            name (str, optional): Project name. Default: None.
            broker (BaseBroker, optional): Broker. Default: None.
            worker (BaseWorker, optional): Worker. Default: None.
            log (Logger, optional): Logger. Default: `qtasks.logs.Logger`.
            config (QueueConfig, optional): Config. Default: `qtasks.configs.config.QueueConfig`.
            events (BaseEvents, optional): Events. Default: `None`.
        """
        self.name = name
        self.config = config or QueueConfig()
        self.log = (
            log.with_subname("Starter")
            if log
            else Logger(
                name=self.name or "QueueTasks",
                subname="Starter",
                default_level=self.config.logs_default_level_server,
                format=self.config.logs_format,
            )
        )
        self.events = events

        self.broker = broker
        self.worker = worker

        self.plugins: dict[str, list[BasePlugin]] = {}

        self.init_plugins()

    @overload
    def start(self: BaseStarter[Literal[False]], *args, **kwargs) -> None: ...

    @overload
    def start(self: BaseStarter[Literal[True]], *args, **kwargs) -> None: ...

    @abstractmethod
    def start(self, *args, **kwargs) -> None:
        """Starter launch. This function is enabled by the main `QueueTasks` instance via `run_forever`."""
        pass

    @overload
    def stop(self: BaseStarter[Literal[False]]) -> None: ...

    @overload
    async def stop(self: BaseStarter[Literal[True]]) -> None: ...

    @abstractmethod
    def stop(self) -> None | Awaitable[None]:
        """Stops the Starter. This function is invoked by the main `QueueTasks` instance after the `run_forever` function completes."""
        pass

    def add_plugin(
        self,
        plugin: Annotated[
            BasePlugin,
            Doc("""
                    Plugin.
                    """),
        ],
        trigger_names: Annotated[
            list[str] | None,
            Doc("""
                    The name of the triggers for the plugin.

                    Default: Default: will be added to `Globals`.
                    """),
        ] = None,
    ) -> None:
        """
        Add a plugin to the class.

        Args:
            plugin (BasePlugin): Plugin
            trigger_names (List[str], optional): The name of the triggers for the plugin. Default: will be added to `Globals`.
        """
        trigger_names = trigger_names or ["Globals"]

        for name in trigger_names:
            if name not in self.plugins:
                self.plugins.update({name: [plugin]})
            else:
                self.plugins[name].append(plugin)
        return

    def update_configs(
        self,
        config: Annotated[
            QueueConfig,
            Doc("""
                    Config.
                    """),
        ],
    ):
        """
        Update configs for all components.

        Args:
            config (QueueConfig): Config.
        """
        self.log.debug("The config has been updated")
        if self.worker:
            self.worker.update_config(config)
        if self.broker:
            self.broker.update_config(config)
            if self.broker.storage:
                self.broker.storage.update_config(config)
                if self.broker.storage.global_config:
                    self.broker.storage.global_config.update_config(config)

    def init_plugins(self):
        """Initializing plugins."""
        pass

__init__(name=None, broker=None, worker=None, log=None, config=None, events=None)

Basic starter initialization.

Parameters:

Name Type Description Default
name str

Project name. Default: None.

None
broker BaseBroker

Broker. Default: None.

None
worker BaseWorker

Worker. Default: None.

None
log Logger

Logger. Default: qtasks.logs.Logger.

None
config QueueConfig

Config. Default: qtasks.configs.config.QueueConfig.

None
events BaseEvents

Events. Default: None.

None
Source code in src/qtasks/starters/base.py
 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
def __init__(
    self,
    name: Annotated[
        str | None,
        Doc("""
                Project name. This name can be used for tags for Starters.

                Default: `None`.
                """),
    ] = None,
    broker: Annotated[
        Optional[BaseBroker],
        Doc("""
                Broker.

                Default: `None`.
                """),
    ] = None,
    worker: Annotated[
        Optional[BaseWorker],
        Doc("""
                Worker.

                Default: `None`.
                """),
    ] = None,
    log: Annotated[
        Logger | None,
        Doc("""
                Logger.

                Default: `qtasks.logs.Logger`.
                """),
    ] = None,
    config: Annotated[
        QueueConfig | None,
        Doc("""
                Config.

                Default: `qtasks.configs.config.QueueConfig`.
                """),
    ] = None,
    events: Annotated[
        Optional[BaseEvents],
        Doc("""
                Events.

                Default: `None`.
                """),
    ] = None,
):
    """
    Basic starter initialization.

    Args:
        name (str, optional): Project name. Default: None.
        broker (BaseBroker, optional): Broker. Default: None.
        worker (BaseWorker, optional): Worker. Default: None.
        log (Logger, optional): Logger. Default: `qtasks.logs.Logger`.
        config (QueueConfig, optional): Config. Default: `qtasks.configs.config.QueueConfig`.
        events (BaseEvents, optional): Events. Default: `None`.
    """
    self.name = name
    self.config = config or QueueConfig()
    self.log = (
        log.with_subname("Starter")
        if log
        else Logger(
            name=self.name or "QueueTasks",
            subname="Starter",
            default_level=self.config.logs_default_level_server,
            format=self.config.logs_format,
        )
    )
    self.events = events

    self.broker = broker
    self.worker = worker

    self.plugins: dict[str, list[BasePlugin]] = {}

    self.init_plugins()

add_plugin(plugin, trigger_names=None)

Add a plugin to the class.

Parameters:

Name Type Description Default
plugin BasePlugin

Plugin

required
trigger_names List[str]

The name of the triggers for the plugin. Default: will be added to Globals.

None
Source code in src/qtasks/starters/base.py
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
def add_plugin(
    self,
    plugin: Annotated[
        BasePlugin,
        Doc("""
                Plugin.
                """),
    ],
    trigger_names: Annotated[
        list[str] | None,
        Doc("""
                The name of the triggers for the plugin.

                Default: Default: will be added to `Globals`.
                """),
    ] = None,
) -> None:
    """
    Add a plugin to the class.

    Args:
        plugin (BasePlugin): Plugin
        trigger_names (List[str], optional): The name of the triggers for the plugin. Default: will be added to `Globals`.
    """
    trigger_names = trigger_names or ["Globals"]

    for name in trigger_names:
        if name not in self.plugins:
            self.plugins.update({name: [plugin]})
        else:
            self.plugins[name].append(plugin)
    return

init_plugins()

Initializing plugins.

Source code in src/qtasks/starters/base.py
208
209
210
def init_plugins(self):
    """Initializing plugins."""
    pass

start(*args, **kwargs) abstractmethod

start(*args, **kwargs) -> None
start(*args, **kwargs) -> None

Starter launch. This function is enabled by the main QueueTasks instance via run_forever.

Source code in src/qtasks/starters/base.py
134
135
136
137
@abstractmethod
def start(self, *args, **kwargs) -> None:
    """Starter launch. This function is enabled by the main `QueueTasks` instance via `run_forever`."""
    pass

stop() abstractmethod

stop() -> None
stop() -> None

Stops the Starter. This function is invoked by the main QueueTasks instance after the run_forever function completes.

Source code in src/qtasks/starters/base.py
145
146
147
148
@abstractmethod
def stop(self) -> None | Awaitable[None]:
    """Stops the Starter. This function is invoked by the main `QueueTasks` instance after the `run_forever` function completes."""
    pass

update_configs(config)

Update configs for all components.

Parameters:

Name Type Description Default
config QueueConfig

Config.

required
Source code in src/qtasks/starters/base.py
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
def update_configs(
    self,
    config: Annotated[
        QueueConfig,
        Doc("""
                Config.
                """),
    ],
):
    """
    Update configs for all components.

    Args:
        config (QueueConfig): Config.
    """
    self.log.debug("The config has been updated")
    if self.worker:
        self.worker.update_config(config)
    if self.broker:
        self.broker.update_config(config)
        if self.broker.storage:
            self.broker.storage.update_config(config)
            if self.broker.storage.global_config:
                self.broker.storage.global_config.update_config(config)