Skip to content

Commit dacbb5d

Browse files
committed
feat(python): auto-discover the activities in the class
1 parent 506b1fd commit dacbb5d

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

python/src/worker.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,47 @@
55
from workflows import HelloWorldWorkflow
66
from activities import Activities
77
import shared
8+
import inspect
89

910

1011
async def main():
1112
default_profile = ClientConfigProfile.load()
1213
connect_config = default_profile.to_client_connect_config()
1314

1415
client = await Client.connect(**connect_config)
16+
17+
# Register the activities - you may need to inject dependencies in here
1518
activities = Activities()
1619

1720
worker = Worker(
1821
client,
1922
task_queue=shared.TASK_QUEUE_NAME,
2023
workflows=[HelloWorldWorkflow],
21-
activities=[activities.sayName],
24+
activities=find_activities(activities),
2225
)
2326

2427
print("Worker started.")
2528
await worker.run()
2629

2730

31+
# ---- auto-discovery helper ----
32+
_ACTIVITY_ATTR = "__temporal_activity_definition" # set by @activity.defn
33+
34+
35+
def find_activities(obj):
36+
"""Return a list of bound methods on `obj` that are Temporal activities."""
37+
acts = []
38+
for _, member in inspect.getmembers(obj):
39+
if callable(member) and is_activity_callable(member):
40+
acts.append(member) # bound method carries injected deps
41+
return acts
42+
43+
44+
def is_activity_callable(attr) -> bool:
45+
# Works for functions and bound methods
46+
func = attr.__func__ if inspect.ismethod(attr) else attr
47+
return hasattr(func, _ACTIVITY_ATTR)
48+
49+
2850
if __name__ == "__main__":
2951
asyncio.run(main())

0 commit comments

Comments
 (0)