@@ -5,6 +5,7 @@ class Workflow
55 class History
66 class EventTarget
77 class UnexpectedEventType < InternalError ; end
8+ class UnexpectedCommandType < InternalError ; end
89
910 ACTIVITY_TYPE = :activity
1011 CANCEL_ACTIVITY_REQUEST_TYPE = :cancel_activity_request
@@ -19,7 +20,7 @@ class UnexpectedEventType < InternalError; end
1920 UPSERT_SEARCH_ATTRIBUTES_REQUEST_TYPE = :upsert_search_attributes_request
2021
2122 # NOTE: The order is important, first prefix match wins (will be a longer match)
22- TARGET_TYPES = {
23+ EVENT_TARGET_TYPES = {
2324 'ACTIVITY_TASK_CANCEL_REQUESTED' => CANCEL_ACTIVITY_REQUEST_TYPE ,
2425 'ACTIVITY_TASK' => ACTIVITY_TYPE ,
2526 'REQUEST_CANCEL_ACTIVITY_TASK' => CANCEL_ACTIVITY_REQUEST_TYPE ,
@@ -38,25 +39,58 @@ class UnexpectedEventType < InternalError; end
3839 'WORKFLOW_EXECUTION' => WORKFLOW_TYPE ,
3940 } . freeze
4041
41- attr_reader :id , :type
42+ WORKFLOW_TARGET_TYPES = {
43+ 'Temporal::Workflow::Command::ScheduleActivity' => ACTIVITY_TYPE ,
44+ 'Temporal::Workflow::Command::RequestActivityCancellation' => CANCEL_ACTIVITY_REQUEST_TYPE ,
45+ 'Temporal::Workflow::Command::RecordMarker' => MARKER_TYPE ,
46+ 'Temporal::Workflow::Command::StartTimer' => TIMER_TYPE ,
47+ 'Temporal::Workflow::Command::CancelTimer' => CANCEL_TIMER_REQUEST_TYPE ,
48+ 'Temporal::Workflow::Command::CompleteWorkflow' => WORKFLOW_TYPE ,
49+ 'Temporal::Workflow::Command::FailWorkflow' => WORKFLOW_TYPE ,
50+ 'Temporal::Workflow::Command::StartChildWorkflow' => CHILD_WORKFLOW_TYPE ,
51+ 'Temporal::Workflow::Command::SignalExternalWorkflow' => EXTERNAL_WORKFLOW_TYPE ,
52+ 'Temporal::Workflow::Command::CancelExternalWorkflow' => CANCEL_EXTERNAL_WORKFLOW_REQUEST_TYPE ,
53+ 'Temporal::Workflow::Command::UpsertSearchAttributes' => UPSERT_SEARCH_ATTRIBUTES_REQUEST_TYPE ,
54+ 'Temporal::Workflow::Command::ContinueAsNew' => WORKFLOW_TYPE ,
55+ } . freeze
56+
57+ COMMAND_ATTRIBUTE_LISTS = {
58+ 'Temporal::Workflow::Command::ScheduleActivity' => [ :activity_id , :activity_type , :input ] ,
59+ }
60+ attr_reader :id , :type , :attributes
4261
4362 def self . workflow
4463 @workflow ||= new ( 1 , WORKFLOW_TYPE )
4564 end
4665
4766 def self . from_event ( event )
48- _ , target_type = TARGET_TYPES . find { |type , _ | event . type . start_with? ( type ) }
67+ _ , target_type = EVENT_TARGET_TYPES . find { |type , _ | event . type . start_with? ( type ) }
4968
5069 unless target_type
5170 raise UnexpectedEventType , "Unexpected event #{ event . type } "
5271 end
5372
54- new ( event . originating_event_id , target_type )
73+ new ( event . originating_event_id , target_type , attributes : event . target_attributes )
74+ end
75+
76+ def self . from_command ( command_id , command )
77+
78+ command_type = command . class . name
79+ target_type = WORKFLOW_TARGET_TYPES [ command_type ]
80+
81+ unless target_type
82+ raise UnexpectedCommandType , "Unexpected command type #{ command_type } "
83+ end
84+
85+ attribute_list = COMMAND_ATTRIBUTE_LISTS . fetch ( command_type , [ ] )
86+
87+ new ( command_id , target_type , attributes : command . to_h . slice ( *attribute_list ) )
5588 end
5689
57- def initialize ( id , type )
90+ def initialize ( id , type , attributes : { } )
5891 @id = id
5992 @type = type
93+ @attributes = attributes
6094
6195 freeze
6296 end
@@ -74,7 +108,7 @@ def hash
74108 end
75109
76110 def to_s
77- "#{ type } ( #{ id } )"
111+ "#{ type } : #{ id } ( #{ attributes } )"
78112 end
79113 end
80114 end
0 commit comments