Skip to content

Base Worker#

In this example, we show how to create a base worker that other workers can inherit from and customize.

Code#

 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
import sergeant


class BaseWorker(
    sergeant.worker.Worker,
):
    def generate_config(
        self,
    ):
        return sergeant.config.WorkerConfig(
            name='base_worker',
            connector=sergeant.config.Connector(
                type='redis',
                params={
                    'nodes': [
                        {
                            'host': 'localhost',
                            'port': 6379,
                            'password': None,
                            'database': 0,
                        },
                    ],
                },
            ),
        )
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import sergeant

from . import base


class Worker(
    base.BaseWorker,
):
    def generate_config(
        self,
    ):
        return super().replace(
            name='derived_worker',
        )

Explanation#

To create a base worker class from which workers can be derived, define the base class object's configuration and use the replace method to copy it with different configurations. replace method was implemented by using dataclasses.replace method, to copy the dataclass with different parameters.