Skip to content

dispatcher

dispatcher

common

JobState dataclass

The state of a job which keeps its state (see RedisStateProtocol) as it was read from the job hash. It gives the dispatcher one thing to hand over instead of the raw redis fields.

Attributes:

  • id (str) –

    The unique identifier of the job.

  • status (Status) –

    The current status of the job.

  • last_update (str | None) –

    ISO timestamp of the last status change.

  • result (JobResult | None) –

    The result of a succeeded job.

  • message (str | None) –

    The error message of a failed job, or why a dismissed one stopped.

  • progress (int | None) –

    The percentage (0-100) the job reported last, None if it reported none (yet).

Source code in src/qgis_server_light/interface/dispatcher/common.py
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
@dataclass
class JobState:
    """The state of a job which keeps its state (see `RedisStateProtocol`) as
    it was read from the job hash. It gives the dispatcher one thing to hand
    over instead of the raw redis fields.

    Attributes:
        id: The unique identifier of the job.
        status: The current status of the job.
        last_update: ISO timestamp of the last status change.
        result: The result of a succeeded job.
        message: The error message of a failed job, or why a dismissed one stopped.
        progress: The percentage (0-100) the job reported last, `None` if it
            reported none (yet).
    """

    id: str = field(metadata={"type": "Element"})
    status: Status = field(metadata={"type": "Element"})
    last_update: str | None = field(default=None, metadata={"type": "Element"})
    result: JobResult | None = field(default=None, metadata={"type": "Element"})
    message: str | None = field(default=None, metadata={"type": "Element"})
    progress: int | None = field(default=None, metadata={"type": "Element"})

    @property
    def finished(self) -> bool:
        """If the job reached a final status, nothing changes on it anymore."""
        return self.status in (Status.SUCCESS, Status.FAILURE, Status.DISMISSED)
finished: bool property

If the job reached a final status, nothing changes on it anymore.

id: str = field(metadata={'type': 'Element'}) class-attribute instance-attribute
last_update: str | None = field(default=None, metadata={'type': 'Element'}) class-attribute instance-attribute
message: str | None = field(default=None, metadata={'type': 'Element'}) class-attribute instance-attribute
progress: int | None = field(default=None, metadata={'type': 'Element'}) class-attribute instance-attribute
result: JobResult | None = field(default=None, metadata={'type': 'Element'}) class-attribute instance-attribute
status: Status = field(metadata={'type': 'Element'}) class-attribute instance-attribute
__init__(id: str, status: Status, last_update: str | None = None, result: JobResult | None = None, message: str | None = None, progress: int | None = None) -> None

Status

Bases: Enum

Source code in src/qgis_server_light/interface/dispatcher/common.py
 9
10
11
12
13
14
class Status(Enum):
    SUCCESS = "succeed"
    FAILURE = "failed"
    RUNNING = "running"
    QUEUED = "queued"
    DISMISSED = "dismissed"
DISMISSED = 'dismissed' class-attribute instance-attribute
FAILURE = 'failed' class-attribute instance-attribute
QUEUED = 'queued' class-attribute instance-attribute
RUNNING = 'running' class-attribute instance-attribute
SUCCESS = 'succeed' class-attribute instance-attribute

Worker dataclass

A worker the dispatcher has seen alive on the queue. It carries what a consumer needs to tell whether jobs can be served and what the worker is doing right now.

Attributes:

  • id (str) –

    The unique identifier of the worker.

  • last_seen (datetime) –

    The time of the last heartbeat of the worker, always timezone aware.

  • info (EngineInfo) –

    The info the worker published with its last heartbeat (status, QGIS version, start time).

Source code in src/qgis_server_light/interface/dispatcher/common.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@dataclass
class Worker:
    """A worker the dispatcher has seen alive on the queue. It carries what a
    consumer needs to tell whether jobs can be served and what the worker is doing
    right now.

    Attributes:
        id: The unique identifier of the worker.
        last_seen: The time of the last heartbeat of the worker, always timezone aware.
        info: The info the worker published with its last heartbeat (status, QGIS
            version, start time).
    """

    id: str
    last_seen: datetime
    info: EngineInfo
id: str instance-attribute
info: EngineInfo instance-attribute
last_seen: datetime instance-attribute
__init__(id: str, last_seen: datetime, info: EngineInfo) -> None

protocol

