Class: HaveAPI::CLI::ActionState
- Inherits:
-
Object
- Object
- HaveAPI::CLI::ActionState
- Defined in:
- lib/haveapi/cli/action_state.rb
Overview
This class can watch action’s state and show it’s progress with a progress bar.
When interrupted, the user is asked whether he wishes to cancel the action, then it shows the progress of cancellation.
Methods from this class may invoke ‘exit()` whenever appropriate.
Instance Method Summary collapse
-
#cancel_action(timeout: nil) ⇒ Object
Ask the user if he wishes to cancel the action.
-
#initialize(opts, client, id) ⇒ ActionState
constructor
A new instance of ActionState.
- #print_help(id = nil) ⇒ Object
-
#wait_for_completion(id: nil, timeout: nil, cancel: false) ⇒ Object
Block until the action is finished or timeout is reached.
Constructor Details
#initialize(opts, client, id) ⇒ ActionState
Returns a new instance of ActionState.
14 15 16 17 18 |
# File 'lib/haveapi/cli/action_state.rb', line 14 def initialize(opts, client, id) @opts = opts @client = client @id = id end |
Instance Method Details
#cancel_action(timeout: nil) ⇒ Object
Ask the user if he wishes to cancel the action. If so, execute cancel and call self.wait_for_completion on the cancellation, if it is blocking operation.
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 |
# File 'lib/haveapi/cli/action_state.rb', line 71 def cancel_action(timeout: nil) $stdout.write('Do you wish to cancel the action? [y/N]: ') $stdout.flush return unless $stdin.readline.strip.downcase == 'y' begin res = HaveAPI::Client::Action.cancel(@client, @id) rescue HaveAPI::Client::ActionFailed => e res = e.response end if res.is_a?(HaveAPI::Client::Response) && res.ok? puts 'Cancelled' exit elsif res @pb.resume wait_for_completion( id: res, timeout: timeout, cancel: true ) exit end warn "Cancel failed: #{res.}" exit(false) end |
#print_help(id = nil) ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/haveapi/cli/action_state.rb', line 102 def print_help(id = nil) id ||= @id puts 'Run' puts " #{$0} action_state show #{id}" puts 'or' puts " #{$0} action_state wait #{id}" puts "to check the action's progress." end |
#wait_for_completion(id: nil, timeout: nil, cancel: false) ⇒ Object
Block until the action is finished or timeout is reached. Progress is shown with a progress bar. Offers cancellation on interrupt.
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 |
# File 'lib/haveapi/cli/action_state.rb', line 25 def wait_for_completion(id: nil, timeout: nil, cancel: false) id ||= @id if cancel puts 'Waiting for the action to cancel (hit Ctrl+C to skip)...' else puts 'Waiting for the action to complete (hit Ctrl+C to skip)...' end last_status = false can_cancel = false begin ret = HaveAPI::Client::Action.wait_for_completion( @client, id, timeout: timeout ) do |state| last_status = state.status can_cancel = state.can_cancel? update_progress(state, cancel) end rescue Interrupt @pb && @pb.stop puts cancel_action(timeout: timeout) if can_cancel && !cancel && last_status puts print_help(id) exit(false) end if ret @pb && @pb.finish else @pb && @pb.stop end ret end |