|
17 | 17 | from cadence.api.v1.service_workflow_pb2 import ( |
18 | 18 | StartWorkflowExecutionRequest, |
19 | 19 | StartWorkflowExecutionResponse, |
| 20 | + SignalWithStartWorkflowExecutionRequest, |
| 21 | + SignalWithStartWorkflowExecutionResponse, |
20 | 22 | ) |
21 | 23 | from cadence.api.v1.common_pb2 import WorkflowType, WorkflowExecution |
22 | 24 | from cadence.api.v1.tasklist_pb2 import TaskList |
@@ -229,6 +231,72 @@ async def start_workflow( |
229 | 231 | except Exception: |
230 | 232 | raise |
231 | 233 |
|
| 234 | + async def signal_with_start_workflow( |
| 235 | + self, |
| 236 | + workflow: Union[str, Callable], |
| 237 | + signal_name: str, |
| 238 | + signal_input: Any = None, |
| 239 | + *args, |
| 240 | + **options_kwargs: Unpack[StartWorkflowOptions], |
| 241 | + ) -> WorkflowExecution: |
| 242 | + """ |
| 243 | + Signal a workflow execution, starting it if it is not already running. |
| 244 | +
|
| 245 | + Args: |
| 246 | + workflow: Workflow function or workflow type name string |
| 247 | + signal_name: Name of the signal |
| 248 | + signal_input: Input data for the signal |
| 249 | + *args: Arguments to pass to the workflow if it needs to be started |
| 250 | + **options_kwargs: StartWorkflowOptions as keyword arguments |
| 251 | +
|
| 252 | + Returns: |
| 253 | + WorkflowExecution with workflow_id and run_id |
| 254 | +
|
| 255 | + Raises: |
| 256 | + ValueError: If required parameters are missing or invalid |
| 257 | + Exception: If the gRPC call fails |
| 258 | + """ |
| 259 | + # Convert kwargs to StartWorkflowOptions and validate |
| 260 | + options = _validate_and_apply_defaults(StartWorkflowOptions(**options_kwargs)) |
| 261 | + |
| 262 | + # Build the start workflow request |
| 263 | + start_request = await self._build_start_workflow_request(workflow, args, options) |
| 264 | + |
| 265 | + # Encode signal input |
| 266 | + signal_payload = None |
| 267 | + if signal_input is not None: |
| 268 | + try: |
| 269 | + signal_payload = await self.data_converter.to_data(signal_input) |
| 270 | + except Exception as e: |
| 271 | + raise ValueError(f"Failed to encode signal input: {e}") |
| 272 | + |
| 273 | + # Build the SignalWithStartWorkflowExecution request |
| 274 | + request = SignalWithStartWorkflowExecutionRequest( |
| 275 | + start_request=start_request, |
| 276 | + signal_name=signal_name, |
| 277 | + ) |
| 278 | + |
| 279 | + if signal_payload: |
| 280 | + request.signal_input.CopyFrom(signal_payload) |
| 281 | + |
| 282 | + # Execute the gRPC call |
| 283 | + try: |
| 284 | + response: SignalWithStartWorkflowExecutionResponse = ( |
| 285 | + await self.workflow_stub.SignalWithStartWorkflowExecution(request) |
| 286 | + ) |
| 287 | + |
| 288 | + # Emit metrics if available |
| 289 | + if self.metrics_emitter: |
| 290 | + # TODO: Add metrics similar to Go client |
| 291 | + pass |
| 292 | + |
| 293 | + execution = WorkflowExecution() |
| 294 | + execution.workflow_id = start_request.workflow_id |
| 295 | + execution.run_id = response.run_id |
| 296 | + return execution |
| 297 | + except Exception: |
| 298 | + raise |
| 299 | + |
232 | 300 |
|
233 | 301 | def _validate_and_copy_defaults(options: ClientOptions) -> ClientOptions: |
234 | 302 | if "target" not in options: |
|
0 commit comments