-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_server.rb
More file actions
executable file
·186 lines (153 loc) · 5.65 KB
/
Copy pathtest_server.rb
File metadata and controls
executable file
·186 lines (153 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'grpc'
require 'tucana'
Tucana.load_protocol(:aquila)
class State
attr_reader :data_types, :runtime_functions, :flow_types, :action_config_definitions
def initialize
@data_types = []
@runtime_functions = []
@flow_types = []
@action_config_definitions = []
end
def add_flow_types(flow_types)
@flow_types += flow_types
end
def add_data_types(dts)
@data_types += dts
end
def add_runtime_functions(functions)
@runtime_functions += functions
end
def add_action_config_definition(config)
@action_config_definitions << config
end
end
class FlowTypeTransferService < Tucana::Aquila::FlowTypeService::Service
def initialize(state)
@state = state
end
def update(req, call)
puts "Got Auth token: #{call.metadata['authorization']}"
@state.add_flow_types(req.flow_types)
puts "Flowtypes received: #{req.flow_types.map(&:identifier).join(', ')}"
Tucana::Aquila::RuntimeFunctionDefinitionUpdateResponse.new(success: true)
end
end
class RuntimeFunctionDefinitionTransferService < Tucana::Aquila::RuntimeFunctionDefinitionService::Service
def initialize(state)
@state = state
end
def update(req, call)
puts "Got Auth token: #{call.metadata['authorization']}"
@state.add_runtime_functions(req.runtime_functions)
puts "RuntimeFunctionDefinitions received: #{req.runtime_functions.map(&:runtime_name).join(', ')}"
Tucana::Aquila::RuntimeFunctionDefinitionUpdateResponse.new(success: true)
end
end
class DataTypeTransferService < Tucana::Aquila::DataTypeService::Service
def initialize(state)
@state = state
end
def update(req, call)
puts "Got Auth token: #{call.metadata['authorization']}"
@state.add_data_types(req.data_types)
puts "Data types received: #{req.data_types.map(&:identifier).join(', ')}"
Tucana::Aquila::DataTypeUpdateResponse.new(success: true)
end
end
class ActionTransferService < Tucana::Aquila::ActionTransferService::Service
def initialize(state)
@state = state
@sent_configs = []
end
def construct_parameters(func)
struct = Tucana::Shared::Struct.new
func.runtime_parameter_definitions.each do |param_def|
struct.fields[param_def.runtime_name] = param_def.default_value
end
struct
end
def transfer(requests, call)
token = call.metadata['authorization']
puts "Got Auth token: #{token}"
Enumerator.new do |yielder|
Thread.new do
loop do
sleep 5
@state.action_config_definitions.each do |config|
next if @sent_configs.include?(config[:identifier])
puts "Received action config definition: #{config[:action_identifier]} (v#{config[:version]})"
count = Random.rand(1..5)
puts "Creating #{count} mock project config"
proj_conf = []
count.times do |i|
proj_conf << Tucana::Shared::ActionProjectConfiguration.new(
project_id: Random.rand(1..1000),
action_configurations: [
Tucana::Shared::ActionConfiguration.new(
identifier: config[:identifier],
value: Tucana::Shared::Value.from_ruby("test value for project: #{i}")
)
]
)
end
yielder << Tucana::Aquila::TransferResponse.new(
{
action_configurations:
Tucana::Shared::ActionConfigurations.new(action_configurations: proj_conf),
}
)
@sent_configs << config[:identifier]
end
@state.runtime_functions.each do |func|
puts "Sending execute request for: #{func.runtime_name}"
exec_identifier = "exec_#{func.runtime_name}_#{Time.now.to_i}"
puts "Execution identifier: #{exec_identifier}"
yielder << Tucana::Aquila::TransferResponse.new(
{
execution: Tucana::Aquila::ExecutionRequest.new(
project_id: Random.rand(1..1000),
execution_identifier: exec_identifier,
function_identifier: func.runtime_name,
parameters: construct_parameters(func)
),
}
)
end
end
end
requests.each do |req|
unless req.result.nil?
puts "Received execution result for: #{req.result.execution_identifier}"
puts "Result value: #{req.result.result}"
next
end
unless req.event.nil?
puts "Received event: #{req.event.event_type}"
puts "Event data: #{req.event.payload}"
puts "Project id: #{req.event.project_id}"
next
end
next if req.logon.nil?
logon = req.logon
@state.add_action_config_definition({
auth_token: token,
action_identifier: logon.action_identifier,
version: logon.version,
config_definitions: logon.action_configurations,
})
puts "Action logon received: #{logon.action_identifier} (v#{logon.version})"
end
end
end
end
state = State.new
server = GRPC::RpcServer.new
server.add_http2_port('0.0.0.0:50051', :this_port_is_insecure)
server.handle(ActionTransferService.new(state))
server.handle(DataTypeTransferService.new(state))
server.handle(RuntimeFunctionDefinitionTransferService.new(state))
server.handle(FlowTypeTransferService.new(state))
server.run_till_terminated_or_interrupted(%w[INT TERM QUIT])