Skip to content

redis_asio

redis_asio

This contains the dispatcher which passes a job info around a redis queue.

RedisDispatcher

Bases: RedisProtocol

The common part of the dispatchers: the connection to redis and how a job is put into it. How the result comes back is up to the subclasses, they are combined with the matching protocol.

Attributes:

Source code in src/qgis_server_light/dispatcher/redis_asio.py
 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
class RedisDispatcher(RedisProtocol):
    """The common part of the dispatchers: the connection to redis and how a job
    is put into it. How the result comes back is up to the subclasses, they are
    combined with the matching protocol.

    Attributes:
        job_info_classes: The job info class per job parameter class, used to
            wrap a job parameter into its job info.
    """

    job_info_classes: ClassVar[dict[type[QslJobParameter], type[QslJobInfoParameter]]] = {
        QslJobParameterRender: QslJobInfoRender,
        QslJobParameterFeatureInfo: QslJobInfoFeatureInfo,
        QslJobParameterLegend: QslJobInfoLegend,
        QslJobParameterFeature: QslJobInfoFeature,
        QslJobParameterCrs: QslJobInfoCrs,
        QslJobParameterExecuteProcess: QslJobInfoExecuteProcess,
    }

    def __init__(self, redis_client: redis_aio.Redis) -> None:
        # we use this to hold connections to redis in a pool, this way we are
        # event loop safe and when creating the redis client for every call of
        # post, we only instantiate a minimal wrapper object which is cheap.

        self.client = redis_client

    @classmethod
    def create(cls, url: str):
        redis_client = redis_aio.Redis.from_url(url)
        return cls(redis_client)

    async def is_reachable(self) -> bool:
        """Pings redis. The caller only needs to know whether jobs could be queued, so
        the redis exceptions stay in here and are logged.

        Returns:
            True if redis answered, False if it could not be reached.
        """
        try:
            return await self.client.ping()
        except RedisError as e:
            logging.warning(f"Redis is not reachable: {e}")
            return False

    async def registered_workers(self) -> list[Worker]:
        """Collects every worker redis still knows, no matter how old its last heartbeat
        is. This way crashed or silently vanished workers can be shown until their info
        expires, the consumer tells them apart by `info.status` and `last_seen`.

        Returns:
            The registered workers, sorted by their id to give a stable order.
        """
        workers = []
        for worker_id in sorted(await self.client.smembers(self.worker_list_name)):
            last_seen, info = await self.client.hmget(
                self.worker_key(worker_id.decode()),
                [self.worker_last_seen_key, self.worker_info_key],
            )
            if last_seen is None or info is None:
                # the worker vanished, its info expired already
                continue
            workers.append(
                Worker(
                    id=worker_id.decode(),
                    last_seen=self.parse_last_seen(last_seen),
                    info=JsonParser().from_bytes(info, EngineInfo),
                )
            )
        return workers

    async def alive_workers(self) -> list[Worker]:
        """Collects the workers which sent a heartbeat within `worker_alive_age` and did
        not crash. Workers which are still registered but stopped beating (e.g. killed)
        or reported a crash are left out, so the result tells who is really there to
        process jobs.

        Returns:
            The alive workers, sorted by their id to give a stable order.
        """
        now = datetime.datetime.now(datetime.UTC)
        alive_age = datetime.timedelta(seconds=self.worker_alive_age)
        return [
            worker
            for worker in await self.registered_workers()
            if now - worker.last_seen <= alive_age and worker.info.status != WorkerStatus.CRASHED
        ]

    @staticmethod
    def parse_last_seen(last_seen: bytes) -> datetime.datetime:
        """Reads the heartbeat timestamp of a worker as it is stored in redis.

        Args:
            last_seen: The ISO timestamp as redis returns it.

        Returns:
            The timezone aware timestamp, naive ones are taken as UTC.
        """
        timestamp = datetime.datetime.fromisoformat(last_seen.decode())
        if timestamp.tzinfo is None:
            # workers before the UTC heartbeat wrote their local time without offset, in
            # containers this is UTC usually
            timestamp = timestamp.replace(tzinfo=datetime.UTC)
        return timestamp

    @classmethod
    def derive_job_info(cls, job_id: str, job_parameter: QslJobParameter) -> QslJobInfoParameter:
        """Wraps the job parameter into the job info matching its type.

        Args:
            job_id: The unique identifier of the job.
            job_parameter: The parameter for the job which should be executed.

        Returns:
            The job info carrying the job parameter.

        Raises:
            TypeError: If there is no job info for the type of the job parameter.
        """
        job_info_class = cls.job_info_classes.get(type(job_parameter))
        if job_info_class is None:
            raise TypeError(f"Unsupported runner type: {type(job_parameter)}")
        return job_info_class(
            id=job_id,
            type=job_info_class.__name__,
            job=job_parameter,
        )

    async def set_job_runtime_status(
        self,
        job_id,
        pipeline: Pipeline,
        status: str,
        start_time: float,
        execute: bool = True,
    ):
        """Sets the status of the job together with its timestamps and duration.

        Args:
            job_id: The unique identifier of the job.
            pipeline: The pipeline holding the transaction.
            status: The status to set.
            start_time: The time the job was posted, used for the duration.
            execute: False if the caller adds more to the transaction and executes it
                itself.
        """
        duration = time.time() - start_time
        ts = datetime.datetime.now().isoformat()
        await pipeline.hset(self.job_key(job_id), self.job_status_key, status)
        await pipeline.hset(
            self.job_key(job_id),
            f"{self.job_timestamp_key}.{status}",
            ts,
        )
        await pipeline.hset(self.job_key(job_id), self.job_last_update_key, ts)
        await pipeline.hset(self.job_key(job_id), self.job_duration_key, str(duration))
        if execute:
            await pipeline.execute()

    async def queue_job(
        self, job_info: QslJobInfoParameter, pipeline: Pipeline, start_time: float
    ) -> None:
        """Puts the job info into the job hash, sets the queued status and pushes
        the job id onto the queue of the protocol. Nothing is executed here, the
        caller adds what belongs to its protocol to the same transaction and
        executes it.

        Args:
            job_info: The job info of the job which should be queued.
            pipeline: The pipeline holding the transaction.
            start_time: The time the job was posted, used for the duration.
        """
        # Putting job info into redis
        await pipeline.hset(
            self.job_key(job_info.id), self.job_info_key, JsonSerializer().render(job_info)
        )
        await pipeline.hset(
            self.job_key(job_info.id), self.job_info_type_key, job_info.__class__.__name__
        )
        # we inform, that the job was queued. This happens in the same
        # transaction as the queuing, so it can not overwrite a status
        # already set by the worker.
        await self.set_job_runtime_status(
            job_info.id, pipeline, Status.QUEUED.value, start_time, execute=False
        )
        # Queuing the job onto the list/queue of the protocol
        await pipeline.rpush(self.queue_name(), job_info.id)

