Class: Aspera::Cli::Result::Poll
- Inherits:
-
Result
- Object
- Result
- Aspera::Cli::Result::Poll
- Defined in:
- lib/aspera/cli/poll.rb
Overview
Result of a read-only command executed with option poll: the command is executed when the result is displayed,
once or several times, and each result is displayed according to show.
The command is not executed by Runner#run_with_result (MCP server): only by Runner#run, which displays the result.
Instance Attribute Summary collapse
- #action ⇒ Object writeonly
Class Method Summary collapse
-
.json_view(result) ⇒ Object
Data of a result, as seen by a JMESPath expression of
--out.query: JSON values (keys and objects likeTimebecome strings). -
.truthy?(value) ⇒ Boolean
Truth of a JMESPath value:
false,null, empty string, empty list and empty object are false.
Instance Method Summary collapse
-
#format(formatter) ⇒ nil
Execute the command (once or several times), and display results.
-
#initialize(parameters, format: nil) ⇒ Poll
constructor
Parameters are checked before the first run: keys, types and values by the schema of the option, the rest here.
Constructor Details
#initialize(parameters, format: nil) ⇒ Poll
Parameters are checked before the first run: keys, types and values by the schema of the option, the rest here.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/aspera/cli/poll.rb', line 73 def initialize(parameters, format: nil) super() # A `null` value removes the entry (as for all options): the parameter is absent, its default applies parameters = parameters.transform_keys(&:to_sym).compact # Nagios plugins exit after the first result raise BadArgument, 'poll: not with format nagios' if format.eql?(:nagios) @interval = seconds(parameters.fetch(:interval, DEFAULT_INTERVAL), :interval) @max = parameters[:max] @timeout = parameters[:timeout].nil? ? nil : seconds(parameters[:timeout], :timeout) @until = parameters[:until] Formatter.check_jmespath(@until, 'poll.until') unless @until.nil? @show = parameters.fetch(:show, SHOW_MODES.first).to_sym @errors = parameters.fetch(:errors, ERROR_MODES.first).to_sym # Set by the dispatcher: executes the command, and returns its result @action = nil end |
Instance Attribute Details
#action=(value) ⇒ Object (writeonly)
91 92 93 |
# File 'lib/aspera/cli/poll.rb', line 91 def action=(value) @action = value end |
Class Method Details
.json_view(result) ⇒ Object
Data of a result, as seen by a JMESPath expression of --out.query: JSON values (keys and objects like Time become strings)
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/aspera/cli/poll.rb', line 56 def json_view(result) data = case result when Result::Empty then [] when Result::Special then nil else result.data end # A scalar is used as is: it may be binary (e.g. an image) return data.is_a?(Symbol) ? data.to_s : data unless data.is_a?(Hash) || data.is_a?(Array) JSON.parse(JSON.generate(data)) end |
.truthy?(value) ⇒ Boolean
Truth of a JMESPath value: false, null, empty string, empty list and empty object are false
47 48 49 50 51 |
# File 'lib/aspera/cli/poll.rb', line 47 def truthy?(value) return false if value.nil? || value.eql?(false) return !value.empty? if value.is_a?(String) || value.is_a?(Array) || value.is_a?(Hash) true end |
Instance Method Details
#format(formatter) ⇒ nil
Execute the command (once or several times), and display results.
Ctrl-C stops polling: the last result is displayed with show=last, then Interrupt is raised again.
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 |
# File 'lib/aspera/cli/poll.rb', line 99 def format(formatter) Aspera.assert(!@action.nil?) { 'poll: no action' } start = clock run = 0 met = false previous = nil last = nil interrupted = false # Error of the last run, with `errors=retry` error = nil begin loop do run += 1 error = nil begin result = @action.call rescue StandardError => e case @errors when :fail then raise when :retry then error = e else Aspera.error_unexpected_value(@errors) { 'poll.errors' } end status(formatter, "poll: run #{run}#{"/#{@max}" unless @max.nil?} #{} error: #{e.}") end if error.nil? Aspera.assert_type(result, Result) { 'result of command' } met = self.class.truthy?(evaluate(formatter, result)) unless @until.nil? output = render(formatter, result) case @show when :each then display(formatter, run, output) when :change then display(formatter, run, output) unless output.eql?(previous) when :last then last = [run, output] else Aspera.error_unexpected_value(@show) { 'poll.show' } end previous = output end break if met || (!@max.nil? && run >= @max) wait = @interval unless @timeout.nil? remaining = start + @timeout - clock break unless remaining.positive? wait = [wait, remaining].min end pause(wait) end rescue Interrupt interrupted = true end display(formatter, *last) unless last.nil? raise Interrupt if interrupted raise error unless error.nil? status(formatter, "poll: condition met at run #{run}") if met raise PollConditionNotMet, "condition not met after #{run} run#{'s' unless run.eql?(1)}: #{@until}" unless @until.nil? || met nil end |