Class: Mxup::Watcher
- Inherits:
-
Object
- Object
- Mxup::Watcher
- Defined in:
- lib/mxup/watcher.rb
Overview
Re-evaluates the commands declared in config.live_env and restarts
dependent windows when a command's output changes.
Typically launched as a detached child process from Runner#up via
mxup watch <session>; can also be invoked directly in the foreground
for debugging.
Constant Summary collapse
- POLL_INTERVAL =
2
Class Method Summary collapse
- .log_path(session, runtime_root: RUNTIME_DIR) ⇒ Object
- .pid_path(session, runtime_root: RUNTIME_DIR) ⇒ Object
-
.state_path(session, runtime_root: RUNTIME_DIR) ⇒ Object
Path of the watcher state file inside the session's runtime dir.
Instance Method Summary collapse
- #commands ⇒ Object
-
#initialize(config, session: config.session, launcher: nil, out: $stdout, err: $stderr, interval: POLL_INTERVAL) ⇒ Watcher
constructor
A new instance of Watcher.
- #run ⇒ Object
-
#tick ⇒ Object
Exposed for tests.
Constructor Details
#initialize(config, session: config.session, launcher: nil, out: $stdout, err: $stderr, interval: POLL_INTERVAL) ⇒ Watcher
Returns a new instance of Watcher.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/mxup/watcher.rb', line 17 def initialize(config, session: config.session, launcher: nil, out: $stdout, err: $stderr, interval: POLL_INTERVAL) @config = config @session = session @launcher = launcher || Launcher.new(config) @out = out @err = err @interval = interval @stop = false @cmd_to_windows = build_index @outputs = @cmd_to_windows.keys.each_with_object({}) { |c, h| h[c] = evaluate(c) } @state = { pid: Process.pid, started_at: Time.now.to_i, last_restart: {} } end |
Class Method Details
.log_path(session, runtime_root: RUNTIME_DIR) ⇒ Object
40 41 42 |
# File 'lib/mxup/watcher.rb', line 40 def self.log_path(session, runtime_root: RUNTIME_DIR) File.join(runtime_root, session, 'watcher.log') end |
.pid_path(session, runtime_root: RUNTIME_DIR) ⇒ Object
36 37 38 |
# File 'lib/mxup/watcher.rb', line 36 def self.pid_path(session, runtime_root: RUNTIME_DIR) File.join(runtime_root, session, 'watcher.pid') end |
.state_path(session, runtime_root: RUNTIME_DIR) ⇒ Object
Path of the watcher state file inside the session's runtime dir.
32 33 34 |
# File 'lib/mxup/watcher.rb', line 32 def self.state_path(session, runtime_root: RUNTIME_DIR) File.join(runtime_root, session, 'watcher.state') end |
Instance Method Details
#commands ⇒ Object
68 69 70 |
# File 'lib/mxup/watcher.rb', line 68 def commands @cmd_to_windows.keys end |
#run ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mxup/watcher.rb', line 44 def run install_signal_handlers write_state log "watching #{@cmd_to_windows.size} command(s) for session #{@session}" until @stop unless Tmux.has_session?(@session) log "session #{@session} is gone; exiting" break end @cmd_to_windows.each_key { |cmd| check(cmd) } sleep @interval end cleanup_state 0 end |
#tick ⇒ Object
Exposed for tests.
64 65 66 |
# File 'lib/mxup/watcher.rb', line 64 def tick @cmd_to_windows.each_key { |cmd| check(cmd) } end |