client = redis_client instance-attribute

job_info_classes: dict[type[QslJobParameter], type[QslJobInfoParameter]] = {QslJobParameterRender: QslJobInfoRender, QslJobParameterFeatureInfo: QslJobInfoFeatureInfo, QslJobParameterLegend: QslJobInfoLegend, QslJobParameterFeature: QslJobInfoFeature, QslJobParameterCrs: QslJobInfoCrs, QslJobParameterExecuteProcess: QslJobInfoExecuteProcess} class-attribute

__init__(redis_client: redis_aio.Redis) -> None

Source code in src/qgis_server_light/dispatcher/redis_asio.py
83
84
85
86
87
88
def __init__(self, redis_client: redis_aio.Redis) -> None:
    # we use this to hold connections to redis in a pool, this way we are
    # event loop safe and when creating the redis client for every call of
    # post, we only instantiate a minimal wrapper object which is cheap.

    self.client = redis_client

alive_workers() -> list[Worker] async

Collects the workers which sent a heartbeat within worker_alive_age and did not crash. Workers which are still registered but stopped beating (e.g. killed) or reported a crash are left out, so the result tells who is really there to process jobs.

Returns:

  • list[Worker] –

    The alive workers, sorted by their id to give a stable order.

Source code in src/qgis_server_light/dispatcher/redis_asio.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
async def alive_workers(self) -> list[Worker]:
    """Collects the workers which sent a heartbeat within `worker_alive_age` and did
    not crash. Workers which are still registered but stopped beating (e.g. killed)
    or reported a crash are left out, so the result tells who is really there to
    process jobs.

    Returns:
        The alive workers, sorted by their id to give a stable order.
    """
    now = datetime.datetime.now(datetime.UTC)
    alive_age = datetime.timedelta(seconds=self.worker_alive_age)
    return [
        worker
        for worker in await self.registered_workers()
        if now - worker.last_seen <= alive_age and worker.info.status != WorkerStatus.CRASHED
    ]

create(url: str) classmethod

Source code in src/qgis_server_light/dispatcher/redis_asio.py
90
91
92
93
@classmethod
def create(cls, url: str):
    redis_client = redis_aio.Redis.from_url(url)
    return cls(redis_client)

derive_job_info(job_id: str, job_parameter: QslJobParameter) -> QslJobInfoParameter classmethod

Wraps the job parameter into the job info matching its type.

Parameters:

  • job_id (str) –

    The unique identifier of the job.

  • job_parameter (QslJobParameter) –

    The parameter for the job which should be executed.

Returns:

Raises:

  • TypeError –

    If there is no job info for the type of the job parameter.

Source code in src/qgis_server_light/dispatcher/redis_asio.py
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
@classmethod
def derive_job_info(cls, job_id: str, job_parameter: QslJobParameter) -> QslJobInfoParameter:
    """Wraps the job parameter into the job info matching its type.

    Args:
        job_id: The unique identifier of the job.
        job_parameter: The parameter for the job which should be executed.

    Returns:
        The job info carrying the job parameter.

    Raises:
        TypeError: If there is no job info for the type of the job parameter.
    """
    job_info_class = cls.job_info_classes.get(type(job_parameter))
    if job_info_class is None:
        raise TypeError(f"Unsupported runner type: {type(job_parameter)}")
    return job_info_class(
        id=job_id,
        type=job_info_class.__name__,
        job=job_parameter,
    )

is_reachable() -> bool async

Pings redis. The caller only needs to know whether jobs could be queued, so the redis exceptions stay in here and are logged.

Returns:

  • bool –

    True if redis answered, False if it could not be reached.

Source code in src/qgis_server_light/dispatcher/redis_asio.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
async def is_reachable(self) -> bool:
    """Pings redis. The caller only needs to know whether jobs could be queued, so
    the redis exceptions stay in here and are logged.

    Returns:
        True if redis answered, False if it could not be reached.
    """
    try:
        return await self.client.ping()
    except RedisError as e:
        logging.warning(f"Redis is not reachable: {e}")
        return False

parse_last_seen(last_seen: bytes) -> datetime.datetime staticmethod

Reads the heartbeat timestamp of a worker as it is stored in redis.

