https://dagster.io/ logo
Title
s

Stefan Adelbert

12/09/2021, 3:10 AM
Resource to Resource Dependencies If I have a resource,
A
, which depends on another resource,
B
, I can use
required_resource_keys
as described here: https://docs.dagster.io/concepts/resources#resource-to-resource-dependencies. But let's say that I want to have two instances of resource
A
configured differently, and I want those two instances to depend on two different instances of resource
B
- I don't know how to achieve that. If resource
A
looks like this:
@resource(required_resource_keys={"b"})
def A:
    return None
Then two instances of
A
,
a1
and
a2
, depend on an instance of a resource called
b
, but really I would like
a1
to depend on
b1
and
a2
to depend on
b2
. Is there a way to achieve this?
c

chris

12/09/2021, 8:05 PM
Hey Stefan - the case you're describing, where you have: two instances of resource 
A
 configured differently, and I want those two instances to depend on two different instances of resource 
B
Is a limitation of the existing system, tracked by this issue. Those two instances of resource
A
will implicitly depend on the same resource key, and there's no way to remap the dependent resource key to something else. This problem can occur for ops too in the same way (I have two configurations of the same op, that I want to depend on different resources)
s

Stefan Adelbert

12/09/2021, 9:30 PM
@chris Thanks for that. I have visions for a deepish hierarchy of dependent, reusable resources. But those visions might have to wait. I welcome any suggestions for workarounds.
c

chris

12/09/2021, 9:31 PM
Since this is all happening at definition time, you could create some sort of resource factory that takes in a config and a resource key and spits out a resource with the specifications you are imagining
def my_resource_factory(config, resource_key):
    @resource(required_resource_keys={resource_key}, config_schema=...)
    def my_resource():
         ...

    return my_resource.configured(config)
s

Stefan Adelbert

12/09/2021, 9:35 PM
Yes, yes! I was just thinking along those lines.
I think the resource factory idea actually solves both of the (my) resource-related problems at the moment (dynamic default config, dynamic dependencies). Nice one, mate.
c

chris

12/09/2021, 9:40 PM
yea I think this probably solves the dynamic default config story way nicer than the super jank solution I posted on the other thread 😅
s

Stefan Adelbert

12/09/2021, 10:01 PM
Yup, the factory solution is elegance. It's not a hack, not a workaround. It means that dagster can stay "pure" and not have to implement crazy schemes that potentially make it inflexible. And we can use nice (testable) patterns to give the dynamic behaviour we care about. Maybe this concept could be in the dagster docs under patterns.
👍 1
c

chris

12/09/2021, 10:12 PM
@Dagster Bot docs document the resource factory pattern as a solution for default config and resource remapping
d

Dagster Bot

12/09/2021, 10:12 PM