33
44from dataclasses import dataclass , field
55from decimal import Decimal , InvalidOperation
6- from typing import Callable , Iterable , Optional
6+ from typing import Any , Callable , Iterable , Optional
77
88from durabletask import task
99from durabletask .azuremanaged .internal import on_demand_sandbox_activities_service_pb2 as pb
1010from durabletask .azuremanaged .preview .on_demand_sandbox .helpers import (
11- _normalize_required ,
12- _resolve_activity_names ,
11+ normalize_required ,
12+ resolve_activity_names ,
1313)
1414
1515
@@ -32,17 +32,17 @@ class OnDemandSandboxWorkerProfileOptions:
3232 scheduler_managed_identity_client_id : Optional [str ] = None
3333 cpu : str = DEFAULT_CPU
3434 memory : str = DEFAULT_MEMORY
35- environment_variables : dict [str , str ] = field (default_factory = dict )
35+ environment_variables : dict [str , str ] = field (default_factory = dict [ str , str ] )
3636 max_concurrent_activities : int = DEFAULT_MAX_CONCURRENT_ACTIVITIES
37- entrypoint : list [str ] = field (default_factory = list )
38- cmd : list [str ] = field (default_factory = list )
39- activity_names : list [str ] = field (default_factory = list )
37+ entrypoint : list [str ] = field (default_factory = list [ str ] )
38+ cmd : list [str ] = field (default_factory = list [ str ] )
39+ activity_names : list [str ] = field (default_factory = list [ str ] )
4040
41- def add_activity (self , activity : str | Callable ) -> None :
41+ def add_activity (self , activity : str | Callable [..., Any ] ) -> None :
4242 """Add an activity to the on-demand sandbox worker profile declaration."""
4343 activity_name = task .get_name (activity ) if callable (activity ) else activity
4444 self .activity_names .append (
45- _normalize_required (activity_name , "On-demand sandbox activity name is required." ))
45+ normalize_required (activity_name , "On-demand sandbox activity name is required." ))
4646
4747
4848class OnDemandSandboxWorkerProfile :
@@ -57,7 +57,7 @@ def configure(self, options: OnDemandSandboxWorkerProfileOptions) -> None:
5757
5858def on_demand_sandbox_worker_profile (worker_profile_id : str ) -> Callable [[type ], type ]:
5959 """Declare an on-demand sandbox worker profile using a decorated marker class."""
60- normalized_profile = _normalize_required (worker_profile_id , "On-demand sandbox worker profile ID is required." )
60+ normalized_profile = normalize_required (worker_profile_id , "On-demand sandbox worker profile ID is required." )
6161
6262 def decorator (cls : type ) -> type :
6363 if normalized_profile in _worker_profiles :
@@ -73,7 +73,7 @@ def decorator(cls: type) -> type:
7373 if callable (configure ):
7474 configure (options )
7575
76- if not _resolve_activity_names (options .activity_names ):
76+ if not resolve_activity_names (options .activity_names ):
7777 raise ValueError (
7878 f"On-demand sandbox worker profile '{ normalized_profile } ' must declare at least one activity." )
7979
@@ -86,7 +86,7 @@ def decorator(cls: type) -> type:
8686def _build_on_demand_sandbox_activity_declaration (
8787 * ,
8888 activity_names : str | Iterable [str ],
89- scheduler_managed_identity_client_id : str ,
89+ scheduler_managed_identity_client_id : Optional [ str ] ,
9090 worker_profile_id : str = DEFAULT_WORKER_PROFILE_ID ,
9191 container_image : Optional [str ] = None ,
9292 image_pull_managed_identity_client_id : Optional [str ] = None ,
@@ -103,7 +103,7 @@ def _build_on_demand_sandbox_activity_declaration(
103103 such as "myregistry.azurecr.io/workers/hello:1.0" or
104104 "myregistry.azurecr.io/workers/hello@sha256:0123456789abcdef...".
105105 """
106- resolved_activity_names = _resolve_activity_names (activity_names )
106+ resolved_activity_names = resolve_activity_names (activity_names )
107107 if not resolved_activity_names :
108108 raise ValueError ("On-demand sandbox activity declaration requires at least one activity name." )
109109
@@ -113,16 +113,16 @@ def _build_on_demand_sandbox_activity_declaration(
113113 if max_concurrent_activities <= 0 :
114114 raise ValueError ("On-demand sandbox activity max concurrent activities must be greater than zero." )
115115
116- image_ref = _normalize_required (
116+ image_ref = normalize_required (
117117 container_image ,
118118 "On-demand sandbox activity image metadata requires a container image reference like "
119119 "'myregistry.azurecr.io/workers/hello:1.0' or "
120120 "'myregistry.azurecr.io/workers/hello@sha256:...'." )
121121
122- resolved_scheduler_managed_identity_client_id = _normalize_required (
122+ resolved_scheduler_managed_identity_client_id = normalize_required (
123123 scheduler_managed_identity_client_id ,
124124 "On-demand sandbox activity declaration requires the managed identity client ID workers use to connect to Durable Task Scheduler." )
125- resolved_image_pull_managed_identity_client_id = _normalize_required (
125+ resolved_image_pull_managed_identity_client_id = normalize_required (
126126 image_pull_managed_identity_client_id ,
127127 "On-demand sandbox activity declaration requires the managed identity client ID ADC uses to pull the worker image." )
128128
@@ -146,19 +146,19 @@ def _build_on_demand_sandbox_activity_declaration(
146146 return declaration
147147
148148
149- def _build_profile_on_demand_sandbox_activity_declarations () -> list [pb .OnDemandSandboxActivityDeclaration ]:
149+ def build_profile_on_demand_sandbox_activity_declarations () -> list [pb .OnDemandSandboxActivityDeclaration ]:
150150 """Build on-demand sandbox declarations from worker profile configuration."""
151151 declarations : list [pb .OnDemandSandboxActivityDeclaration ] = []
152152 activity_owners : dict [str , str ] = {}
153153 for profile in _worker_profiles .values ():
154- activity_names = _resolve_activity_names (profile .activity_names )
154+ activity_names = resolve_activity_names (profile .activity_names )
155155
156156 for activity_name in activity_names :
157157 existing_profile = activity_owners .get (activity_name )
158158 if existing_profile and existing_profile != profile .worker_profile_id :
159159 raise ValueError (
160160 f"On-demand sandbox activity '{ activity_name } ' is assigned to both worker profile "
161- f"'{ existing_profile } ' and '{ profile .worker_profile_id } '." )
161+ f"'{ existing_profile } ' and '{ profile .worker_profile_id } '." )
162162 activity_owners [activity_name ] = profile .worker_profile_id
163163
164164 declarations .append (_build_on_demand_sandbox_activity_declaration (
@@ -177,7 +177,7 @@ def _build_profile_on_demand_sandbox_activity_declarations() -> list[pb.OnDemand
177177 return declarations
178178
179179
180- def _build_on_demand_sandbox_worker_start (
180+ def build_on_demand_sandbox_worker_start (
181181 * ,
182182 taskhub : str ,
183183 worker_profile_id : str ,
@@ -194,7 +194,7 @@ def _build_on_demand_sandbox_worker_start(
194194 if max_activities_count <= 0 :
195195 raise ValueError ("On-demand sandbox activity worker max activity count must be greater than zero." )
196196
197- resolved_activity_names = _resolve_activity_names (activity_names )
197+ resolved_activity_names = resolve_activity_names (activity_names )
198198 if not resolved_activity_names :
199199 raise ValueError ("On-demand sandbox activity worker registration requires at least one registered activity." )
200200
@@ -209,7 +209,7 @@ def _build_on_demand_sandbox_worker_start(
209209 return message
210210
211211
212- def _build_on_demand_sandbox_worker_heartbeat (active_activities_count : int ) -> pb .OnDemandSandboxActivityWorkerMessage :
212+ def build_on_demand_sandbox_worker_heartbeat (active_activities_count : int ) -> pb .OnDemandSandboxActivityWorkerMessage :
213213 if active_activities_count < 0 :
214214 raise ValueError ("On-demand sandbox activity worker active activity count cannot be negative." )
215215
@@ -223,7 +223,7 @@ def _normalize_optional_strings(values: Iterable[str]) -> list[str]:
223223
224224
225225def _normalize_cpu (value : str ) -> str :
226- normalized = _normalize_required (value , "On-demand sandbox activity declaration requires CPU resources." )
226+ normalized = normalize_required (value , "On-demand sandbox activity declaration requires CPU resources." )
227227 milli_cpu = _try_parse_cpu_millicores (normalized )
228228 if milli_cpu is None or milli_cpu <= 0 :
229229 raise ValueError (
@@ -233,7 +233,7 @@ def _normalize_cpu(value: str) -> str:
233233
234234
235235def _normalize_memory (value : str ) -> str :
236- normalized = _normalize_required (value , "On-demand sandbox activity declaration requires memory resources." )
236+ normalized = normalize_required (value , "On-demand sandbox activity declaration requires memory resources." )
237237 memory_mib = _try_parse_memory_mib (normalized )
238238 if memory_mib is None or memory_mib <= 0 :
239239 raise ValueError (
0 commit comments