This contains the protocol definition about how a job is passed around a redis queue. It is shared by the dispatcher and the worker and therefore must not depend on anything else than the standard library.

RedisProtocol

Names of the redis queue, the job hash fields and the notification channel a job is exchanged with. It is the common ground of both ways a job result is delivered, the concrete queue is defined by the subclasses RedisPublishProtocol and RedisStateProtocol.

A job is put into redis as hash under job:{id} (see job_key) and its id is pushed onto the list returned by queue_name. A worker listens on the queue of the protocol it is built on. The hash holds the fields:

  • info: The job info serialized as JSON.
  • info_type: The class name of the job info, used to parse info.
  • status: The current Status value of the job.
  • timestamp.{status}: ISO timestamp when the job entered that status.
  • timestamp.last_update: ISO timestamp of the last status change.
  • duration: Seconds since the job was posted, as string.

The channel notifications:{id} (see channel) is used to tell a waiting dispatcher that the job is finished. What is sent over it depends on the subclass.

A worker registers itself as hash under worker:{id} (see worker_key) and adds its id to the set worker_list_name. The hash holds the fields:

  • info: The engine info serialized as JSON.
  • last_seen: ISO timestamp (UTC) of the last heartbeat.
  • processes: The descriptions of the processes a process-worker serves (ProcessDescriptions) as JSON, only written by process-workers.

Attributes:

Source code in src/qgis_server_light/interface/dispatcher/protocol.py
  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
class RedisProtocol:
    """Names of the redis queue, the job hash fields and the notification
    channel a job is exchanged with. It is the common ground of both ways a
    job result is delivered, the concrete queue is defined by the subclasses
    `RedisPublishProtocol` and `RedisStateProtocol`.

    A job is put into redis as hash under `job:{id}` (see `job_key`) and its
    id is pushed onto the list returned by `queue_name`. A worker listens on
    the queue of the protocol it is built on. The hash holds the fields:

    - `info`: The job info serialized as JSON.
    - `info_type`: The class name of the job info, used to parse `info`.
    - `status`: The current `Status` value of the job.
    - `timestamp.{status}`: ISO timestamp when the job entered that status.
    - `timestamp.last_update`: ISO timestamp of the last status change.
    - `duration`: Seconds since the job was posted, as string.

    The channel `notifications:{id}` (see `channel`) is used to tell a waiting
    dispatcher that the job is finished. What is sent over it depends on the
    subclass.

    A worker registers itself as hash under `worker:{id}` (see `worker_key`) and
    adds its id to the set `worker_list_name`. The hash holds the fields:

    - `info`: The engine info serialized as JSON.
    - `last_seen`: ISO timestamp (UTC) of the last heartbeat.
    - `processes`: The descriptions of the processes a process-worker serves
      (`ProcessDescriptions`) as JSON, only written by process-workers.

    Attributes:
        job_queue_name: The name of the redis list the ids of the jobs are
            queued on. Set by the subclasses.
        job_info_key: The hash field holding the serialized job info.
        job_info_type_key: The hash field holding the class name of the job info.
        job_channel_name: The prefix of the channel a finished job is notified on.
        job_status_key: The hash field holding the job status.
        job_duration_key: The hash field holding the job duration.
        job_timestamp_key: The prefix of the hash fields holding the timestamps.
        job_last_update_key: The hash field holding the timestamp of the last update.
        worker_list_name: The name of the redis set the worker ids are registered in.
        worker_info_key: The hash field holding the serialized engine info.
        worker_last_seen_key: The hash field holding the timestamp of the last heartbeat.
        worker_heartbeat_interval: Seconds between two heartbeats of a worker.
        worker_alive_age: Seconds a heartbeat may be old for the worker to count as alive.
        worker_processes_key: The hash field holding the descriptions of the processes
            the worker serves.
    """

    job_queue_name: str
    job_info_key: str = "info"
    job_info_type_key: str = "info_type"
    job_channel_name: str = "notifications"
    job_status_key: str = "status"
    job_duration_key: str = "duration"
    job_timestamp_key: str = "timestamp"
    job_last_update_key: str = f"{job_timestamp_key}.last_update"
    worker_list_name: str = "workers"
    worker_info_key: str = "info"
    worker_last_seen_key: str = "last_seen"
    worker_heartbeat_interval: int = 1
    worker_alive_age: int = 10 * worker_heartbeat_interval
    worker_processes_key: str = "processes"

    @staticmethod
    def job_key(job_id: str) -> str:
        """The key of the redis hash holding the job.

        Args:
            job_id: The unique identifier of the job.
        """
        return f"job:{job_id}"

    @classmethod
    def queue_name(cls) -> str:
        """The name of the queue the jobs of this protocol are queued on. It is
        a method on purpose, so the place where the name is decided stays the
        same if it ever needs to be assembled."""
        return cls.job_queue_name

    @classmethod
    def channel(cls, job_id: str) -> str:
        """The channel a finished job is notified on.

        Args:
            job_id: The unique identifier of the job.
        """
        return f"{cls.job_channel_name}:{job_id}"

    @staticmethod
    def worker_key(worker_id: str) -> str:
        """The key of the redis hash holding the worker registration.

        Args:
            worker_id: The unique identifier of the worker.
        """
        return f"worker:{worker_id}"
