Task Scheduler¶
The task scheduler TaskScheduler inherits from the TaskConsumer to add scheduling of periodic tasks.
It can be imported from fluid.scheduler
:
fluid.scheduler.TaskScheduler
¶
Bases: TaskConsumer
A task manager for scheduling tasks
Source code in fluid/scheduler/scheduler.py
deps
instance-attribute
¶
Dependencies for the task manager.
Production applications requires global dependencies to be
available to all tasks. This can be achieved by setting
the deps
attribute of the task manager to an object
with the required dependencies.
Each task can cast the dependencies to the required type.
config
instance-attribute
¶
config = config or TaskManagerConfig(**kwargs)
Task manager configuration
dispatcher
instance-attribute
¶
dispatcher = TaskDispatcher()
A dispatcher of TaskRun events.
Application can register handlers to listen for events happening during the lifecycle of a task run.
num_concurrent_tasks
property
¶
The number of concurrent_tasks running in the consumer
has_started
¶
is_running
¶
is_stopping
¶
is_stopped
¶
gracefully_stop
¶
after_shutdown
¶
Called after shutdown of worker
By default it does nothing, but can be overriden to do something such as exit the process.
status
async
¶
on_startup
async
¶
on_shutdown
async
¶
startup
async
¶
start the worker
This method creates a task to run the worker.
Source code in fluid/utils/worker.py
shutdown
async
¶
Shutdown a running worker and wait for it to stop
This method will try to gracefully stop the worker and wait for it to stop. If the worker does not stop in the grace period, it will force shutdown by cancelling the task.
Source code in fluid/utils/worker.py
wait_for_shutdown
async
¶
Wait for the worker to stop
This method will wait for the worker to stop running, but doesn't try to gracefully stop it nor force shutdown.
Source code in fluid/utils/worker.py
workers
¶
run
async
¶
Source code in fluid/utils/worker.py
add_workers
¶
add workers to the workers
They can be added while the worker is running.
add_async_context_manager
¶
Add an async context manager to the task manager
These context managers are entered when the task manager starts
register_task
¶
Register a task with the task manager
PARAMETER | DESCRIPTION |
---|---|
task
|
Task to register
TYPE:
|
execute
async
¶
Execute a task and wait for it to finish
PARAMETER | DESCRIPTION |
---|---|
task
|
The task or task name, if a task name it must be registered with the task manager.
TYPE:
|
run_id
|
Unique ID for the task run. If not provided a new UUID is generated.
TYPE:
|
priority
|
Override the default task priority if provided
TYPE:
|
**params
|
The optional parameters for the task run. They must match the task params model
TYPE:
|
Source code in fluid/scheduler/consumer.py
execute_sync
¶
Execute a task synchronously
This method is a blocking method that should be used in a synchronous context.
PARAMETER | DESCRIPTION |
---|---|
task
|
The task or task name, if a task name it must be registered with the task manager.
TYPE:
|
run_id
|
Unique ID for the task run. If not provided a new UUID is generated.
TYPE:
|
priority
|
Override the default task priority if provided
TYPE:
|
**params
|
The optional parameters for the task run. They must match the task params model
TYPE:
|
Source code in fluid/scheduler/consumer.py
queue
async
¶
Queue a task for execution
This methods fires two events:
init
: when the task run is createdqueued
: after the task is queued
It returns the TaskRun object
PARAMETER | DESCRIPTION |
---|---|
task
|
The task or task name, if a task name it must be registered with the task manager.
TYPE:
|
run_id
|
Unique ID for the task run. If not provided a new UUID is generated.
TYPE:
|
priority
|
Override the default task priority if provided
TYPE:
|
**params
|
The optional parameters for the task run. They must match the task params model
TYPE:
|
Source code in fluid/scheduler/consumer.py
create_task_run
¶
Create a TaskRun in init
state
PARAMETER | DESCRIPTION |
---|---|
task
|
The task or task name, if a task name it must be registered with the task manager.
TYPE:
|
run_id
|
Unique ID for the task run. If not provided a new UUID is generated.
TYPE:
|
priority
|
Override the default task priority if provided
TYPE:
|
**params
|
The optional parameters for the task run. They must match the task params model
TYPE:
|
Source code in fluid/scheduler/consumer.py
register_from_module
¶
PARAMETER | DESCRIPTION |
---|---|
module
|
Python module with tasks implementations - can contain any object, only instances of Task are registered
TYPE:
|
Source code in fluid/scheduler/consumer.py
register_from_dict
¶
PARAMETER | DESCRIPTION |
---|---|
data
|
Python dictionary with tasks implementations - can contain any object, only instances of Task are registered
TYPE:
|
Source code in fluid/scheduler/consumer.py
register_async_handler
¶
Source code in fluid/scheduler/consumer.py
unregister_async_handler
¶
sync_queue
¶
num_concurrent_tasks_for
¶
queue_and_wait
async
¶
Queue a task and wait for it to finish