Parameters:

  • last_seen (bytes) –

    The ISO timestamp as redis returns it.

Returns:

  • datetime –

    The timezone aware timestamp, naive ones are taken as UTC.

Source code in src/qgis_server_light/dispatcher/redis_asio.py
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
@staticmethod
def parse_last_seen(last_seen: bytes) -> datetime.datetime:
    """Reads the heartbeat timestamp of a worker as it is stored in redis.

    Args:
        last_seen: The ISO timestamp as redis returns it.

    Returns:
        The timezone aware timestamp, naive ones are taken as UTC.
    """
    timestamp = datetime.datetime.fromisoformat(last_seen.decode())
    if timestamp.tzinfo is None:
        # workers before the UTC heartbeat wrote their local time without offset, in
        # containers this is UTC usually
        timestamp = timestamp.replace(tzinfo=datetime.UTC)
    return timestamp

queue_job(job_info: QslJobInfoParameter, pipeline: Pipeline, start_time: float) -> None async

Puts the job info into the job hash, sets the queued status and pushes the job id onto the queue of the protocol. Nothing is executed here, the caller adds what belongs to its protocol to the same transaction and executes it.

Parameters:

  • job_info (QslJobInfoParameter) –

    The job info of the job which should be queued.

  • pipeline (Pipeline) –

    The pipeline holding the transaction.

  • start_time (float) –

    The time the job was posted, used for the duration.

Source code in src/qgis_server_light/dispatcher/redis_asio.py
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
async def queue_job(
    self, job_info: QslJobInfoParameter, pipeline: Pipeline, start_time: float
) -> None:
    """Puts the job info into the job hash, sets the queued status and pushes
    the job id onto the queue of the protocol. Nothing is executed here, the
    caller adds what belongs to its protocol to the same transaction and
    executes it.

    Args:
        job_info: The job info of the job which should be queued.
        pipeline: The pipeline holding the transaction.
        start_time: The time the job was posted, used for the duration.
    """
    # Putting job info into redis
    await pipeline.hset(
        self.job_key(job_info.id), self.job_info_key, JsonSerializer().render(job_info)
    )
    await pipeline.hset(
        self.job_key(job_info.id), self.job_info_type_key, job_info.__class__.__name__
    )
    # we inform, that the job was queued. This happens in the same
    # transaction as the queuing, so it can not overwrite a status
    # already set by the worker.
    await self.set_job_runtime_status(
        job_info.id, pipeline, Status.QUEUED.value, start_time, execute=False
    )
    # Queuing the job onto the list/queue of the protocol
    await pipeline.rpush(self.queue_name(), job_info.id)

registered_workers() -> list[Worker] async

Collects every worker redis still knows, no matter how old its last heartbeat is. This way crashed or silently vanished workers can be shown until their info expires, the consumer tells them apart by info.status and last_seen.

Returns:

  • list[Worker] –

    The registered workers, sorted by their id to give a stable order.

Source code in src/qgis_server_light/dispatcher/redis_asio.py
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
async def registered_workers(self) -> list[Worker]:
    """Collects every worker redis still knows, no matter how old its last heartbeat
    is. This way crashed or silently vanished workers can be shown until their info
    expires, the consumer tells them apart by `info.status` and `last_seen`.

    Returns:
        The registered workers, sorted by their id to give a stable order.
    """
    workers = []
    for worker_id in sorted(await self.client.smembers(self.worker_list_name)):
        last_seen, info = await self.client.hmget(
            self.worker_key(worker_id.decode()),
            [self.worker_last_seen_key, self.worker_info_key],
        )
        if last_seen is None or info is None:
            # the worker vanished, its info expired already
            continue
        workers.append(
            Worker(
                id=worker_id.decode(),
                last_seen=self.parse_last_seen(last_seen),
                info=JsonParser().from_bytes(info, EngineInfo),
            )
        )
    return workers

set_job_runtime_status(job_id, pipeline: Pipeline, status: str, start_time: float, execute: bool = True) async

Sets the status of the job together with its timestamps and duration.

Parameters:

  • job_id –

    The unique identifier of the job.

  • pipeline (Pipeline) –

    The pipeline holding the transaction.

  • status (str) –

    The status to set.

  • start_time (float) –

    The time the job was posted, used for the duration.

  • execute (bool, default: True ) –

    False if the caller adds more to the transaction and executes it itself.

Source code in src/qgis_server_light/dispatcher/redis_asio.py
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
async def set_job_runtime_status(
    self,
    job_id,
    pipeline: Pipeline,
    status: str,
    start_time: float,
    execute: bool = True,
):
    """Sets the status of the job together with its timestamps and duration.

    Args:
        job_id: The unique identifier of the job.
        pipeline: The pipeline holding the transaction.
        status: The status to set.
        start_time: The time the job was posted, used for the duration.
        execute: False if the caller adds more to the transaction and executes it
            itself.
    """
    duration = time.time() - start_time
    ts = datetime.datetime.now().isoformat()
    await pipeline.hset(self.job_key(job_id), self.job_status_key, status)
    await pipeline.hset(
        self.job_key(job_id),
        f"{self.job_timestamp_key}.{status}",
        ts,
    )
    await pipeline.hset(self.job_key(job_id), self.job_last_update_key, ts)
    await pipeline.hset(self.job_key(job_id), self.job_duration_key, str(duration))
    if execute:
        await pipeline.execute()

RedisQueue

Bases: RedisDispatcher, RedisPublishProtocol

The dispatcher for short living jobs a caller waits for. The result is received over the channel and nothing is kept in redis afterwards.

