Learn how to create a Prefect worker to run your flows.
job_configuration
section of this base job template match the worker’s configuration class attributes. The values in the job_configuration
section of the base job template are used to populate the attributes of the worker’s configuration class.
The work pool creator gets to decide how they want to populate the values in the job_configuration
section of the base job template. The values can be hard-coded, templated using placeholders, or a mix of these two approaches. Because you, as the worker developer, don’t know how the work pool creator will populate the values, you should set sensible defaults for your configuration class attributes as a matter of best practice.
BaseJobConfiguration
SubclassBaseJobConfiguration
.
BaseJobConfiguration
has attributes that are common to all workers:
Attribute | Description |
---|---|
name | The name to assign to the created execution environment. |
env | Environment variables to set in the created execution environment. |
labels | The labels assigned to the created execution environment for metadata purposes. |
command | The command to use when starting a flow run. |
prepare_for_flow_run
method.
Here’s an example prepare_for_flow_run
method that adds a label to the execution environment:
job_configuration
section of the resulting base job template.
For this example, the base job template would look like this:
job_configuration
section with placeholders whose name matches the attribute name. The variables
section was also populated with the OpenAPI schema for each attribute. If a configuration class is used without explicitly declaring any template variables, the template variables will be inferred from the configuration class attributes.
memory
attribute, you can do so like this:
str
to accommodate the units, and we added a new json_schema_extra
attribute to each attribute. The template
key in json_schema_extra
is used to populate the job_configuration
section of the resulting base job template.
For this example, the job_configuration
section of the resulting base job template would look like this:
job_configuration
section, the key will be replaced with the value template variable.job_configuration
section and no value is provided for the template variable, the key will be removed from the job_configuration
section.aws_credentials
attribute in the UI and the worker will use these credentials when interacting with AWS resources.
BaseVariables
class. Like the BaseJobConfiguration
class, the BaseVariables
class has attributes that are common to all workers:
Attribute | Description |
---|---|
name | The name to assign to the created execution environment. |
env | Environment variables to set in the created execution environment. |
labels | The labels assigned the created execution environment for metadata purposes. |
command | The command to use when starting a flow run. |
BaseVariables
class to define additional template variables. For example, if you want to allow memory and CPU requests for your worker, you can do so like this:
MyWorkerTemplateVariables
is used in conjunction with MyWorkerConfiguration
from the Customizing Configuration Attribute Templates section, the resulting base job template will look like this:
variables
section of a base job template and validate the template variables provided by the user.
We don’t recommend using template variable classes within your worker implementation for validation purposes because the work pool creator ultimately defines the template variables. The configuration class should handle any necessary run-time validation.
BaseWorker
class and provide it with the following attributes:
Attribute | Description | Required |
---|---|---|
type | The type of the worker. | Yes |
job_configuration | The configuration class for the worker. | Yes |
job_configuration_variables | The template variables class for the worker. | No |
_documentation_url | Link to documentation for the worker. | No |
_logo_url | Link to a logo for the worker. | No |
_description | A description of the worker. | No |
run
run
method. The run
method is called for each flow run the worker receives for execution from the work pool.
The run
method has the following signature:
run
method is passed: the flow run to execute, the execution environment configuration for the flow run, and a task status object that allows the worker to track whether the flow run was submitted successfully.
The run
method must also return a BaseWorkerResult
object. The BaseWorkerResult
object returned contains information about the flow run execution. For the most part, you can implement the BaseWorkerResult
with no modifications like so:
BaseWorkerResult
class.
run
method is:
task_status
objectBaseWorkerResult
objectProcessWorker
and KubernetesWorker
implementations.
--type
option to the prefect worker start
CLI command. To make your worker type available via the CLI, it must be available at import time.
If your worker is in a package, you can add an entry point to your setup file in the following format: