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
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 348 349 350 | |
__init__(db_url, script_path, number_of_workers=cpu_count(), scheduler_heartbeat_interval=5, scheduler_timeout_sec=20, process_manager_interval=1.0, stop_worker_timeout=60, logging_level=logging.INFO)
Create a new Lilota runtime instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_url
|
str
|
Database connection URL used by the scheduler. |
required |
script_path
|
str
|
Path to a Python script that defines and registers Lilota tasks. Each worker process executes this script as its entry point. |
required |
number_of_workers
|
int
|
Number of worker processes to spawn. Defaults to the number of CPU cores. |
cpu_count()
|
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
|
Source code in lilota/core.py
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 | |
get_all_nodes()
Retrieve all registered scheduler nodes.
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
331 332 333 334 335 336 337 338 | |
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
341 342 343 344 345 346 347 348 349 350 | |
join()
Block until all worker processes have finished.
This is typically used in long-running worker setups where the main process should wait until workers exit.
Source code in lilota/core.py
307 308 309 310 311 312 313 314 315 | |
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 |
|---|---|---|
int |
int
|
Identifier of the scheduled task. |
Source code in lilota/core.py
318 319 320 321 322 323 324 325 326 327 328 | |
start()
Start the scheduler and spawn worker processes.
This method
- Starts the Lilota scheduler.
- Launches worker processes that execute the configured task script.
- Monitors the started processes.
After startup, the instance is ready to schedule and process tasks.
Source code in lilota/core.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
stop()
Stop the scheduler and terminate all worker processes.
Worker processes are first asked to terminate gracefully. If a worker
does not exit within stop_worker_timeout seconds, it will be
forcefully killed.
After completion, all worker processes are cleaned up and removed from the internal process list.
Source code in lilota/core.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
ProcessManagerTask
Bases: HeartbeatTask
Periodic task responsible for monitoring child processes and propagating exceptions raised inside them to the parent process.
The task checks a shared error queue for exceptions reported by child processes. If an error is found, it reconstructs and raises the exception in the parent process context so it can be handled or terminate execution.
Source code in lilota/core.py
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 | |
__init__(interval, processes, error_queue, process_factory)
Initialize the process manager task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
float
|
Time interval (in seconds) between task executions. |
required |
processes
|
list[Process]
|
List of managed child processes. |
required |
error_queue
|
Queue
|
Queue used by child processes to report exceptions back to the parent process. Each entry is expected to contain a dictionary with exception metadata. |
required |
process_factory
|
Callable[[], Process]
|
Function that creates a new worker process and returns it. |
required |
Source code in lilota/core.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
execute()
Execute one monitoring cycle.
Attempts to retrieve an error report from the error queue without
blocking. If an error is present, it will be reconstructed and raised
in the parent process via raise_child_exception.
The processes are also monitored. If a process is no longer alive,
it is removed from the list and a new worker process is started.
Source code in lilota/core.py
73 74 75 76 77 78 79 80 81 82 83 84 | |
raise_child_exception(error_info)
Reconstruct and raise an exception originating from a child process.
The error information is expected to be a dictionary containing
- "type": Name of the exception class
- "message": Original exception message
- "traceback": Serialized traceback string from the child process
The method attempts to recreate the exception using the built-in exception class matching the provided type name. If the exception type cannot be resolved, a RuntimeError is used as a fallback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error_info
|
dict
|
Dictionary containing exception metadata from a child process. |
required |
Raises:
| Type | Description |
|---|---|
Exception
|
Reconstructed exception with the original message and |
Source code in lilota/core.py
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 | |
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
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 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 | |
__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
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 | |
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
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
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
177 178 179 180 181 182 183 184 | |
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
168 169 170 171 172 173 174 | |
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
187 188 189 190 191 192 193 194 195 196 | |
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
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 | |
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
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 | |
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
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 | |
__init__(interval, node_id, node_store, logger)
Initialize the heartbeat task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
float
|
Interval in seconds between heartbeat executions. |
required |
node_id
|
str
|
Unique identifier of the node. |
required |
node_store
|
SqlAlchemyNodeStore
|
Store used for node operations. |
required |
logger
|
Logger
|
Logger instance used for reporting errors. |
required |
Source code in lilota/node.py
19 20 21 22 23 24 25 26 27 28 29 30 31 | |
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
34 35 36 37 38 39 40 41 42 43 | |
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 | |
__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 | |
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
83 84 85 86 87 88 89 | |
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 |
|---|---|---|
int |
int
|
Identifier of the created task. |
Source code in lilota/scheduler.py
69 70 71 72 73 74 75 76 77 78 79 80 | |
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
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 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 | |
__init__(db_url, node_heartbeat_interval=5.0, node_timeout_sec=20, task_heartbeat_interval=0.1, max_task_heartbeat_interval=5.0, set_progress_manually=False, logging_level=logging.INFO, **kwargs)
Initialize a worker 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
|
task_heartbeat_interval
|
float
|
Initial interval in seconds between polling attempts for tasks. Defaults to 0.1. |
0.1
|
max_task_heartbeat_interval
|
float
|
Maximum polling interval when no tasks are available. Defaults to 5.0. |
5.0
|
set_progress_manually
|
bool
|
User is responsible for setting |
False
|
logging_level
|
int
|
Logging level used by the worker. |
INFO
|
**kwargs
|
Additional keyword arguments passed to |
{}
|
Source code in lilota/worker.py
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 | |
register(name, *, input_model=None, output_model=None, task_progress=None, timeout=timedelta(minutes=5))
Decorator for registering a task function.
This method allows task registration using decorator syntax.
Example
@lilota.register("my_task") def my_task(data): return data
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique name of the task. |
required |
input_model
|
Optional[Type[Any]]
|
Optional input validation model. |
None
|
output_model
|
Optional[Type[Any]]
|
Optional output validation model. |
None
|
task_progress
|
Optional[TaskProgress]
|
Task progress tracking strategy. |
None
|
timeout
|
Optional[timedelta]
|
Optional timeout that can be set for a task. |
timedelta(minutes=5)
|
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
A decorator that registers the function. |
Source code in lilota/worker.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 215 216 217 218 219 220 221 222 223 224 | |
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
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 | |
__init__(interval, 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 |
node_id
|
str
|
Unique identifier of the worker node. |
required |
node_timeout_sec
|
int
|
Timeout in seconds after which nodes are considered dead. |
required |
node_store
|
SqlAlchemyNodeStore
|
Store used for node operations. |
required |
node_leader_store
|
SqlAlchemyNodeLeaderStore
|
Store used for leader election and leadership renewal. |
required |
logger
|
Logger
|
Logger instance. |
required |
Source code in lilota/worker.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
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
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
SqlAlchemyLogStore
Database store for logging entries into Lilota.
Source code in lilota/stores.py
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 | |
__init__(db_url)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_url
|
str
|
Database connection URL. |
required |
Source code in lilota/stores.py
400 401 402 403 404 405 406 407 | |
get_log_entries_by_node_id(node_id)
Return all log entries associated with a given node.
Source code in lilota/stores.py
425 426 427 428 429 430 431 432 433 | |
get_session()
Return a new SQLAlchemy session.
Source code in lilota/stores.py
419 420 421 422 | |
SqlAlchemyNodeLeaderStore
Bases: StoreBase
Store managing leader election for worker nodes.
Source code in lilota/stores.py
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 | |
__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
440 441 442 443 444 445 446 447 448 | |
delete_leader_by_id(id)
Delete the leader record by ID.
Returns True if deleted successfully, False otherwise.
Source code in lilota/stores.py
540 541 542 543 544 545 546 547 548 549 550 551 | |
renew_leadership(node_id)
Renew leadership lease for the given node.
Returns True if renewed successfully.
Source code in lilota/stores.py
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | |
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
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 | |
SqlAlchemyNodeStore
Bases: StoreBase
Database store for managing Lilota nodes.
Source code in lilota/stores.py
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 | |
__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
50 51 52 53 54 55 56 | |
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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
get_all_nodes()
Return all nodes in the database.
Source code in lilota/stores.py
81 82 83 84 | |
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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
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
140 141 142 143 144 145 146 147 148 149 150 151 152 | |
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
103 104 105 106 107 108 109 110 111 112 113 114 115 | |
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
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
SqlAlchemyTaskStore
Bases: StoreBase
Database store for managing Lilota tasks.
Source code in lilota/stores.py
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 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 | |
__init__(db_url, logger, set_progress_manually=False)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_url
|
str
|
Database connection URL. |
required |
logger
|
Logger
|
Logger for store operations. |
required |
set_progress_manually
|
bool
|
Whether task progress must be updated manually. |
False
|
Source code in lilota/stores.py
159 160 161 162 163 164 165 166 167 | |
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
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
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
367 368 369 370 371 372 373 374 375 376 377 378 379 | |
end_task_failure(id, error)
Mark a task as failed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UUID
|
Id of the task. |
required |
error
|
dict
|
Error information to store. |
required |
Source code in lilota/stores.py
353 354 355 356 357 358 359 360 361 362 363 364 | |
end_task_success(id, output)
Mark a task as successfully completed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UUID
|
Id of the task. |
required |
output
|
Any
|
Task result data. |
required |
Source code in lilota/stores.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
expire_overdue_tasks()
Set status to "expired" for running tasks whose expiration time has passed.
Source code in lilota/stores.py
213 214 215 216 217 218 219 220 221 222 223 224 225 | |
get_all_tasks()
Return all tasks ordered by ID.
Source code in lilota/stores.py
196 197 198 199 | |
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
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 | |
get_task_by_id(id)
Return a task by its UUID.
Source code in lilota/stores.py
244 245 246 247 248 249 250 | |
get_unfinished_tasks()
Return all tasks that are not yet completed or failed.
Source code in lilota/stores.py
202 203 204 205 206 207 208 209 210 | |
has_unfinished_tasks()
Check if there are any unfinished tasks in the database.
Source code in lilota/stores.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
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
323 324 325 326 327 328 329 330 331 332 333 | |
start_task(id, timeout=None)
Mark a task as RUNNING and initialize metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UUID
|
Id of the task. |
required |
timeout
|
timedelta | None
|
Optional timeout that can be set for a task. |
None
|
Returns:
| Type | Description |
|---|---|
Task
|
Task | None: The started task, or None if no task is available. |
Source code in lilota/stores.py
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 | |
StoreBase
Bases: ABC
Abstract base class for all database stores.
Provides common initialization and session management.
Source code in lilota/stores.py
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 | |
__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
20 21 22 23 24 25 26 27 28 29 | |
LogEntry
Bases: Base
Database model storing log messages generated by Lilota.
Source code in lilota/models.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
ModelProtocol
Bases: Protocol
Protocol for models that can be serialized to dictionaries.
Source code in lilota/models.py
73 74 75 76 77 78 79 | |
as_dict()
Return a dictionary representation of the model.
Source code in lilota/models.py
77 78 79 | |
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
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 | |
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
282 283 284 285 286 287 288 289 290 291 292 | |
NodeStatus
Bases: StrEnum
Enumeration describing the lifecycle state of a node.
Source code in lilota/models.py
25 26 27 28 29 30 31 32 33 | |
NodeType
Bases: StrEnum
Enumeration of supported node types.
Source code in lilota/models.py
37 38 39 40 | |
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 - serializing the output
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]
|
Optional output model used to serialize the task result. |
required |
task_progress
|
Optional[TaskProgress]
|
Optional progress helper. |
required |
timeout
|
Optional[timedelta]
|
Optional timeout that can be set for a task. |
required |
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 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 | |
__call__(raw_input, task_progress)
Execute the registered task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_input
|
Any
|
Raw input payload stored in the database. |
required |
task_progress
|
TaskProgress
|
Progress tracking object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Serialized task result. |
Source code in lilota/models.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 | |
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
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 | |
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
|
int
|
Identifier of the task. |
required |
set_progress
|
Callable[[int, int], None]
|
Function used to persist progress updates. |
required |
Source code in lilota/models.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
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
61 62 63 64 65 66 67 | |
TaskStatus
Bases: StrEnum
Enumeration of possible task states.
Source code in lilota/models.py
13 14 15 16 17 18 19 20 21 | |
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
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 | |
__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
40 41 42 43 44 45 46 47 48 49 50 51 | |
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 | |
stop()
Signal the heartbeat thread to stop.
The thread will stop after the current execution cycle completes.
Source code in lilota/heartbeat.py
76 77 78 79 80 81 | |
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
84 85 86 87 88 89 90 91 92 | |
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
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
__init__(interval)
Initialize the heartbeat task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
float
|
Interval in seconds between task executions. |
required |
Source code in lilota/heartbeat.py
13 14 15 16 17 18 19 | |
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
22 23 24 25 26 27 28 29 | |
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
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
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
79 80 81 82 83 84 85 86 87 88 89 90 | |
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:SqlAlchemyLogStore. Each log record is converted
into a :class:LogEntry model instance.
Source code in lilota/logging.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 32 33 34 35 36 37 38 39 40 41 42 43 | |
__init__(log_store)
Initialize the logging handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_store
|
SqlAlchemyLogStore
|
Store used to persist log entries. |
required |
Source code in lilota/logging.py
15 16 17 18 19 20 21 22 | |
emit(record)
Persist a log record in the database.
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 | |
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:SqlAlchemyLogStore. 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
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 | |
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
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
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
26 27 28 29 30 31 32 33 34 35 36 37 | |
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
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
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
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 | |