Source code in src/qgis_server_light/dispatcher/redis_asio.py
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
class RedisQueue(RedisDispatcher, RedisPublishProtocol):
    """The dispatcher for short living jobs a caller waits for. The result is
    received over the channel and nothing is kept in redis afterwards.
    """

    async def post(
        self,
        job_parameter: (
            QslJobParameterRender
            | QslJobParameterFeatureInfo
            | QslJobParameterLegend
            | QslJobParameterFeature
            | QslJobParameterCrs
        ),
        to: float = 10.0,
    ) -> tuple[JobResult, str]:
        """
        Posts a new `runner` to the runner queue and waits maximum `timeout` seconds to complete.
        Will return a JobResult if successful or raise an error.

        Args:
            job_parameter: The parameter for the job which should be executed.
            to: The timeout a job is expected to be waited for before canceling
                job execution.
        """
        job_id = str(uuid4())
        start_time = time.time()
        try:
            job_info = self.derive_job_info(job_id, job_parameter)
        except TypeError as e:
            return (
                JobResult(
                    id=job_id,
                    data=str(e),
                    content_type="application/text",
                ),
                Status.FAILURE.value,
            )
        try:
            async with self.client.pubsub() as ps:
                # We subscribe to `notifications:{job_id}` *before* the job is
                # queued. Redis pub/sub does not buffer messages, so a fast job
                # (e.g. a legend) could otherwise be published by the worker
                # before we listen, and its result would be lost.
                await ps.subscribe(self.channel(job_id))
                async with self.client.pipeline() as p:
                    await self.queue_job(job_info, p, start_time)
                    await p.execute()

                logging.info(f"{job_id} queued")
                try:
                    # this puts a timeout trigger on the subscription, after timeout
                    # an asyncio.TimeoutError or asyncio.exceptions.CancelledError
                    # is raised. See except block below.
                    async with timeout(to):
                        while True:
                            message = await ps.get_message(
                                timeout=to, ignore_subscribe_messages=True
                            )
                            if not message:
                                continue  # https://github.com/redis/redis-py/issues/733
                            status_binary = await self.client.hget(
                                self.job_key(job_id), self.job_status_key
                            )
                            status = status_binary.decode()
                            result: JobResult = pickle.loads(message["data"])
                            duration = time.time() - start_time
                            if status == Status.SUCCESS.value:
                                logging.info(
                                    f"Job id: {job_id}, status: {status}, duration: {duration}"
                                )
                            elif status == Status.FAILURE.value:
                                logging.info(
                                    f"Job id: {job_id}, status: {status}, "
                                    f"duration: {duration}, error: {result.data}"
                                )
                            return result, status
                except (TimeoutError, asyncio.exceptions.CancelledError):
                    logging.info(f"{job_id} timeout")
                    raise
        except Exception as e:
            duration = time.time() - start_time
            logging.info(
                f"Job id: {job_id}, status: {Status.FAILURE.value}, duration: {duration}",
                exc_info=True,
            )
            return (
                JobResult(
                    id=job_id,
                    data=str(e),
                    content_type="application/text",
                ),
                Status.FAILURE.value,
            )
        finally:
            try:
                await self.client.delete(self.job_key(job_id))
            except Exception:
                logging.warning(
                    f"Cleanup failed for {job_id}",
                    exc_info=True,
                )

post(job_parameter: QslJobParameterRender | QslJobParameterFeatureInfo | QslJobParameterLegend | QslJobParameterFeature | QslJobParameterCrs, to: float = 10.0) -> tuple[JobResult, str] async

Posts a new runner to the runner queue and waits maximum timeout seconds to complete. Will return a JobResult if successful or raise an error.

Parameters:

Source code in src/qgis_server_light/dispatcher/redis_asio.py
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
async def post(
    self,
    job_parameter: (
        QslJobParameterRender
        | QslJobParameterFeatureInfo
        | QslJobParameterLegend
        | QslJobParameterFeature
        | QslJobParameterCrs
    ),
    to: float = 10.0,
) -> tuple[JobResult, str]:
    """
    Posts a new `runner` to the runner queue and waits maximum `timeout` seconds to complete.
    Will return a JobResult if successful or raise an error.

    Args:
        job_parameter: The parameter for the job which should be executed.
        to: The timeout a job is expected to be waited for before canceling
            job execution.
    """
    job_id = str(uuid4())
    start_time = time.time()
    try:
        job_info = self.derive_job_info(job_id, job_parameter)
    except TypeError as e:
        return (
            JobResult(
                id=job_id,
                data=str(e),
                content_type="application/text",
            ),
            Status.FAILURE.value,
        )
    try:
        async with self.client.pubsub() as ps:
            # We subscribe to `notifications:{job_id}` *before* the job is
            # queued. Redis pub/sub does not buffer messages, so a fast job
            # (e.g. a legend) could otherwise be published by the worker
            # before we listen, and its result would be lost.
            await ps.subscribe(self.channel(job_id))
            async with self.client.pipeline() as p:
                await self.queue_job(job_info, p, start_time)
                await p.execute()

            logging.info(f"{job_id} queued")
            try:
                # this puts a timeout trigger on the subscription, after timeout
                # an asyncio.TimeoutError or asyncio.exceptions.CancelledError
                # is raised. See except block below.
                async with timeout(to):
                    while True:
                        message = await ps.get_message(
                            timeout=to, ignore_subscribe_messages=True
                        )
                        if not message:
                            continue  # https://github.com/redis/redis-py/issues/733
                        status_binary = await self.client.hget(
                            self.job_key(job_id), self.job_status_key
                        )
                        status = status_binary.decode()
                        result: JobResult = pickle.loads(message["data"])
                        duration = time.time() - start_time
                        if status == Status.SUCCESS.value:
                            logging.info(
                                f"Job id: {job_id}, status: {status}, duration: {duration}"
                            )
                        elif status == Status.FAILURE.value:
                            logging.info(
                                f"Job id: {job_id}, status: {status}, "
                                f"duration: {duration}, error: {result.data}"
                            )
                        return result, status
            except (TimeoutError, asyncio.exceptions.CancelledError):
                logging.info(f"{job_id} timeout")
                raise
    except Exception as e:
        duration = time.time() - start_time
        logging.info(
            f"Job id: {job_id}, status: {Status.FAILURE.value}, duration: {duration}",
            exc_info=True,
        )
        return (
            JobResult(
                id=job_id,
                data=str(e),
                content_type="application/text",
            ),
            Status.FAILURE.value,
        )
    finally:
        try:
            await self.client.delete(self.job_key(job_id))
        except Exception:
            logging.warning(
                f"Cleanup failed for {job_id}",
                exc_info=True,
            )

RedisStateQueue

Bases: RedisDispatcher, RedisStateProtocol

The dispatcher for jobs which keep their state. A job is submitted and its state can be read at any time later, until the job hash expires. A caller who wants to wait for the result uses wait.

Source code in src/qgis_server_light/dispatcher/redis_asio.py
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
class RedisStateQueue(RedisDispatcher, RedisStateProtocol):
    """The dispatcher for jobs which keep their state. A job is submitted and
    its state can be read at any time later, until the job hash expires. A
    caller who wants to wait for the result uses `wait`.
    """

    async def process_descriptions(self) -> ProcessDescriptions:
        """The descriptions of the processes the alive process-workers serve, each worker
        keeps its own in its hash. Empty as long as no process-worker is alive.

        Returns:
            The processes merged over all workers, see `merge_process_descriptions`.
        """
        served = []
        for worker in await self.alive_workers():
            processes_json = await self.client.hget(
                self.worker_key(worker.id), self.worker_processes_key
            )
            if processes_json is None:
                # the worker serves no processes or redis lost them until its next beat
                continue
            process_descriptions = JsonParser().from_bytes(processes_json, ProcessDescriptions)
            served.append((worker.info.qgis_info, process_descriptions))
        return self.merge_process_descriptions(served)

    @staticmethod
    def merge_process_descriptions(
        served: list[tuple[QgisInfo, ProcessDescriptions]],
    ) -> ProcessDescriptions:
        """Merges the processes of several workers into one list. Workers on one redis
        should all run the same QGIS and serve the same processes. When they don't, the
        description of the newest QGIS is taken and the process gets a warning, so the
        deployment can be corrected.

        Args:
            served: The QGIS of each worker and the processes it serves.

        Returns:
            Each process once, with a warning for each process the workers don't agree on.
        """
        descriptions_by_process_id: dict[str, list[tuple[QgisInfo, ProcessDescription]]] = {}
        # the newest QGIS first, its description is the one taken
        for qgis_info, process_descriptions in sorted(
            served, key=lambda item: item[0].version, reverse=True
        ):
            for process in process_descriptions.processes:
                descriptions_by_process_id.setdefault(process.id, []).append((qgis_info, process))

        merged = ProcessDescriptions()
        for process_id, described in descriptions_by_process_id.items():
            newest = described[0][1]
            merged.processes.append(newest)
            # each version once, the newest first
            qgis_versions = list(dict.fromkeys(qgis.version_name for qgis, _ in described))
            descriptions_differ = any(process != newest for _, process in described)
            if len(qgis_versions) > 1 or descriptions_differ:
                logging.warning(
                    f"Process `{process_id}` is served by QGIS {qgis_versions}, "
                    f"descriptions differ: {descriptions_differ}"
                )
                merged.warnings.append(
                    ProcessWarning(
                        process_id=process_id,
                        qgis_versions=qgis_versions,
                        descriptions_differ=descriptions_differ,
                    )
                )
        return merged

    async def submit(
        self,
        job_id: str,
        job_parameter: QslJobParameterExecuteProcess,
        execution_mode: ExecutionMode = ExecutionMode.ASYNC,
    ) -> None:
        """Queues a process job and returns right away. The expiry is set in the
        same transaction, so a job which is never picked up does not stay in
        redis forever.

        Args:
            job_id: The unique identifier of the job, given by the caller so it
                can refer to the job later.
            job_parameter: The parameter for the process which should be executed.
            execution_mode: `ExecutionMode.SYNC` if the caller is going to
                `wait` for the job, the worker only notifies then.
        """
        job_info = self.derive_job_info(job_id, job_parameter)
        job_info.execution_mode = execution_mode
        async with self.client.pipeline() as p:
            await self.queue_job(job_info, p, time.time())
            await p.expire(self.job_key(job_id), self.job_expire)
            await p.execute()
        logging.info(f"{job_id} submitted")

    async def dismiss(self, job_id: str) -> JobState | None:
        """Dismisses a job. A queued job is taken off the queue and is dismissed right
        away. A job a worker took already gets the cancel flag, the worker cancels it
        and finishes it as dismissed, so its state changes a moment later. A finished
        job stays as it is, there is nothing left to stop.

        Args:
            job_id: The unique identifier of the job.

        Returns:
            The state of the job after dismissing, `None` if the job does not exist
            (anymore).
        """
        job_state = await self.state(job_id)
        if job_state is None or job_state.finished:
            return job_state
        async with self.client.pipeline() as p:
            # in one transaction, so a worker taking the job meanwhile still sees the flag
            await p.lrem(self.queue_name(), 0, job_id)
            await p.hset(self.job_key(job_id), self.job_cancel_key, 1)
            removed, _ = await p.execute()
        if removed:
            # no worker will ever see the job, so the dispatcher finishes it itself
            async with self.client.pipeline() as p:
                await p.hset(self.job_key(job_id), self.job_message_key, "The job was dismissed.")
                await self.set_job_runtime_status(
                    job_id, p, Status.DISMISSED.value, time.time(), execute=False
                )
                await p.expire(self.job_key(job_id), self.job_expire)
                # a dispatcher waiting for a synchronous job wakes up
                await p.publish(self.channel(job_id), Status.DISMISSED.value)
                await p.execute()
        logging.info(f"{job_id} dismissed")
        return await self.state(job_id)

    async def state(self, job_id: str) -> JobState | None:
        """Reads the current state of the job from its job hash.

        Args:
            job_id: The unique identifier of the job.

        Returns:
            The state of the job, `None` if the job does not exist (anymore).
        """
        job_hash = await self.client.hgetall(self.job_key(job_id))
        if not job_hash:
            return None
        job_hash = {key.decode(): value for key, value in job_hash.items()}
        return JobState(
            id=job_id,
            status=Status(job_hash[self.job_status_key].decode()),
            last_update=self.decode_optional(job_hash.get(self.job_last_update_key)),
            result=self.job_result(job_id, job_hash),
            message=self.decode_optional(job_hash.get(self.job_message_key)),
            progress=self.decode_progress(job_hash.get(self.job_progress_key)),
        )

    @staticmethod
    def decode_optional(value: bytes | None) -> str | None:
        """Decodes a hash field which might not be set."""
        return value.decode() if value is not None else None

    @staticmethod
    def decode_progress(value: bytes | None) -> int | None:
        """Decodes the progress, which is only set if the job reported one."""
        return int(value) if value is not None else None

    def job_result(self, job_id: str, job_hash: dict[str, bytes]) -> JobResult | None:
        """Assembles the result of the job from its decoded job hash. The data
        stays bytes, the content type tells the caller how to read it.

        Args:
            job_id: The unique identifier of the job.
            job_hash: The job hash with decoded field names.

        Returns:
            The result, `None` as long as the job has not succeeded.
        """
        if self.job_result_key not in job_hash:
            return None
        return JobResult(
            id=job_id,
            data=job_hash[self.job_result_key],
            content_type=job_hash[self.job_result_content_type_key].decode(),
        )

    async def wait(self, job_id: str, to: float = 10.0) -> JobState | None:
        """Waits maximum `to` seconds for the job to finish. This is what makes a
        synchronous execution possible on top of a job which keeps its state.
        It only gets notified if the job was submitted with
        `ExecutionMode.SYNC`, otherwise it runs into the timeout.

        Args:
            job_id: The unique identifier of the job.
            to: The timeout in seconds.

        Returns:
            The state of the job after it finished or the timeout passed, `None`
            if the job does not exist (anymore).
        """
        async with self.client.pubsub() as ps:
            # We subscribe before reading the state, so a notification published in
            # between is not lost.
            await ps.subscribe(self.channel(job_id))
            job_state = await self.state(job_id)
            if job_state is None or job_state.finished:
                return job_state
            await self.wait_for_notification(ps, job_id, to)
        return await self.state(job_id)

    @staticmethod
    async def wait_for_notification(ps: PubSub, job_id: str, to: float) -> None:
        """Blocks until a message arrives on the subscribed channel or the timeout
        passes. The message itself is not of interest, the state is read from
        the job hash afterwards.

        Args:
            ps: The pubsub which is subscribed to the channel of the job.
            job_id: The unique identifier of the job, used for logging.
            to: The timeout in seconds.
        """
        try:
            async with timeout(to):
                while True:
                    message = await ps.get_message(timeout=to, ignore_subscribe_messages=True)
                    if message:
                        return
        except (TimeoutError, asyncio.exceptions.CancelledError):
            logging.info(f"{job_id} timeout")

