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:
-
job_info_classes(dict[type[QslJobParameter], type[QslJobInfoParameter]]) –The job info class per job parameter class, used to wrap a job parameter into its job info.
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 | |
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 | |
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 | |
create(url: str)
classmethod
Source code in src/qgis_server_light/dispatcher/redis_asio.py
90 91 92 93 | |
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:
-
QslJobInfoParameter–The job info carrying the job parameter.
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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:
-
job_parameter(QslJobParameterRender | QslJobParameterFeatureInfo | QslJobParameterLegend | QslJobParameterFeature | QslJobParameterCrs) –The parameter for the job which should be executed.
-
to(float, default:10.0) –The timeout a job is expected to be waited for before canceling job execution.
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 | |
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 | |
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 | |
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 | |
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,
Noneif 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 | |
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,
Noneas 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 | |
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:
-
served(list[tuple[QgisInfo, ProcessDescriptions]]) –The QGIS of each worker and the processes it serves.
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 | |
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 | |
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,
Noneif 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 | |
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.SYNCif the caller is going towaitfor 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 | |
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 | |
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 | |