Class: RBWatch::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/rbwatch/runner.rb

Constant Summary collapse

CRASH_LOOP_THRESHOLD =
1.0

Instance Method Summary collapse

Constructor Details

#initialize(configuration:, dashboard: nil, scanner: nil, process_manager: nil, watcher: nil, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }) ⇒ Runner

Returns a new instance of Runner.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rbwatch/runner.rb', line 7

def initialize(configuration:, dashboard: nil, scanner: nil, process_manager: nil, watcher: nil, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })
  @configuration = configuration
  @theme = configuration.theme
  @dashboard = dashboard || Dashboard.new(output: configuration.stderr, theme: @theme, clear: configuration.clear?)
  @scanner = scanner || FileScanner.new(configuration)
  @process_manager = process_manager || ProcessManager.new(
    command: configuration.command,
    stdout: configuration.stdout,
    stderr: configuration.stderr
  )
  @watcher = watcher || Watcher.new(
    scanner: @scanner,
    delay: configuration.delay,
    poll_interval: poll_interval
  )
  @clock = clock
  @started_at = current_time
  @process_started_at = nil
  @restart_count = 0
  @shutdown_requested = false
  @restart_suspended = false
  @previous_signal_handlers = {}
end

Instance Method Details

#runObject



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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rbwatch/runner.rb', line 31

def run
  ensure_command!
  install_signal_handlers

  dashboard.clear_screen if configuration.clear?
  dashboard.hide_cursor

  dashboard.render_banner(
    version: VERSION,
    command: configuration.command_string,
    watch_paths: configuration.watch_paths,
    config_file: configuration.config_file,
    theme_name: configuration.theme_name
  ) if configuration.banner?

  dashboard.log_info("Starting process...")
  dashboard.log_info("Watching #{scanner.count} files")
  dashboard.log_info("Loaded config file: #{configuration.config_file}") if configuration.config_file
  dashboard.log_info("Extensions: #{configuration.extensions.join(', ')}") if configuration.verbose?
  dashboard.log_info("Ignoring: #{configuration.ignore.join(', ')}") if configuration.verbose?
  dashboard.log_info("Theme: #{configuration.theme_name}") if configuration.verbose?

  spinner = dashboard.spinner("Starting application...")
  spinner.auto_spin
  process_manager.start
  mark_process_started
  spinner.success("Application started")

  dashboard.log_success("Process started (PID #{process_manager.pid})")
  render_status_card
  dashboard.render_footer

  watcher.reset(scanner.scan)
  watch_loop

  0
rescue Theme::UnknownThemeError => e
  dashboard.log_error(e.message)
  1
rescue StandardError => e
  dashboard.log_error("#{e.class}: #{e.message}")
  dashboard.log_error(e.backtrace.first) if configuration.verbose? && e.backtrace
  1
ensure
  dashboard.log_warning("Received #{@shutdown_signal}, shutting down...") if @shutdown_signal
  watcher.stop
  process_manager.stop
  dashboard.show_cursor
  restore_signal_handlers
end