decode_optional(value: bytes | None) -> str | None staticmethod

Decodes a hash field which might not be set.

Source code in src/qgis_server_light/dispatcher/redis_asio.py
403
404
405
406
@staticmethod
def decode_optional(value: bytes | None) -> str | None:
    """Decodes a hash field which might not be set."""
    return value.decode() if value is not None else None

decode_progress(value: bytes | None) -> int | None staticmethod

Decodes the progress, which is only set if the job reported one.

Source code in src/qgis_server_light/dispatcher/redis_asio.py
408
409
410
411
@staticmethod
def decode_progress(value: bytes | None) -> int | None:
    """Decodes the progress, which is only set if the job reported one."""
    return int(value) if value is not None else None

dismiss(job_id: str) -> JobState | None async

Dismisses a job. A queued job is taken off the queue and is dismissed right away. A job a worker took already gets the cancel flag, the worker cancels it and finishes it as dismissed, so its state changes a moment later. A finished job stays as it is, there is nothing left to stop.

Parameters:

  • job_id (str) –

    The unique identifier of the job.

Returns:

  • JobState | None –

    The state of the job after dismissing, None if the job does not exist

  • JobState | None –

    (anymore).

Source code in src/qgis_server_light/dispatcher/redis_asio.py
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
async def dismiss(self, job_id: str) -> JobState | None:
    """Dismisses a job. A queued job is taken off the queue and is dismissed right
    away. A job a worker took already gets the cancel flag, the worker cancels it
    and finishes it as dismissed, so its state changes a moment later. A finished
    job stays as it is, there is nothing left to stop.

    Args:
        job_id: The unique identifier of the job.

    Returns:
        The state of the job after dismissing, `None` if the job does not exist
        (anymore).
    """
    job_state = await self.state(job_id)
    if job_state is None or job_state.finished:
        return job_state
    async with self.client.pipeline() as p:
        # in one transaction, so a worker taking the job meanwhile still sees the flag
        await p.lrem(self.queue_name(), 0, job_id)
        await p.hset(self.job_key(job_id), self.job_cancel_key, 1)
        removed, _ = await p.execute()
    if removed:
        # no worker will ever see the job, so the dispatcher finishes it itself
        async with self.client.pipeline() as p:
            await p.hset(self.job_key(job_id), self.job_message_key, "The job was dismissed.")
            await self.set_job_runtime_status(
                job_id, p, Status.DISMISSED.value, time.time(), execute=False
            )
            await p.expire(self.job_key(job_id), self.job_expire)
            # a dispatcher waiting for a synchronous job wakes up
            await p.publish(self.channel(job_id), Status.DISMISSED.value)
            await p.execute()
    logging.info(f"{job_id} dismissed")
    return await self.state(job_id)

