@
dagster.
resource
(config_schema=None, description=None, required_resource_keys=None, version=None)[source]¶Define a resource.
The decorated function should accept an InitResourceContext
and return an instance of
the resource. This function will become the resource_fn
of an underlying
ResourceDefinition
.
If the decorated function yields once rather than returning (in the manner of functions
decorable with @contextlib.contextmanager
) then
the body of the function after the yield will be run after execution resolves, allowing users
to write their own teardown/cleanup logic.
config_schema (Optional[ConfigSchema]) – The schema for the config. Configuration data available in init_context.resource_config. If not set, Dagster will accept any config provided.
description (Optional[str]) – A human-readable description of the resource.
version (Optional[str]) – (Experimental) The version of a resource function. Two wrapped resource functions should only have the same version if they produce the same resource definition when provided with the same inputs.
required_resource_keys (Optional[Set[str]]) – Keys for the resources required by this resource.
dagster.
ResourceDefinition
(resource_fn, config_schema=None, description=None, required_resource_keys=None, version=None)[source]¶Core class for defining resources.
Resources are scoped ways to make external resources (like database connections) available to during job execution and to clean up after execution resolves.
If resource_fn yields once rather than returning (in the manner of functions decorable with
@contextlib.contextmanager
) then the body of the
function after the yield will be run after execution resolves, allowing users to write their
own teardown/cleanup logic.
Depending on your executor, resources may be instantiated and cleaned up more than once in a job execution.
resource_fn (Callable[[InitResourceContext], Any]) – User-provided function to instantiate
the resource, which will be made available to executions keyed on the
context.resources
object.
config_schema (Optional[ConfigSchema) – The schema for the config. If set, Dagster will check that config provided for the resource matches this schema and fail if it does not. If not set, Dagster will accept any config provided for the resource.
description (Optional[str]) – A human-readable description of the resource.
required_resource_keys – (Optional[Set[str]]) Keys for the resources required by this resource. A DagsterInvariantViolationError will be raised during initialization if dependencies are cyclic.
version (Optional[str]) – (Experimental) The version of the resource’s definition fn. Two wrapped resource functions should only have the same version if they produce the same resource definition when provided with the same inputs.
configured
(config_or_config_fn, config_schema=None, description=None)¶Wraps this object in an object of the same type that provides configuration to the inner object.
config_or_config_fn (Union[Any, Callable[[Any], Any]]) – Either (1) Run configuration
that fully satisfies this object’s config schema or (2) A function that accepts run
configuration and returns run configuration that fully satisfies this object’s
config schema. In the latter case, config_schema must be specified. When
passing a function, it’s easiest to use configured()
.
config_schema (ConfigSchema) – If config_or_config_fn is a function, the config schema that its input must satisfy.
description (Optional[str]) – Description of the new definition. If not specified, inherits the description of the definition being configured.
Returns (ConfigurableDefinition): A configured version of this object.
hardcoded_resource
(value, description=None)[source]¶A helper function that creates a ResourceDefinition
with a hardcoded object.
value (Any) – The value that will be accessible via context.resources.resource_name.
description ([Optional[str]]) – The description of the resource. Defaults to None.
A hardcoded resource.
mock_resource
(description=None)[source]¶A helper function that creates a ResourceDefinition
which wraps a mock.MagicMock
.
description ([Optional[str]]) – The description of the resource. Defaults to None.
you mock existing resources.
dagster.
InitResourceContext
(resource_config, resources, resource_def=None, instance=None, dagster_run=None, pipeline_run=None, log_manager=None, pipeline_def_for_backwards_compat=None)[source]¶Resource-specific initialization context.
resource_config
¶The configuration data provided by the run config. The schema
for this data is defined by the config_field
argument to
ResourceDefinition
.
Any
resource_def
¶The definition of the resource currently being constructed.
log_manager
¶The log manager for this run of the job or pipeline
resources
¶The resources that are available to the resource that we are initalizing.
ScopedResources
dagster_run
¶The dagster run to use. When initializing resources outside of execution context, this will be None.
Optional[PipelineRun]
run_id
¶The id for this run of the job or pipeline. When initializing resources outside of execution context, this will be None.
Optional[str]
pipeline_run
¶(legacy) The dagster run to use. When initializing resources outside of execution context, this will be None.
Optional[PipelineRun]
dagster.
make_values_resource
(**kwargs)[source]¶A helper function that creates a ResourceDefinition
to take in user-defined values.
This is useful for sharing values between ops.
**kwargs – Arbitrary keyword arguments that will be passed to the config schema of the returned resource definition. If not set, Dagster will accept any config provided for the resource.
For example:
@op(required_resource_keys={"globals"})
def my_op(context):
print(context.resources.globals["my_str_var"])
@job(resource_defs={"globals": make_values_resource(my_str_var=str, my_int_var=int)})
def my_job():
my_op()
A resource that passes in user-defined values.
dagster.
build_init_resource_context
(config=None, resources=None, instance=None)[source]¶Builds resource initialization context from provided parameters.
build_init_resource_context
can be used as either a function or context manager. If there is a
provided resource to build_init_resource_context
that is a context manager, then it must be
used as a context manager. This function can be used to provide the context argument to the
invocation of a resource.
resources (Optional[Dict[str, Any]]) – The resources to provide to the context. These can be either values or resource definitions.
config (Optional[Any]) – The resource config to provide to the context.
instance (Optional[DagsterInstance]) – The dagster instance configured for the context. Defaults to DagsterInstance.ephemeral().
Examples
context = build_init_resource_context()
resource_to_init(context)
with build_init_resource_context(
resources={"foo": context_manager_resource}
) as context:
resource_to_init(context)
dagster.
build_resources
(resources, instance=None, resource_config=None, pipeline_run=None, log_manager=None)[source]¶Context manager that yields resources using provided resource definitions and run config.
This API allows for using resources in an independent context. Resources will be initialized with the provided run config, and optionally, pipeline_run. The resulting resources will be yielded on a dictionary keyed identically to that provided for resource_defs. Upon exiting the context, resources will also be torn down safely.
resources (Dict[str, Any]) – Resource instances or definitions to build. All required resource dependencies to a given resource must be contained within this dictionary, or the resource build will fail.
instance (Optional[DagsterInstance]) – The dagster instance configured to instantiate resources on.
resource_config (Optional[Dict[str, Any]]) – A dict representing the config to be provided to each resource during initialization and teardown.
pipeline_run (Optional[PipelineRun]) – The pipeline run to provide during resource initialization and teardown. If the provided resources require either the pipeline_run or run_id attributes of the provided context during resource initialization and/or teardown, this must be provided, or initialization will fail.
log_manager (Optional[DagsterLogManager]) – Log Manager to use during resource initialization. Defaults to system log manager.
Examples:
from dagster import resource, build_resources
@resource
def the_resource():
return "foo"
with build_resources(resources={"from_def": the_resource, "from_val": "bar"}) as resources:
assert resources.from_def == "foo"
assert resources.from_val == "bar"