Lilota module
Lilota
High-level interface for Lilota task scheduling and worker management.
The Lilota class coordinates a scheduler instance and a pool of worker processes that execute user-defined task scripts.
A scheduler manages task persistence, distribution, and node heartbeats, while worker processes execute tasks defined inside the provided script.
Workers are started as separate Python processes that run the provided script file as a module entry point.
Source code in lilota/core.py
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 475 476 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 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 | |
error
property
Return the last fatal error encountered by the system.
The error is reset when start() starts the workers again.
Returns:
| Name | Type | Description |
|---|---|---|
Any |
The error object or message captured from a worker process, |
|
|
or None if no error has occurred. |
__init__(db_url=DEFAULT_DB_URL, *, mode=LilotaMode.ALL, script_path=None, number_of_workers=1, scheduler_heartbeat_interval=5, scheduler_timeout_sec=20, process_manager_interval=1.0, stop_worker_timeout=60, logging_level=logging.INFO, on_fatal_error=None)
Create a new Lilota runtime instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_url
|
str
|
Database connection URL used by the scheduler. |
DEFAULT_DB_URL
|
mode
|
LilotaMode
|
Execution mode controlling whether the scheduler, workers, or both are started. Defaults to ALL. |
ALL
|
script_path
|
str
|
Path to a Python script that defines and registers Lilota tasks.
Each worker process executes this script as its entry point.
The script must call |
None
|
number_of_workers
|
int
|
Number of worker processes to spawn. A worker executes one task at a time, so this is the number of tasks that can be executed in parallel. Defaults to 1 and cannot exceed the number of CPU cores * 5. |
1
|
scheduler_heartbeat_interval
|
float
|
Interval in seconds between scheduler node heartbeats. Defaults to 5 seconds. |
5
|
scheduler_timeout_sec
|
int
|
Time in seconds after which a node is considered inactive if no heartbeat is received. Defaults to 20 seconds. |
20
|
stop_worker_timeout
|
int
|
Maximum time in seconds to wait for worker processes to exit gracefully before they are forcefully killed. Defaults to 60 seconds. |
60
|
logging_level
|
int
|
Logging level used by the scheduler. Defaults to logging.INFO. |
INFO
|
on_fatal_error
|
Callable[[str], None]
|
Function that is called when a fatal error has stopped all
workers. It gets the error, which is also what |
None
|
Source code in lilota/core.py
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 475 476 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 | |
cleanup(older_than)
Delete old rows from the tables of lilota.
Nothing else deletes them: every task, every log entry and every node stays in the database until this method is called. Call it from time to time, for example once a day. It works in every mode, whether lilota is started or not.
These rows are deleted if they are older than older_than:
- Tasks that are finished: completed, failed, expired or cancelled.
The age is counted from the time the task ended or expired.
- Log entries. The age is counted from the time the entry was written.
- Nodes with the status dead. The age is counted from the time the
node was last seen.
A task that is waiting or being executed, a failed or expired task that still waits for its retry, and a node that is alive are never deleted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
older_than
|
timedelta
|
The age from which on a row is deleted.
|
required |
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
dict[str, int]: The number of deleted rows, with the keys |
dict[str, int]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in lilota/core.py
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 | |
get_all_nodes()
Retrieve all registered nodes: schedulers and workers.
Returns:
| Type | Description |
|---|---|
list[Node]
|
list[Node]: A list containing all nodes currently stored in the |
list[Node]
|
node store. |
Source code in lilota/core.py
738 739 740 741 742 743 744 745 | |
get_task_by_id(id)
Retrieve a task by its unique identifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UUID
|
Unique task identifier. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Task object associated with the given ID. |
Source code in lilota/core.py
747 748 749 750 751 752 753 754 755 756 | |
has_failed()
Check whether a fatal error has occurred.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if a fatal error has been detected, otherwise False. |
Source code in lilota/core.py
542 543 544 545 546 547 548 | |
schedule(name, input=None)
Schedule a task for execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the registered task. |
required |
input
|
Any
|
Input payload for the task. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
UUID |
UUID
|
Identifier of the scheduled task. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If lilota has never been started, or if it only runs workers (mode WORKERS). |
Source code in lilota/core.py
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 | |
scheduler(db_url=DEFAULT_DB_URL, **kwargs)
classmethod
Create a LilotaScheduler instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_url
|
str
|
Database connection URL. |
DEFAULT_DB_URL
|
**kwargs
|
Additional keyword arguments passed to the constructor. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
LilotaScheduler |
LilotaScheduler
|
Configured LilotaScheduler instance. |
Source code in lilota/core.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | |
start()
Start the configured Lilota components.
Depending on the selected mode, this method: - Starts the scheduler (SCHEDULER, ALL) - Spawns worker processes (WORKER, ALL) - Starts process monitoring for workers (WORKER, ALL)
After startup, the instance is ready to schedule and/or execute tasks.
After a fatal error the method starts the workers again. stop() does
not have to be called first. The fatal error is reset: has_failed()
returns False and error returns None, until the next fatal error.
Source code in lilota/core.py
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 | |
stop()
Stop all running Lilota components.
This includes
- Stopping the scheduler (if running)
- Stopping process monitoring
- Gracefully terminating worker processes
Worker processes are first asked to terminate gracefully. If a worker
does not exit within stop_worker_timeout seconds, it is forcefully killed.
Source code in lilota/core.py
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | |
wait_for_failure(timeout=None)
Block until a fatal error occurs or a timeout is reached.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float | None
|
Maximum time in seconds to wait. If None, waits indefinitely. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if a failure occurred before the timeout, False otherwise. |
Source code in lilota/core.py
550 551 552 553 554 555 556 557 558 559 560 | |
workers(script_path, number_of_workers=1, db_url=DEFAULT_DB_URL, **kwargs)
classmethod
Create a Lilota instance running only worker processes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_url
|
str
|
Database connection URL. |
DEFAULT_DB_URL
|
script_path
|
str
|
Path to the worker script. |
required |
number_of_workers
|
int
|
Number of worker processes to spawn. Defaults to 1. |
1
|
**kwargs
|
Additional keyword arguments passed to the constructor. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Lilota |
Configured instance in worker-only mode. |
Source code in lilota/core.py
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 | |
LilotaMode
Bases: str, Enum
Execution mode for the Lilota runtime.
Attributes:
| Name | Type | Description |
|---|---|---|
SCHEDULER |
Run only the scheduler component. |
|
WORKERS |
Run only worker processes. |
|
ALL |
Run both scheduler and workers. |
Source code in lilota/core.py
359 360 361 362 363 364 365 366 367 368 369 370 | |
ProcessManager
Source code in lilota/core.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 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 250 251 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 | |
__init__(on_fatal_error=None)
Initializes the ProcessManager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
on_fatal_error
|
Callable[[str], None] | None
|
Optional callback that is invoked when a managed process exits with an error. The callback receives the stderr output of the failed process. |
None
|
Source code in lilota/core.py
122 123 124 125 126 127 128 129 130 131 | |
monitor()
Monitors managed processes for termination and handles restarts.
Iterates over all managed processes and checks whether they have exited.
A process that was killed is restarted using the original script path. That is a worker that stopped itself because its task has expired, or a process that was terminated by a signal. Such a worker has not failed by itself, so what it has written to stderr before does not matter.
If a process ends on its own with a non-zero return code and has produced stderr output, the script has failed. All processes are stopped and the fatal error callback is invoked.
The same happens if a process exits successfully. A started worker runs until it is stopped from the outside, so a script that ends on its own never started its worker.
In all other cases the process is restarted as well.
A worker that stopped itself because its task has expired is restarted at
once. Every other process has died, and is restarted after a waiting
time: WORKER_RESTART_DELAY seconds, and twice as long with every
restart in a row. After WORKER_RESTART_ATTEMPTS restarts in a row the
script keeps dying. All processes are stopped and the fatal error callback
is invoked. A process that has been running for
WORKER_RESTART_RESET_TIME seconds before it died does not count as a
restart in a row.
Source code in lilota/core.py
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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |
start(script_path, restarts=0)
Starts a new subprocess running the given Python script.
The process is launched using the current Python interpreter and added to the internal list of managed processes.
What the process writes to stdout goes to the stdout of the current
process. What it writes to stderr is read by a WorkerErrorOutput,
which passes it on to the stderr of the current process.
The process is told the ID of the current process, so that a worker
stops itself when the current process is gone (see
ParentProcessTask in lilota.worker).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
script_path
|
str
|
Path to the Python script to execute. |
required |
restarts
|
int
|
How many times in a row the script has been restarted
because its process had died. Only |
0
|
Returns:
| Type | Description |
|---|---|
Popen
|
subprocess.Popen: The Popen object representing the started process. |
Source code in lilota/core.py
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 | |
stop_all(timeout=5.0)
Stops all managed processes gracefully, then forcefully if needed.
Attempts to terminate all running processes. If a process does not exit within the specified timeout, it is forcefully killed. After stopping, the internal process list is cleared.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float
|
Maximum time in seconds to wait for each process to terminate gracefully before forcefully killing it. Defaults to 5.0. |
5.0
|
Source code in lilota/core.py
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 | |
WorkerErrorOutput
Bases: Thread
Reads what a worker process writes to stderr, for as long as the process runs.
The stderr of a worker process is connected to a pipe, because the text is needed
when the process fails (see ProcessManager.monitor). A pipe only holds a
limited amount of data. If nobody reads from it, the worker blocks as soon as it
is full, in the middle of whatever task it is executing. This thread therefore
reads every line the moment it is written.
Each line is passed on to the stderr of the current process, so that warnings and tracebacks of a worker are visible where the output of the application is. The last lines are also kept, to be reported as the error if the process fails.
Source code in lilota/core.py
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 | |
__init__(proc)
Initializes the reader. It starts reading when start() is called.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
proc
|
Popen
|
Worker process, started with |
required |
Source code in lilota/core.py
53 54 55 56 57 58 59 60 61 62 63 | |
run()
Reads the stream until the worker process closes it.
Source code in lilota/core.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
text()
Returns the last lines the worker process wrote to stderr.
Meant to be called after the process has exited. Waits for the remaining
output to be read, but not longer than JOIN_TIMEOUT seconds.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
At most the last |
str
|
process wrote nothing to stderr. |
Source code in lilota/core.py
90 91 92 93 94 95 96 97 98 99 100 101 102 | |
LilotaNode
Bases: ABC
Abstract base class for all Lilota nodes.
A node represents a running component in the Lilota system, such as a scheduler or a worker. This class handles common functionality including:
- database initialization and migrations
- node lifecycle management
- node status updates
- heartbeat management
- access to node and task stores
Source code in lilota/node.py
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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
__init__(*, db_url, node_type, node_heartbeat_interval, node_timeout_sec, logger_name, logging_level)
Initialize a Lilota node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_url
|
str
|
Database connection URL. |
required |
node_type
|
NodeType
|
Type of node ( |
required |
node_heartbeat_interval
|
float
|
Interval in seconds between node heartbeat updates. |
required |
node_timeout_sec
|
int
|
Time in seconds after which a node is considered inactive if no heartbeat is received. |
required |
logger_name
|
str
|
Name of the logger used by the node. |
required |
logging_level
|
int
|
Logging level used for the node logger. |
required |
Source code in lilota/node.py
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 | |
delete_task_by_id(id)
Delete a task from the system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UUID
|
Unique identifier of the task. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if the task was deleted, otherwise False. |
Source code in lilota/node.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
get_node()
Return the current node record.
Returns:
| Type | Description |
|---|---|
|
Any | None: The node record if the node has been created, |
|
|
otherwise |
Source code in lilota/node.py
180 181 182 183 184 185 186 187 | |
get_nodes()
Return all nodes currently registered in the system.
Returns:
| Name | Type | Description |
|---|---|---|
list |
A list of node records. |
Source code in lilota/node.py
172 173 174 175 176 177 178 | |
get_task_by_id(id)
Retrieve a task by its identifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UUID
|
Unique identifier of the task. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Task record associated with the given ID. |
Source code in lilota/node.py
189 190 191 192 193 194 195 196 197 198 | |
start()
Start the node.
Initializes the node in the database, sets its status to RUNNING,
and triggers the node-specific startup logic implemented in
_on_started().
Raises:
| Type | Description |
|---|---|
Exception
|
If the node is already started. |
Source code in lilota/node.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 133 134 135 136 137 138 139 140 141 142 143 | |
stop()
Stop the node.
Updates the node status to STOPPING, executes subclass-specific
shutdown logic via _on_stop(), and then sets the node status to
IDLE.
Raises:
| Type | Description |
|---|---|
Exception
|
If the node was not started. |
Source code in lilota/node.py
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 | |
NodeHeartbeatTask
Bases: HeartbeatTask
Heartbeat task used by Lilota nodes.
This task periodically updates the last_seen_at timestamp of the
associated node in the database to indicate that the node is still alive.
Source code in lilota/node.py
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 | |
__init__(interval, jitter, node_id, node_store, logger)
Initialize the heartbeat task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
float
|
Interval in seconds between heartbeat executions. |
required |
jitter
|
float
|
Jitter that is used for the heartbeat interval. |
required |
node_id
|
UUID
|
Unique identifier of the node. |
required |
node_store
|
NodeStore
|
Store used for node operations. |
required |
logger
|
Logger
|
Logger instance used for reporting errors. |
required |
Source code in lilota/node.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
execute()
Execute the heartbeat update.
Updates the last_seen_at field of the node in the database.
Any errors are logged without interrupting the heartbeat loop.
Source code in lilota/node.py
40 41 42 43 44 45 46 47 48 49 50 51 | |
LilotaScheduler
Bases: LilotaNode
Scheduler node responsible for creating and managing tasks.
The scheduler registers itself as a scheduler node in the system and periodically sends heartbeats to indicate it is alive. Its main role is to create tasks and store them in the task store for workers to execute.
Source code in lilota/scheduler.py
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 | |
__init__(db_url, node_heartbeat_interval=5.0, node_timeout_sec=20, logging_level=logging.INFO, **kwargs)
Initialize the scheduler node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_url
|
str
|
Database connection URL. |
required |
node_heartbeat_interval
|
float
|
Interval in seconds between node heartbeats. Defaults to 5.0. |
5.0
|
node_timeout_sec
|
int
|
Time in seconds before a node is considered inactive. Defaults to 20. |
20
|
logging_level
|
int
|
Logging level used by the scheduler. Defaults to logging.INFO. |
INFO
|
**kwargs
|
Additional keyword arguments passed to the parent
|
{}
|
Source code in lilota/scheduler.py
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 | |
has_unfinished_tasks()
Check whether there are unfinished tasks in the system.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if unfinished tasks exist, otherwise False. |
Source code in lilota/scheduler.py
95 96 97 98 99 100 101 | |
schedule(name, input=None)
Create and store a new task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the registered task. |
required |
input
|
Any
|
Input payload for the task. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
UUID |
UUID
|
Identifier of the created task. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the scheduler has never been started. |
Source code in lilota/scheduler.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
LilotaWorker
Bases: LilotaNode
Worker node responsible for executing scheduled tasks.
Workers poll the task store for pending tasks, execute registered functions, and update task status and progress. Each worker also sends periodic heartbeats and participates in leader election for cluster maintenance.
Source code in lilota/worker.py
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 250 251 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 475 476 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 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 | |
__init__(db_url=DEFAULT_DB_URL, node_heartbeat_interval=5.0, node_heartbeat_interval_jitter=0.2, node_timeout_sec=20, task_heartbeat_interval=0.1, max_task_heartbeat_interval=5.0, logging_level=logging.INFO, **kwargs)
Initialize a worker node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_url
|
str
|
Database connection URL. |
DEFAULT_DB_URL
|
node_heartbeat_interval
|
float
|
Interval in seconds between node heartbeats. Defaults to 5.0. |
5.0
|
node_heartbeat_interval_jitter
|
float
|
Jitter applied to the heartbeat interval to introduce small random variations in execution timing and avoid synchronized worker behavior. |
0.2
|
node_timeout_sec
|
int
|
Time in seconds before a node is considered inactive. Defaults to 20. |
20
|
task_heartbeat_interval
|
float
|
Interval in seconds after which the worker looks for the next task when it has executed one. Every time it finds no task, the interval is doubled. Must be greater than 0. Defaults to 0.1. |
0.1
|
max_task_heartbeat_interval
|
float
|
Maximum polling interval when no tasks are available. A task that is scheduled while the worker is idle is picked up within this time. Must be greater than 0. Defaults to 5.0. |
5.0
|
logging_level
|
int
|
Logging level used by the worker. |
INFO
|
**kwargs
|
Additional keyword arguments passed to |
{}
|
Source code in lilota/worker.py
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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
task(func=None, *, name=None, timeout=timedelta(minutes=5), max_attempts=MAX_ATTEMPTS, keep_input=True)
Register a function as a task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
The function, when the decorator is used without parentheses. |
None
|
|
name
|
str
|
The name the task is scheduled by. Defaults to the name of the function. |
None
|
timeout
|
timedelta
|
How long the task may run before it is expired. Defaults to 5 minutes. None disables the timeout. |
timedelta(minutes=5)
|
max_attempts
|
int
|
How often the task is executed at most, the first execution included. Defaults to 1. |
MAX_ATTEMPTS
|
keep_input
|
bool
|
False to remove the input of the task from the database as soon as the task is finished for good: when it is completed, or when it has failed or expired with no attempts left. For a task whose input should not stay in the database, like a mail with a link that signs somebody in. Defaults to True. |
True
|
Source code in lilota/worker.py
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 | |
ParentProcessTask
Bases: HeartbeatTask
Heartbeat task that notices when the process that started the worker is gone.
A worker process is started by an application and stopped by it, with
Lilota.stop() or when the application exits. An application that is killed,
that crashes or that is terminated without handling the signal stops nothing.
Its workers would run on without it and keep taking tasks.
The operating system gives a process whose parent has ended a new parent. This task therefore compares the parent of the worker process with the process that started it.
Source code in lilota/worker.py
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 | |
__init__(interval, parent_pid, on_parent_gone)
Initialize the task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
float
|
Interval in seconds between the checks. |
required |
parent_pid
|
int
|
ID of the process that started the worker process. |
required |
on_parent_gone
|
Callable[[], None]
|
Called when that process is gone. |
required |
Source code in lilota/worker.py
172 173 174 175 176 177 178 179 180 181 182 183 184 | |
execute()
Check whether the worker process still has the parent that started it.
Source code in lilota/worker.py
186 187 188 189 | |
WorkerHeartbeatTask
Bases: NodeHeartbeatTask
Heartbeat task used by worker nodes.
In addition to updating the node heartbeat, this task also performs leader election among workers. The elected leader periodically performs maintenance tasks such as cleaning up stale nodes.
Source code in lilota/worker.py
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 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 | |
__init__(interval, jitter, node_id, node_timeout_sec, node_store, node_leader_store, task_store, logger)
Initialize the worker heartbeat task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
float
|
Interval in seconds between heartbeats. |
required |
jitter
|
float
|
Jitter that is used for the heartbeat interval. |
required |
node_id
|
UUID
|
Unique identifier of the worker node. |
required |
node_timeout_sec
|
int
|
Timeout in seconds after which nodes are considered dead. |
required |
node_store
|
NodeStore
|
Store used for node operations. |
required |
node_leader_store
|
NodeLeaderStore
|
Store used for leader election and leadership renewal. |
required |
task_store
|
TaskStore
|
Store used for task operations. |
required |
logger
|
Logger
|
Logger instance. |
required |
Source code in lilota/worker.py
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 | |
execute()
Execute the heartbeat logic.
Updates the node's last-seen timestamp and attempts to perform leader election. If the node becomes leader, it will also trigger cleanup tasks.
Source code in lilota/worker.py
66 67 68 69 70 71 72 73 74 75 76 77 | |
LogStore
Database store for logging entries into Lilota.
Source code in lilota/stores.py
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 | |
__init__(db_url)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_url
|
str
|
Database connection URL. |
required |
Source code in lilota/stores.py
761 762 763 764 765 766 767 768 | |
delete_log_entries(cutoff)
Delete the log entries that were written before the cutoff.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cutoff
|
datetime
|
Entries written before this time are deleted. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of log entries that were deleted. |
Source code in lilota/stores.py
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 | |
get_log_entries()
Return all log entries.
Source code in lilota/stores.py
783 784 785 786 | |
get_log_entries_by_node_id(node_id)
Return all log entries associated with a given node.
Source code in lilota/stores.py
804 805 806 807 808 809 810 811 812 | |
get_session()
Return a new SQLAlchemy session.
Source code in lilota/stores.py
778 779 780 781 | |
NodeLeaderStore
Bases: StoreBase
Store managing leader election for worker nodes.
Source code in lilota/stores.py
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 | |
__init__(db_url, logger, node_timeout_sec)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_url
|
str
|
Database connection URL. |
required |
logger
|
Logger
|
Logger for store operations. |
required |
node_timeout_sec
|
int
|
Leader lease timeout in seconds. |
required |
Source code in lilota/stores.py
818 819 820 821 822 823 824 825 826 | |
renew_leadership(node_id)
Renew leadership lease for the given node.
Returns True if renewed successfully.
Source code in lilota/stores.py
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 | |
try_acquire_leadership(node_id)
Attempt to acquire leadership for the given node.
Returns True if leadership is acquired, False otherwise.
Source code in lilota/stores.py
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 | |
NodeStore
Bases: StoreBase
Database store for managing Lilota nodes.
Source code in lilota/stores.py
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 | |
__init__(db_url, logger)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_url
|
str
|
Database connection URL. |
required |
logger
|
Logger
|
Logger for store operations. |
required |
Source code in lilota/stores.py
78 79 80 81 82 83 84 | |
create_node(type, status=NodeStatus.IDLE)
Create a new node record in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type
|
NodeType
|
Type of the node (scheduler or worker). |
required |
status
|
NodeStatus
|
Initial lifecycle status. |
IDLE
|
Returns:
| Name | Type | Description |
|---|---|---|
UUID |
UUID
|
The unique identifier of the created node. |
Source code in lilota/stores.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
delete_dead_nodes(cutoff)
Delete the nodes with the status DEAD that were last seen before the cutoff.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cutoff
|
datetime
|
Nodes last seen before this time are deleted. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of nodes that were deleted. |
Source code in lilota/stores.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
get_all_nodes()
Return all nodes in the database.
Source code in lilota/stores.py
104 105 106 107 | |
get_node_by_id(id)
Return a node by its UUID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UUID
|
Node identifier. |
required |
Returns:
| Type | Description |
|---|---|
|
Node | None: Node object if found, else None. |
Source code in lilota/stores.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
update_node_last_seen_at(id)
Update the heartbeat timestamp of a node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UUID
|
Node identifier. |
required |
Source code in lilota/stores.py
175 176 177 178 179 180 181 182 183 184 185 186 187 | |
update_node_status(id, status)
Update the status of a node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UUID
|
Node identifier. |
required |
status
|
NodeStatus
|
New status. |
required |
Source code in lilota/stores.py
124 125 126 127 128 129 130 131 132 133 134 | |
update_status_on_dead_nodes(cutoff, exclude_node_id)
Mark nodes as DEAD if their last_seen_at is older than cutoff.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cutoff
|
datetime
|
Time threshold to consider a node dead. |
required |
exclude_node_id
|
UUID
|
Node to exclude from the update. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
Number of nodes marked as DEAD. |
Source code in lilota/stores.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
StoreBase
Bases: ABC
Abstract base class for all database stores.
Provides common initialization and session management.
Source code in lilota/stores.py
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 | |
__init__(db_url, logger)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_url
|
str
|
Database connection URL. |
required |
logger
|
Logger
|
Logger for store operations. |
required |
Source code in lilota/stores.py
54 55 56 57 58 59 60 61 62 63 | |
TaskStore
Bases: StoreBase
Database store for managing Lilota tasks.
Source code in lilota/stores.py
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 250 251 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 475 476 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 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 | |
__init__(db_url, logger)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_url
|
str
|
Database connection URL. |
required |
logger
|
Logger
|
Logger for store operations. |
required |
Source code in lilota/stores.py
195 196 197 198 199 200 201 | |
create_task(name, input=None)
Create a new task record in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Registered task name. |
required |
input
|
Any
|
Input data for the task. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
UUID |
The unique identifier of the created task. |
Source code in lilota/stores.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
delete_finished_tasks(cutoff)
Delete the tasks that finished before the cutoff.
A task is finished when it is completed, failed, expired or cancelled. A failed or expired task that still waits for its retry is kept. Without it the retry would never be created.
A task finished at the time it ended. An expired task has no end time, so the time it expired is used instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cutoff
|
datetime
|
Tasks that finished before this time are deleted. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of tasks that were deleted. |
Source code in lilota/stores.py
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 | |
delete_task_by_id(id)
Delete a task by its UUID.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if deleted, False if task not found. |
Source code in lilota/stores.py
641 642 643 644 645 646 647 648 649 650 651 652 653 | |
end_task_failure(id, error, task_progress, can_be_retried=True)
Mark a task as failed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UUID
|
Id of the task. |
required |
error
|
dict
|
Error information to store. |
required |
can_be_retried
|
bool
|
False for a task that must not be executed
again, whatever attempts it has left. Its |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if the task was set to failed, False if it was not changed |
|
|
because it has ended or was retried in the meantime, see |
Source code in lilota/stores.py
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 | |
end_task_success(id, output, task_progress)
Mark a task as successfully completed.
A task that has ended or was retried in the meantime is not changed, see
_end_task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UUID
|
Id of the task. |
required |
output
|
Any
|
Return value of the task function. It is converted
with |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the task was completed, False if it was not changed. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the output cannot be converted to JSON. |
Source code in lilota/stores.py
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 | |
expire_overdue_tasks()
Set status to "expired" for running tasks whose expiration time has passed.
Source code in lilota/stores.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
get_all_tasks()
Return all tasks ordered by ID.
Source code in lilota/stores.py
224 225 226 227 | |
get_next_task(worker_id)
Return the next available task for a worker and lock it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worker_id
|
UUID
|
Worker node locking the task. |
required |
Returns:
| Type | Description |
|---|---|
Task
|
Task | None: The next scheduled task, or None if no task is available. |
Source code in lilota/stores.py
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 | |
get_task_by_id(id)
Return a task by its UUID.
Source code in lilota/stores.py
368 369 370 371 372 373 374 | |
get_unfinished_tasks()
Return all tasks that are not yet completed or failed.
Source code in lilota/stores.py
229 230 231 232 233 234 235 236 237 238 239 240 241 | |
has_unfinished_tasks()
Check if there are any unfinished tasks in the database.
Source code in lilota/stores.py
355 356 357 358 359 360 361 362 363 364 365 366 | |
release_tasks_of_dead_nodes(cutoff, exclude_node_id)
Give back the tasks that were taken by a node that is dead.
A worker that dies cannot finish the tasks it has taken, and no other
worker takes a task that is locked. The tasks get the status "created"
again, as if no worker had taken them, so that the next worker that is
free executes them. This is not an attempt: attempts stays as it is.
A node is dead if it was not seen since the cutoff, which is the rule of
NodeStore.update_status_on_dead_nodes, or if it does not exist. The
status of the node is not looked at, because a node that was taken for
dead and is seen again keeps the status DEAD.
A task that has run into its timeout is not given back. It has taken too
long, whatever happened to its worker, and is left to
expire_overdue_tasks and retry_tasks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cutoff
|
datetime
|
Time threshold to consider a node dead. |
required |
exclude_node_id
|
UUID
|
Node whose tasks are never given back. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of tasks that were given back. |
Source code in lilota/stores.py
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 | |
retry_tasks(batch_size=BATCH_SIZE)
Retry eligible tasks by creating new scheduled task instances.
This method scans for tasks that are eligible for retry based on their status, expiration, and retry limits. It uses optimistic locking to safely "claim" tasks in concurrent environments, ensuring that no task is retried more than once across multiple workers.
Eligible tasks include
- Tasks with status "failed" or "expired"
- Tasks with status "running" that have exceeded their expiration time
A task is only retried if
- It has not already been retried (
retried_atis None) - It has remaining retry attempts (
attempts < max_attempts)
For each claimed task
- A new task is created with incremented attempt count
- The retry is scheduled with a delay proportional to the number of attempts
- The original task is marked with
retried_atto prevent duplicate retries
Concurrency is handled using optimistic locking via the version field.
If another worker updates the task first, the current worker skips it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Maximum number of tasks to process in a single run. Defaults to BATCH_SIZE. |
BATCH_SIZE
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
The number of retry tasks successfully created. |
Raises:
| Type | Description |
|---|---|
SQLAlchemyError
|
If a database error occurs during the transaction. |
Notes
- Uses a linear backoff strategy: delay = 5 * current_attempts (seconds)
- All operations are executed within a single transactional session
- Safe for concurrent execution across multiple workers
Source code in lilota/stores.py
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 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 | |
set_progress(id, progress)
Update the progress percentage of a task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UUID
|
Id of the task. |
required |
progress
|
int
|
The progress of the task (0-100) |
required |
Source code in lilota/stores.py
578 579 580 581 582 583 584 585 586 587 588 | |
start_task(id, max_attempts, timeout=None, keep_input=True)
Mark a task as RUNNING and initialize metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UUID
|
Id of the task. |
required |
max_attempts
|
int
|
Upper limit on how many times the task may be executed. |
required |
timeout
|
timedelta | None
|
Optional timeout that can be set for a task. |
None
|
keep_input
|
bool
|
False to set the input to NULL once the task is finished for good. Written to the row, so that the leader knows it too when it expires or retries the task. |
True
|
Returns:
| Type | Description |
|---|---|
Task
|
Task | None: The started task, or None if no task is available. |
Source code in lilota/stores.py
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 | |
LogEntry
Bases: Base
Database model storing log messages generated by Lilota.
Source code in lilota/models.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
ModelProtocol
Bases: Protocol
Protocol for models that can be serialized to dictionaries.
Source code in lilota/models.py
134 135 136 137 138 139 140 | |
as_dict()
Return a dictionary representation of the model.
Source code in lilota/models.py
138 139 140 | |
Node
Bases: Base
Database model representing a Lilota node.
A node represents a running component of the system such as a scheduler or a worker.
Source code in lilota/models.py
245 246 247 248 249 250 251 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 | |
NodeLeader
Bases: Base
Database model representing the worker node currently acting as leader.
The leader is responsible for cluster-level maintenance tasks such as cleaning up stale nodes.
Source code in lilota/models.py
364 365 366 367 368 369 370 371 372 373 374 | |
NodeStatus
Bases: StrEnum
Enumeration describing the lifecycle state of a node.
Source code in lilota/models.py
36 37 38 39 40 41 42 43 44 45 | |
NodeType
Bases: StrEnum
Enumeration of supported node types.
Source code in lilota/models.py
48 49 50 51 52 | |
RegisteredTask
Wrapper representing a registered task function.
This class stores metadata about the task function such as input and output models and optionally a progress tracker.
It is responsible for: - deserializing the task input - executing the task function
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
Task function to execute. |
required |
input_model
|
Optional[Type]
|
Optional input model used to deserialize input payloads. |
required |
output_model
|
Optional[Type]
|
Return type of the task function. |
required |
input_param_name
|
Optional[str]
|
Name of the input parameter. |
required |
context_param_name
|
Optional[str]
|
Name of the context parameter. |
required |
timeout
|
Optional[timedelta]
|
Optional timeout that can be set for a task. |
required |
max_attempts
|
int
|
Upper limit on how many times the task may be executed. |
required |
keep_input
|
bool
|
False to remove the input of the task from the database as soon as the task is finished for good. |
True
|
Source code in lilota/models.py
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 | |
__call__(raw_input, task_context)
Execute the registered task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_input
|
Any
|
Raw input payload stored in the database. |
required |
task_context
|
TaskContext
|
Task context that contains a TaskProgress object and a logger object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
What the task function returns. It is converted when it is |
|
|
stored ( |
||
|
this method is an error of the task function. |
Source code in lilota/models.py
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 | |
Task
Bases: Base
Database model representing a scheduled task.
Tasks are created by the scheduler and executed by worker nodes. The model stores the execution state, input/output data, and runtime metadata.
Source code in lilota/models.py
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 | |
TaskContext
Runtime context injected into Lilota tasks.
The task context provides access to runtime-only services and metadata
during task execution. It is created by the worker runtime and injected
automatically into task functions that declare a TaskContext parameter.
Example
@worker.task def process(data: InputModel, ctx: TaskContext): ctx.logger.info("starting") ctx.progress.set(50)
Attributes:
| Name | Type | Description |
|---|---|---|
task_id |
Optional[UUID]
|
Unique identifier of the currently executing task. |
progress |
Optional[TaskProgress]
|
Progress tracking interface for updating task execution progress. |
logger |
Optional[Logger]
|
Logger instance associated with the current task execution. |
Source code in lilota/models.py
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 | |
__init__(*, task_id=None, progress=None, logger=None)
Initialize a task execution context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
Optional[UUID]
|
Unique identifier of the currently executing task. |
None
|
progress
|
Optional[TaskProgress]
|
Progress tracking interface for updating task progress. |
None
|
logger
|
Optional[Logger]
|
Logger instance associated with the task execution. |
None
|
Source code in lilota/models.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
TaskProgress
Helper object used to update task progress.
This object is passed to task functions when progress tracking is enabled. It allows the task to report its current progress to the task store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
UUID
|
Identifier of the task. |
required |
set_progress
|
Callable[[UUID, int], None]
|
Function used to persist progress updates. |
required |
Source code in lilota/models.py
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 | |
set(progress)
Update the progress of the task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
progress
|
int
|
Progress value (typically 0–100). |
required |
Source code in lilota/models.py
73 74 75 76 77 78 79 80 | |
TaskStatus
Bases: StrEnum
Enumeration of possible task states.
Source code in lilota/models.py
24 25 26 27 28 29 30 31 32 33 | |
Heartbeat
Bases: Thread
Background thread that executes a heartbeat task periodically.
The heartbeat repeatedly calls the provided :class:HeartbeatTask
at the configured interval until the thread is stopped.
Source code in lilota/heartbeat.py
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 | |
__init__(name, task, logger)
Initialize the heartbeat thread.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the thread. |
required |
task
|
HeartbeatTask
|
Task executed periodically by the heartbeat. |
required |
logger
|
Logger
|
Logger used for reporting errors. |
required |
Source code in lilota/heartbeat.py
41 42 43 44 45 46 47 48 49 50 51 52 | |
run()
Run the heartbeat loop.
The loop repeatedly executes the configured heartbeat task and waits for the defined interval before executing it again. The loop stops when the stop event is triggered.
Source code in lilota/heartbeat.py
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 | |
stop()
Signal the heartbeat thread to stop.
The thread will stop after the current execution cycle completes.
Source code in lilota/heartbeat.py
80 81 82 83 84 85 | |
stop_and_join(timeout=None)
Stop the heartbeat thread and wait for it to finish.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float | None
|
Maximum number of seconds to
wait for the thread to terminate. If |
None
|
Source code in lilota/heartbeat.py
87 88 89 90 91 92 93 94 95 96 97 | |
HeartbeatTask
Bases: ABC
Abstract base class for heartbeat tasks.
A heartbeat task defines logic that should be executed periodically
by a :class:Heartbeat thread.
Source code in lilota/heartbeat.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
__init__(interval, jitter)
Initialize the heartbeat task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
float
|
Interval in seconds between task executions. |
required |
jitter
|
float
|
Jitter that is used for the heartbeat interval. |
required |
Source code in lilota/heartbeat.py
14 15 16 17 18 19 20 21 22 | |
execute()
abstractmethod
Execute the heartbeat logic.
Subclasses must implement this method to define the work performed during each heartbeat cycle.
Source code in lilota/heartbeat.py
24 25 26 27 28 29 30 31 | |
ContextLogger
Bases: LoggerAdapter
Logger adapter that automatically injects contextual metadata.
This adapter attaches additional context such as node_id and
task_id to log records so that log entries can be associated
with specific nodes or tasks.
Source code in lilota/logging.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
process(msg, kwargs)
Inject contextual metadata into the log record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
str
|
Log message. |
required |
kwargs
|
dict
|
Keyword arguments passed to the logger. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Processed |
Source code in lilota/logging.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
LilotaLoggingFilter
Bases: Filter
Logging filter used to suppress noisy third-party logs.
Currently filters Alembic logs so that only warnings and errors are recorded.
Source code in lilota/logging.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
filter(record)
Determine whether a log record should be processed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
Log record being evaluated. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Source code in lilota/logging.py
98 99 100 101 102 103 104 105 106 107 108 109 | |
SqlAlchemyHandler
Bases: Handler
Logging handler that stores log records in the database.
This handler writes log messages to the lilota_log table using
the provided :class:LogStore. Each log record is converted
into a :class:LogEntry model instance.
Source code in lilota/logging.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 | |
__init__(log_store)
Initialize the logging handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_store
|
LogStore
|
Store used to persist log entries. |
required |
Source code in lilota/logging.py
16 17 18 19 20 21 22 23 | |
emit(record)
Persist a log record in the database.
The method never raises. A record that cannot be stored, for example because the database cannot be reached, is written to stderr instead. Otherwise code that logs an error would fail a second time, in its error handling, and a thread that only wanted to report a problem would end.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
Log record to store. |
required |
Source code in lilota/logging.py
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 | |
configure_logging(db_url, logger_name, logging_level)
Configure a Lilota logger that writes log messages to the database.
This function creates and configures a logger with the given name. The
logger uses :class:SqlAlchemyHandler to persist log records in the
database through :class:LogStore. Any existing handlers
attached to the logger are removed before configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_url
|
str
|
Database connection URL used by the log store. |
required |
logger_name
|
str
|
Name of the logger to configure. |
required |
logging_level
|
int
|
Logging level to apply to both the logger and its handler. |
required |
Returns:
| Type | Description |
|---|---|
Logger
|
logging.Logger: Configured logger instance that writes log messages to the database. |
Source code in lilota/logging.py
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 | |
create_context_logger(base_logger, **kwargs)
Create a context-aware logger.
The returned logger automatically attaches contextual metadata
(such as node_id and task_id) to all emitted log records.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_logger
|
Logger
|
Base logger instance. |
required |
**kwargs
|
Optional context values (e.g., |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
ContextLogger |
Logger adapter with contextual metadata. |
Source code in lilota/logging.py
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 | |
error_to_dict(error_message)
Wrap an error message string into a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error_message
|
str
|
The error message to wrap. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary with key "message" containing the error message. |
Source code in lilota/utils.py
29 30 31 32 33 34 35 36 37 38 | |
exception_to_dict(ex)
Convert an exception into a dictionary containing type, message, and traceback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ex
|
Exception
|
The exception to convert. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary with keys: - "type": Exception class name. - "message": Exception message string. - "traceback": Formatted traceback string. |
Source code in lilota/utils.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
normalize_data(data)
Normalize input data to a dictionary for storage or serialization.
Supports dict, ModelProtocol objects, and dataclasses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The input data to normalize. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary representation of the data. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in lilota/utils.py
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 | |
to_json(data)
Convert the return value of a task function to data that can be stored as JSON.
None,bool,int,floatandstrstay as they are.- A
dict, alistand atupleare converted value by value. A tuple becomes a list. - A
ModelProtocolobject and a dataclass become a dictionary. - A
datetime, adateand atimebecome text in ISO 8601 format. - A
UUIDand aDecimalbecome text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The return value to convert. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Data that only consists of JSON values. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in lilota/utils.py
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 | |