job_channel_name: str = 'notifications' class-attribute instance-attribute
job_duration_key: str = 'duration' class-attribute instance-attribute
job_info_key: str = 'info' class-attribute instance-attribute
job_info_type_key: str = 'info_type' class-attribute instance-attribute
job_last_update_key: str = f'{job_timestamp_key}.last_update' class-attribute instance-attribute
job_queue_name: str instance-attribute
job_status_key: str = 'status' class-attribute instance-attribute
job_timestamp_key: str = 'timestamp' class-attribute instance-attribute
worker_alive_age: int = 10 * worker_heartbeat_interval class-attribute instance-attribute
worker_heartbeat_interval: int = 1 class-attribute instance-attribute
worker_info_key: str = 'info' class-attribute instance-attribute
worker_last_seen_key: str = 'last_seen' class-attribute instance-attribute
worker_list_name: str = 'workers' class-attribute instance-attribute
worker_processes_key: str = 'processes' class-attribute instance-attribute
channel(job_id: str) -> str classmethod

The channel a finished job is notified on.

Parameters:

  • job_id (str) –

    The unique identifier of the job.

Source code in src/qgis_server_light/interface/dispatcher/protocol.py
87
88
89
90
91
92
93
94
@classmethod
def channel(cls, job_id: str) -> str:
    """The channel a finished job is notified on.

    Args:
        job_id: The unique identifier of the job.
    """
    return f"{cls.job_channel_name}:{job_id}"
job_key(job_id: str) -> str staticmethod

The key of the redis hash holding the job.

Parameters:

  • job_id (str) –

    The unique identifier of the job.

Source code in src/qgis_server_light/interface/dispatcher/protocol.py
71
72
73
74
75
76
77
78
@staticmethod
def job_key(job_id: str) -> str:
    """The key of the redis hash holding the job.

    Args:
        job_id: The unique identifier of the job.
    """
    return f"job:{job_id}"
queue_name() -> str classmethod

The name of the queue the jobs of this protocol are queued on. It is a method on purpose, so the place where the name is decided stays the same if it ever needs to be assembled.

Source code in src/qgis_server_light/interface/dispatcher/protocol.py
80
81
82
83
84
85
@classmethod
def queue_name(cls) -> str:
    """The name of the queue the jobs of this protocol are queued on. It is
    a method on purpose, so the place where the name is decided stays the
    same if it ever needs to be assembled."""
    return cls.job_queue_name
worker_key(worker_id: str) -> str staticmethod

The key of the redis hash holding the worker registration.

Parameters:

  • worker_id (str) –

    The unique identifier of the worker.

Source code in src/qgis_server_light/interface/dispatcher/protocol.py
 96
 97
 98
 99
100
101
102
103
@staticmethod
def worker_key(worker_id: str) -> str:
    """The key of the redis hash holding the worker registration.

    Args:
        worker_id: The unique identifier of the worker.
    """
    return f"worker:{worker_id}"

RedisPublishProtocol

Bases: RedisProtocol

The protocol of short living jobs a dispatcher waits for, like rendering a map or a legend. The worker publishes the pickled JobResult over the channel and the dispatcher deletes the job hash after it has received it. Nothing is kept, a result is only there for the one who is listening.

Attributes:

  • job_queue_name (str) –

    The name of the redis list the ids of the jobs are queued on.

