Class: Aspera::Cli::Result::Poll

Inherits:
Result
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • parameters (Hash) —

    Value of option poll, see schema PollOptions

  • format (Symbol) (defaults to: nil) —

    Output format (--format)

Raises:



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)

Parameters:

  • action (Proc) —

    Executes the command, and returns its result



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)

Parameters:

  • result (Result) —

    Result of a command

Returns:

  • (Object) —

    JSON view of the data



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

Parameters:

  • value (Object) —

    Value of a JMESPath expression

Returns:

  • (Boolean)


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.

Parameters:

  • formatter (Formatter) —

    Displays each result, with formatting options

Returns:

  • (nil)

Raises:

  • (Cli::PollConditionNotMet) —

    if until is not true after the last run

  • (StandardError) —

    error of a run: with errors=fail, or of the last run with errors=retry



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?} #{timestamp} error: #{e.message}")
      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