job_result(job_id: str, job_hash: dict[str, bytes]) -> JobResult | None

Assembles the result of the job from its decoded job hash. The data stays bytes, the content type tells the caller how to read it.

Parameters:

  • job_id (str) –

    The unique identifier of the job.

  • job_hash (dict[str, bytes]) –

    The job hash with decoded field names.

Returns:

  • JobResult | None –

    The result, None as long as the job has not succeeded.

Source code in src/qgis_server_light/dispatcher/redis_asio.py
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
def job_result(self, job_id: str, job_hash: dict[str, bytes]) -> JobResult | None:
    """Assembles the result of the job from its decoded job hash. The data
    stays bytes, the content type tells the caller how to read it.

    Args:
        job_id: The unique identifier of the job.
        job_hash: The job hash with decoded field names.

    Returns:
        The result, `None` as long as the job has not succeeded.
    """
    if self.job_result_key not in job_hash:
        return None
    return JobResult(
        id=job_id,
        data=job_hash[self.job_result_key],
        content_type=job_hash[self.job_result_content_type_key].decode(),
    )

merge_process_descriptions(served: list[tuple[QgisInfo, ProcessDescriptions]]) -> ProcessDescriptions staticmethod

Merges the processes of several workers into one list. Workers on one redis should all run the same QGIS and serve the same processes. When they don't, the description of the newest QGIS is taken and the process gets a warning, so the deployment can be corrected.

Parameters:

Returns:

  • ProcessDescriptions –

    Each process once, with a warning for each process the workers don't agree on.

Source code in src/qgis_server_light/dispatcher/redis_asio.py
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
@staticmethod
def merge_process_descriptions(
    served: list[tuple[QgisInfo, ProcessDescriptions]],
) -> ProcessDescriptions:
    """Merges the processes of several workers into one list. Workers on one redis
    should all run the same QGIS and serve the same processes. When they don't, the
    description of the newest QGIS is taken and the process gets a warning, so the
    deployment can be corrected.

    Args:
        served: The QGIS of each worker and the processes it serves.

    Returns:
        Each process once, with a warning for each process the workers don't agree on.
    """
    descriptions_by_process_id: dict[str, list[tuple[QgisInfo, ProcessDescription]]] = {}
    # the newest QGIS first, its description is the one taken
    for qgis_info, process_descriptions in sorted(
        served, key=lambda item: item[0].version, reverse=True
    ):
        for process in process_descriptions.processes:
            descriptions_by_process_id.setdefault(process.id, []).append((qgis_info, process))

    merged = ProcessDescriptions()
    for process_id, described in descriptions_by_process_id.items():
        newest = described[0][1]
        merged.processes.append(newest)
        # each version once, the newest first
        qgis_versions = list(dict.fromkeys(qgis.version_name for qgis, _ in described))
        descriptions_differ = any(process != newest for _, process in described)
        if len(qgis_versions) > 1 or descriptions_differ:
            logging.warning(
                f"Process `{process_id}` is served by QGIS {qgis_versions}, "
                f"descriptions differ: {descriptions_differ}"
            )
            merged.warnings.append(
                ProcessWarning(
                    process_id=process_id,
                    qgis_versions=qgis_versions,
                    descriptions_differ=descriptions_differ,
                )
            )
    return merged

process_descriptions() -> ProcessDescriptions async

The descriptions of the processes the alive process-workers serve, each worker keeps its own in its hash. Empty as long as no process-worker is alive.

Returns:

  • ProcessDescriptions –

    The processes merged over all workers, see merge_process_descriptions.

Source code in src/qgis_server_light/dispatcher/redis_asio.py
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
async def process_descriptions(self) -> ProcessDescriptions:
    """The descriptions of the processes the alive process-workers serve, each worker
    keeps its own in its hash. Empty as long as no process-worker is alive.

    Returns:
        The processes merged over all workers, see `merge_process_descriptions`.
    """
    served = []
    for worker in await self.alive_workers():
        processes_json = await self.client.hget(
            self.worker_key(worker.id), self.worker_processes_key
        )
        if processes_json is None:
            # the worker serves no processes or redis lost them until its next beat
            continue
        process_descriptions = JsonParser().from_bytes(processes_json, ProcessDescriptions)
        served.append((worker.info.qgis_info, process_descriptions))
    return self.merge_process_descriptions(served)

