diff --git a/lib/temporal.rb b/lib/temporal.rb index 7c9060de..918860b5 100644 --- a/lib/temporal.rb +++ b/lib/temporal.rb @@ -90,6 +90,18 @@ def reset_workflow(namespace, workflow_id, run_id, workflow_task_id: nil, reason response.run_id end + def terminate_workflow(workflow_id, namespace: nil, run_id: nil, reason: nil, details: nil) + namespace ||= Temporal.configuration.namespace + + client.terminate_workflow_execution( + namespace: namespace, + workflow_id: workflow_id, + run_id: run_id, + reason: reason, + details: details + ) + end + def fetch_workflow_execution_info(namespace, workflow_id, run_id) response = client.describe_workflow_execution( namespace: namespace, diff --git a/lib/temporal/client/grpc_client.rb b/lib/temporal/client/grpc_client.rb index eab4f1a3..5e536a54 100644 --- a/lib/temporal/client/grpc_client.rb +++ b/lib/temporal/client/grpc_client.rb @@ -273,7 +273,6 @@ def reset_workflow_execution(namespace:, workflow_id:, run_id:, reason:, workflo workflow_execution: Temporal::Api::Common::V1::WorkflowExecution.new( workflow_id: workflow_id, run_id: run_id, - request_id: SecureRandom.uuid ), reason: reason, workflow_task_finish_event_id: workflow_task_event_id @@ -281,8 +280,25 @@ def reset_workflow_execution(namespace:, workflow_id:, run_id:, reason:, workflo client.reset_workflow_execution(request) end - def terminate_workflow_execution - raise NotImplementedError + def terminate_workflow_execution( + namespace:, + workflow_id:, + run_id:, + reason: nil, + details: nil + ) + request = Temporal::Api::WorkflowService::V1::TerminateWorkflowExecutionRequest.new( + identity: identity, + namespace: namespace, + workflow_execution: Temporal::Api::Common::V1::WorkflowExecution.new( + workflow_id: workflow_id, + run_id: run_id, + ), + reason: reason, + details: Serializer::Payload.new(details).to_proto + ) + + client.terminate_workflow_execution(request) end def list_open_workflow_executions diff --git a/spec/unit/lib/temporal_spec.rb b/spec/unit/lib/temporal_spec.rb index dec43f7b..eecc2331 100644 --- a/spec/unit/lib/temporal_spec.rb +++ b/spec/unit/lib/temporal_spec.rb @@ -160,6 +160,28 @@ class TestStartWorkflow < Temporal::Workflow end end + describe '.terminate_workflow' do + let(:temporal_response) do + Temporal::Api::WorkflowService::V1::TerminateWorkflowExecutionResponse.new + end + + before { allow(client).to receive(:terminate_workflow_execution).and_return(temporal_response) } + + it 'terminates a workflow' do + described_class.terminate_workflow('my-workflow', reason: 'just stop it') + + expect(client) + .to have_received(:terminate_workflow_execution) + .with( + namespace: 'default-namespace', + workflow_id: 'my-workflow', + reason: 'just stop it', + details: nil, + run_id: nil + ) + end + end + describe '.schedule_workflow' do let(:temporal_response) do Temporal::Api::WorkflowService::V1::StartWorkflowExecutionResponse.new(run_id: 'xxx') @@ -239,7 +261,7 @@ class TestStartWorkflow < Temporal::Workflow before { allow(client).to receive(:reset_workflow_execution).and_return(temporal_response) } - context 'when decision_task_id is provided' do + context 'when workflow_task_id is provided' do let(:workflow_task_id) { 42 } it 'calls client reset_workflow_execution' do