Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #37319 - Allow setting IDs for outputs #135

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions lib/smart_proxy_dynflow/continuous_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,20 @@ def humanize
raw_outputs.map { |output| output['output'] }.join("\n")
end

def add_exception(context, exception, timestamp = Time.now.getlocal)
add_output(context + ": #{exception.class} - #{exception.message}", 'debug', timestamp)
def add_exception(context, exception, timestamp: Time.now.getlocal, id: nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this an API change?

def add_exception(context, exception, timestamp = 'now')
  puts timestamp
end

def add_exception_new(context, exception, timestamp: 'now')
  puts timestamp
end

add_exception('a', 'b')
add_exception('a', 'b', 'explicit')

add_exception_new('a', 'b')
add_exception_new('a', 'b', 'explicit')
$ ruby test2.rb
now
explicit
now
test2.rb:5:in `add_exception_new': wrong number of arguments (given 3, expected 2) (ArgumentError)
	from test2.rb:13:in `<main>'

Or do you accept it since it only appears to be used internally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this isn't strictly private api, none of the smart proxy plugins which depend on sp-dynflow seem to use this, so I'd be willing to let it slide.

add_output(context + ": #{exception.class} - #{exception.message}", 'debug', timestamp: timestamp, id: id)
end

def add_output(*args)
add_raw_output(self.class.format_output(*args))
def add_output(...)
add_raw_output(self.class.format_output(...))
end

def self.format_output(message, type = 'debug', timestamp = Time.now.getlocal)
{ 'output_type' => type,
'output' => message,
'timestamp' => timestamp.to_f }
def self.format_output(message, type = 'debug', timestamp: Time.now.getlocal, id: nil)
base = { 'output_type' => type,
'output' => message,
'timestamp' => timestamp.to_f }
base['id'] = id if id
base
end
end
end
4 changes: 2 additions & 2 deletions lib/smart_proxy_dynflow/runner/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def timeout_interval
# or nil for no timeout
end

def publish_data(data, type)
@continuous_output.add_output(data, type)
def publish_data(...)
@continuous_output.add_output(...)
end

def publish_exception(context, exception, fatal = true)
Expand Down
8 changes: 4 additions & 4 deletions lib/smart_proxy_dynflow/runner/parent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ def host_action(identifier)
Dynflow::Action::Suspended.new OpenStruct.new(options)
end

def broadcast_data(data, type)
@outputs.each_value { |output| output.add_output(data, type) }
def broadcast_data(...)
@outputs.each_value { |output| output.add_output(...) }
end

def publish_data(_data, _type)
true
end

def publish_data_for(identifier, data, type)
@outputs[identifier].add_output(data, type)
def publish_data_for(identifier, data, type, **kwargs)
@outputs[identifier].add_output(data, type, **kwargs)
end

def dispatch_exception(context, exception)
Expand Down
87 changes: 86 additions & 1 deletion test/runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,39 @@ class RunnerTest < Minitest::Spec
it 'returns a hash with outputs' do
message = 'a message'
type = 'stdout'
runner.publish_data(message, type)
now = Time.now
runner.publish_data(message, type, timestamp: now)
updates = runner.generate_updates
_(updates.keys).must_equal [suspended_action]
update = updates.values.first
_(update.exit_status).must_be :nil?
_(update.continuous_output.raw_outputs.count).must_equal 1
end

it 'accepts timestamp' do
now = Time.now
runner.publish_data('message', 'stdout')
update = runner.generate_updates.values.first
_(update.continuous_output.raw_outputs.first['timestamp']).must_be_instance_of Float
_(update.continuous_output.raw_outputs.first['timestamp']).wont_equal now.to_f

runner.publish_data('message', 'stdout', timestamp: now)
update = runner.generate_updates.values.first
_(update.continuous_output.raw_outputs.first['timestamp']).must_be_instance_of Float
_(update.continuous_output.raw_outputs.first['timestamp']).must_equal now.to_f
end

it 'accepts id' do
runner.publish_data('message', 'stdout')
update = runner.generate_updates.values.first
_(update.continuous_output.raw_outputs.first.key?('id')).must_equal false

id = 5
runner.publish_data('message', 'stdout', id: id)
update = runner.generate_updates.values.first
_(update.continuous_output.raw_outputs.first['id']).must_equal id
end

it 'works in compatibility mode' do
runner = Base.new
message = 'a message'
Expand Down Expand Up @@ -80,13 +105,73 @@ class RunnerTest < Minitest::Spec
runner.publish_data_for('foo', 'message', 'stdout')
_(runner.generate_updates.keys.count).must_equal 1
end

it 'accepts timestamp' do
now = Time.now
runner.publish_data_for('foo', 'message', 'stdout')
update = runner.generate_updates.values.first
_(update.continuous_output.raw_outputs.first['timestamp']).must_be_instance_of Float
_(update.continuous_output.raw_outputs.first['timestamp']).wont_equal now.to_f

runner.publish_data_for('foo', 'message', 'stdout', timestamp: now)
update = runner.generate_updates.values.first
_(update.continuous_output.raw_outputs.first['timestamp']).must_be_instance_of Float
_(update.continuous_output.raw_outputs.first['timestamp']).must_equal now.to_f
end

it 'accepts id' do
runner.publish_data_for('foo', 'message', 'stdout')
update = runner.generate_updates.values.first
_(update.continuous_output.raw_outputs.first.key?('id')).must_equal false

id = 5
runner.publish_data_for('foo', 'message', 'stdout', id: id)
update = runner.generate_updates.values.first
_(update.continuous_output.raw_outputs.first['id']).must_equal id
end
end

describe '#broadcast_data' do
it 'publishes data for all hosts' do
runner.broadcast_data('message', 'stdout')
_(runner.generate_updates.keys.count).must_equal 2
end

it 'accepts timestamp' do
now = Time.now
runner.broadcast_data('message', 'stdout')
updates = runner.generate_updates.values
_(updates.count).must_equal 2
updates.each do |update|
_(update.continuous_output.raw_outputs.first['timestamp']).must_be_instance_of Float
_(update.continuous_output.raw_outputs.first['timestamp']).wont_equal now.to_f
end

runner.broadcast_data('message', 'stdout', timestamp: now)
updates = runner.generate_updates.values
_(updates.count).must_equal 2
updates.each do |update|
_(update.continuous_output.raw_outputs.first['timestamp']).must_be_instance_of Float
_(update.continuous_output.raw_outputs.first['timestamp']).must_equal now.to_f
end
end

it 'accepts id' do
runner.broadcast_data('message', 'stdout')
updates = runner.generate_updates.values
_(updates.count).must_equal 2
updates.each do |update|
_(update.continuous_output.raw_outputs.first.key?('id')).must_equal false
end

id = 5
runner.broadcast_data('message', 'stdout', id: id)
updates = runner.generate_updates.values
_(updates.count).must_equal 2
updates.each do |update|
_(update.continuous_output.raw_outputs.first['id']).must_equal id
end
end
end

describe '#publish_exception' do
Expand Down