state(job_id: str) -> JobState | None async

Reads the current state of the job from its job hash.

Parameters:

  • job_id (str) –

    The unique identifier of the job.

Returns:

  • JobState | None –

    The state of the job, None if the job does not exist (anymore).

Source code in src/qgis_server_light/dispatcher/redis_asio.py
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
async def state(self, job_id: str) -> JobState | None:
    """Reads the current state of the job from its job hash.

    Args:
        job_id: The unique identifier of the job.

    Returns:
        The state of the job, `None` if the job does not exist (anymore).
    """
    job_hash = await self.client.hgetall(self.job_key(job_id))
    if not job_hash:
        return None
    job_hash = {key.decode(): value for key, value in job_hash.items()}
    return JobState(
        id=job_id,
        status=Status(job_hash[self.job_status_key].decode()),
        last_update=self.decode_optional(job_hash.get(self.job_last_update_key)),
        result=self.job_result(job_id, job_hash),
        message=self.decode_optional(job_hash.get(self.job_message_key)),
        progress=self.decode_progress(job_hash.get(self.job_progress_key)),
    )

submit(job_id: str, job_parameter: QslJobParameterExecuteProcess, execution_mode: ExecutionMode = ExecutionMode.ASYNC) -> None async

Queues a process job and returns right away. The expiry is set in the same transaction, so a job which is never picked up does not stay in redis forever.

Parameters:

  • job_id (str) –

    The unique identifier of the job, given by the caller so it can refer to the job later.

  • job_parameter (QslJobParameterExecuteProcess) –

    The parameter for the process which should be executed.

  • execution_mode (ExecutionMode, default: ASYNC ) –

    ExecutionMode.SYNC if the caller is going to wait for the job, the worker only notifies then.

Source code in src/qgis_server_light/dispatcher/redis_asio.py
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
async def submit(
    self,
    job_id: str,
    job_parameter: QslJobParameterExecuteProcess,
    execution_mode: ExecutionMode = ExecutionMode.ASYNC,
) -> None:
    """Queues a process job and returns right away. The expiry is set in the
    same transaction, so a job which is never picked up does not stay in
    redis forever.

    Args:
        job_id: The unique identifier of the job, given by the caller so it
            can refer to the job later.
        job_parameter: The parameter for the process which should be executed.
        execution_mode: `ExecutionMode.SYNC` if the caller is going to
            `wait` for the job, the worker only notifies then.
    """
    job_info = self.derive_job_info(job_id, job_parameter)
    job_info.execution_mode = execution_mode
    async with self.client.pipeline() as p:
        await self.queue_job(job_info, p, time.time())
        await p.expire(self.job_key(job_id), self.job_expire)
        await p.execute()
    logging.info(f"{job_id} submitted")

wait(job_id: str, to: float = 10.0) -> JobState | None async

Waits maximum to seconds for the job to finish. This is what makes a synchronous execution possible on top of a job which keeps its state. It only gets notified if the job was submitted with ExecutionMode.SYNC, otherwise it runs into the timeout.

Parameters:

  • job_id (str) –

    The unique identifier of the job.

  • to (float, default: 10.0 ) –

    The timeout in seconds.

Returns:

  • JobState | None –

    The state of the job after it finished or the timeout passed, None

  • JobState | None –

    if the job does not exist (anymore).

Source code in src/qgis_server_light/dispatcher/redis_asio.py
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
async def wait(self, job_id: str, to: float = 10.0) -> JobState | None:
    """Waits maximum `to` seconds for the job to finish. This is what makes a
    synchronous execution possible on top of a job which keeps its state.
    It only gets notified if the job was submitted with
    `ExecutionMode.SYNC`, otherwise it runs into the timeout.

    Args:
        job_id: The unique identifier of the job.
        to: The timeout in seconds.

    Returns:
        The state of the job after it finished or the timeout passed, `None`
        if the job does not exist (anymore).
    """
    async with self.client.pubsub() as ps:
        # We subscribe before reading the state, so a notification published in
        # between is not lost.
        await ps.subscribe(self.channel(job_id))
        job_state = await self.state(job_id)
        if job_state is None or job_state.finished:
            return job_state
        await self.wait_for_notification(ps, job_id, to)
    return await self.state(job_id)

wait_for_notification(ps: PubSub, job_id: str, to: float) -> None async staticmethod

Blocks until a message arrives on the subscribed channel or the timeout passes. The message itself is not of interest, the state is read from the job hash afterwards.

Parameters:

  • ps (PubSub) –

    The pubsub which is subscribed to the channel of the job.

  • job_id (str) –

    The unique identifier of the job, used for logging.

  • to (float) –

    The timeout in seconds.

Source code in src/qgis_server_light/dispatcher/redis_asio.py
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
@staticmethod
async def wait_for_notification(ps: PubSub, job_id: str, to: float) -> None:
    """Blocks until a message arrives on the subscribed channel or the timeout
    passes. The message itself is not of interest, the state is read from
    the job hash afterwards.

    Args:
        ps: The pubsub which is subscribed to the channel of the job.
        job_id: The unique identifier of the job, used for logging.
        to: The timeout in seconds.
    """
    try:
        async with timeout(to):
            while True:
                message = await ps.get_message(timeout=to, ignore_subscribe_messages=True)
                if message:
                    return
    except (TimeoutError, asyncio.exceptions.CancelledError):
        logging.info(f"{job_id} timeout")