Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DualStateContextResourcesContainer on UnboundHookContext #16000

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def hook_invocation_result(
# Validate that all required resources are provided in the context

ensure_requirements_satisfied(
hook_context._resource_defs, list(hook_def.get_resource_requirements()) # noqa: SLF001
hook_context._resources_container.resource_defs, # noqa: SLF001
list(hook_def.get_resource_requirements()),
)

bound_context = BoundHookContext(
Expand Down
33 changes: 10 additions & 23 deletions python_modules/dagster/dagster/_core/execution/context/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
from ...definitions.dependency import Node
from ...definitions.hook_definition import HookDefinition
from ...definitions.op_definition import OpDefinition
from ...definitions.resource_definition import IContainsGenerator, Resources
from ...definitions.resource_definition import Resources
from ...errors import DagsterInvalidPropertyError, DagsterInvariantViolationError
from ...log_manager import DagsterLogManager
from ..plan.step import ExecutionStep
from ..plan.utils import RetryRequestedFromPolicy
from .dual_state_context import DualStateContextResourcesContainer
from .system import StepExecutionContext

if TYPE_CHECKING:
Expand Down Expand Up @@ -202,7 +203,6 @@ def __init__(
op_exception: Optional[Exception],
instance: Optional["DagsterInstance"],
):
from ..build_resources import build_resources, wrap_resources_for_execution
from ..context_creation_job import initialize_console_manager

self._op = None
Expand All @@ -214,11 +214,7 @@ def temp_graph():

self._op = temp_graph.nodes[0]

# Open resource context manager
self._resource_defs = wrap_resources_for_execution(resources)
self._resources_cm = build_resources(self._resource_defs)
self._resources = self._resources_cm.__enter__()
self._resources_contain_cm = isinstance(self._resources, IContainsGenerator)
self._resources_container = DualStateContextResourcesContainer(resources)

self._run_id = run_id
self._job_name = job_name
Expand All @@ -227,18 +223,15 @@ def temp_graph():

self._log = initialize_console_manager(None)

self._cm_scope_entered = False

def __enter__(self):
self._cm_scope_entered = True
def __enter__(self) -> "UnboundHookContext":
self._resources_container.call_on_enter()
return self

def __exit__(self, *exc: Any):
self._resources_cm.__exit__(*exc)
def __exit__(self, *exc: Any) -> None:
self._resources_container.call_on_exit()

def __del__(self):
if self._resources_contain_cm and not self._cm_scope_entered:
self._resources_cm.__exit__(None, None, None)
def __del__(self) -> None:
self._resources_container.call_on_del()

@property
def job_name(self) -> str:
Expand Down Expand Up @@ -274,13 +267,7 @@ def required_resource_keys(self) -> Set[str]:

@property
def resources(self) -> "Resources":
if self._resources_contain_cm and not self._cm_scope_entered:
raise DagsterInvariantViolationError(
"At least one provided resource is a generator, but attempting to access "
"resources outside of context manager scope. You can use the following syntax to "
"open a context manager: `with build_hook_context(...) as context:`"
)
return self._resources
return self._resources_container.get_resources("build_hook_context")

@property
def solid_config(self) -> Any:
Expand Down