Source code in src/qgis_server_light/interface/dispatcher/protocol.py
106
107
108
109
110
111
112
113
114
115
116
117
118
class RedisPublishProtocol(RedisProtocol):
    """The protocol of short living jobs a dispatcher waits for, like
    rendering a map or a legend. The worker publishes the pickled `JobResult`
    over the channel and the dispatcher deletes the job hash after it has
    received it. Nothing is kept, a result is only there for the one who is
    listening.

    Attributes:
        job_queue_name: The name of the redis list the ids of the jobs are
            queued on.
    """

    job_queue_name: str = "jobs"
job_queue_name: str = 'jobs' class-attribute instance-attribute

RedisStateProtocol

Bases: RedisProtocol

The protocol of jobs which keep their state, these are the processes. The worker writes the result (or the error message) into the job hash in the same transaction as the final status, so whoever reads the hash never sees a finished job without its result. The hash expires after job_expire seconds, it is the only place the result lives.

Only the status value is sent over the channel and only if the job was submitted with ExecutionMode.SYNC. It just wakes the waiting dispatcher, which then reads the job hash. Asynchronous jobs are polled, nobody waits for a notification there.

Processes are queued on their own list, so long-running processes never block map related jobs.

Additional hash fields:

  • result: The data of the JobResult of a succeeded job.
  • result_content_type: The content type of result.
  • message: The error message of a failed job.
  • progress: The percentage (0-100) the running job reported, only if it reports any. It is written while the job runs, outside the transaction.
  • cancel: Set by the dispatcher to dismiss a job the worker took already. The worker cancels the job and finishes it as dismissed.

Attributes:

  • job_queue_name (str) –

    The name of the redis list the ids of the jobs are queued on.

  • job_result_key (str) –

    The hash field holding the result data.

  • job_result_content_type_key (str) –

    The hash field holding the content type of the result data.

  • job_message_key (str) –

    The hash field holding the error message.

  • job_progress_key (str) –

    The hash field holding the progress in percent.

  • job_cancel_key (str) –

    The hash field asking the worker to cancel the job.

  • job_expire (int) –

    Seconds the job hash is kept after it was submitted and again after it is finished.

Source code in src/qgis_server_light/interface/dispatcher/protocol.py
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
class RedisStateProtocol(RedisProtocol):
    """The protocol of jobs which keep their state, these are the processes.
    The worker writes the result (or the error message) into the job hash in
    the same transaction as the final status, so whoever reads the hash never
    sees a finished job without its result. The hash expires after
    `job_expire` seconds, it is the only place the result lives.

    Only the status value is sent over the channel and only if the job was
    submitted with `ExecutionMode.SYNC`. It just wakes the waiting dispatcher,
    which then reads the job hash. Asynchronous jobs are polled, nobody waits
    for a notification there.

    Processes are queued on their own list, so long-running processes never
    block map related jobs.

    Additional hash fields:

    - `result`: The data of the `JobResult` of a succeeded job.
    - `result_content_type`: The content type of `result`.
    - `message`: The error message of a failed job.
    - `progress`: The percentage (0-100) the running job reported, only if it
      reports any. It is written while the job runs, outside the transaction.
    - `cancel`: Set by the dispatcher to dismiss a job the worker took already.
      The worker cancels the job and finishes it as `dismissed`.

    Attributes:
        job_queue_name: The name of the redis list the ids of the jobs are
            queued on.
        job_result_key: The hash field holding the result data.
        job_result_content_type_key: The hash field holding the content type
            of the result data.
        job_message_key: The hash field holding the error message.
        job_progress_key: The hash field holding the progress in percent.
        job_cancel_key: The hash field asking the worker to cancel the job.
        job_expire: Seconds the job hash is kept after it was submitted and
            again after it is finished.
    """

    job_queue_name: str = "jobs:process"
    job_result_key: str = "result"
    job_result_content_type_key: str = "result_content_type"
    job_message_key: str = "message"
    job_progress_key: str = "progress"
    job_cancel_key: str = "cancel"
    job_expire: int = 86400
job_cancel_key: str = 'cancel' class-attribute instance-attribute
job_expire: int = 86400 class-attribute instance-attribute
job_message_key: str = 'message' class-attribute instance-attribute
job_progress_key: str = 'progress' class-attribute instance-attribute
job_queue_name: str = 'jobs:process' class-attribute instance-attribute
job_result_content_type_key: str = 'result_content_type' class-attribute instance-attribute
job_result_key: str = 'result' class-attribute instance-attribute