Module: Bcome::LoadingBar::Handler

Included in:
Driver::Base, Driver::Gcp::Authentication::Base, Node::Base, Node::Inventory::Defined, Ssh::Connector
Defined in:
lib/objects/loading_bar/handler.rb

Instance Method Summary collapse

Instance Method Details

#cursorObject



16
17
18
# File 'lib/objects/loading_bar/handler.rb', line 16

def cursor
  ::TTY::Cursor
end

#do_signal(signal) ⇒ Object



51
52
53
# File 'lib/objects/loading_bar/handler.rb', line 51

def do_signal(signal)
  ::Process.kill(signal, @pid)
end

#fork_processObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/objects/loading_bar/handler.rb', line 36

def fork_process
  @pid = fork do
    Signal.trap(::Bcome::LoadingBar::Indicator::Base::SIGNAL_SUCCESS) do
      @indicator.increment_success
    end

    Signal.trap(::Bcome::LoadingBar::Indicator::Base::SIGNAL_FAILURE) do
      @indicator.increment_failure
    end

    @indicator.indicate
  end
  ::Bcome::LoadingBar::PidBucket.instance << @pid
end

#signal_failureObject



73
74
75
76
77
# File 'lib/objects/loading_bar/handler.rb', line 73

def signal_failure
  do_signal(::Bcome::LoadingBar::Indicator::Base::SIGNAL_FAILURE)
  # Keep parent indicator in sync (see #signal_stop)
  @indicator.increment_failure
end

#signal_stopObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/objects/loading_bar/handler.rb', line 55

def signal_stop
  # Stop the loader
  do_signal(::Bcome::LoadingBar::Indicator::Base::SIGNAL_STOP)

  # De-register its pid
  ::Bcome::LoadingBar::PidBucket.instance - @pid

  # Show the PARENT process indicator, which has been kept in sync with the forked process. We do this due to race condition where the forked indicator
  # does not gets final status before it is killed, so that it looks as if it has not completed
  @indicator.show
end

#signal_successObject



67
68
69
70
71
# File 'lib/objects/loading_bar/handler.rb', line 67

def signal_success
  do_signal(::Bcome::LoadingBar::Indicator::Base::SIGNAL_SUCCESS)
  # Keep parent indicator in sync (see #signal_stop)
  @indicator.increment_success
end

#start_indicator(config) ⇒ Object



6
7
8
9
10
# File 'lib/objects/loading_bar/handler.rb', line 6

def start_indicator(config)
  klass = config[:type] == :progress ? ::Bcome::LoadingBar::Indicator::Progress : ::Bcome::LoadingBar::Indicator::Basic
  @indicator = klass.new(config)
  fork_process
end

#stop_indicatorObject



12
13
14
# File 'lib/objects/loading_bar/handler.rb', line 12

def stop_indicator
  signal_stop
end

#wrap_indicator(config, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/objects/loading_bar/handler.rb', line 20

def wrap_indicator(config, &block)
  begin
    start_indicator(config)
    cursor.invisible do
      block.call
    end
  rescue IRB::Abort
    stop_indicator
    raise ::Bcome::Exception::Generic, 'Interrupt'
  rescue StandardError => e
    stop_indicator
    raise ::Bcome::Exception::Generic, e.message if config[:type] == :basic
  end
  stop_indicator
end