Class: Anyt::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/anyt/command.rb

Overview

Runs system command (websocket server)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd = Anyt.config.command) ⇒ Command

Returns a new instance of Command.



22
23
24
# File 'lib/anyt/command.rb', line 22

def initialize(cmd = Anyt.config.command)
  @cmd = cmd
end

Class Attribute Details

.instanceObject (readonly)

Returns the value of attribute instance.



9
10
11
# File 'lib/anyt/command.rb', line 9

def instance
  @instance
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



20
21
22
# File 'lib/anyt/command.rb', line 20

def cmd
  @cmd
end

Class Method Details

.defaultObject



11
12
13
# File 'lib/anyt/command.rb', line 11

def default
  @instance ||= new
end

.restartObject



15
16
17
# File 'lib/anyt/command.rb', line 15

def restart
  instance&.restart
end

Instance Method Details

#restartObject

rubocop: enable Metrics/MethodLength rubocop: enable Metrics/AbcSize



56
57
58
59
60
61
62
63
64
65
# File 'lib/anyt/command.rb', line 56

def restart
  return unless running?

  AnyCable.logger.debug "Restarting command PID: #{process.pid}"

  stop
  process.wait

  run
end

#runObject Also known as: start



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/anyt/command.rb', line 26

def run
  return if running?

  return unless cmd

  AnyCable.logger.debug "Running command: #{cmd}"

  @process = ChildProcess.build(*cmd.split(/\s+/))

  process.io.inherit! if AnyCable.config.debug

  process.detach = true

  process.environment["ANYCABLE_DEBUG"] = "1" if AnyCable.config.debug?
  process.environment["ANYT_REMOTE_CONTROL_PORT"] = Anyt.config.remote_control_port
  process.environment["ACTION_CABLE_ADAPTER"] = "any_cable" unless Anyt.config.use_action_cable

  process.start

  AnyCable.logger.debug "Command PID: #{process.pid}"

  sleep Anyt.config.wait_command
  raise "Command failed to start" unless running?
end

#running?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/anyt/command.rb', line 75

def running?
  process&.alive?
end

#stopObject



67
68
69
70
71
72
73
# File 'lib/anyt/command.rb', line 67

def stop
  return unless running?

  AnyCable.logger.debug "Terminate PID: #{process.pid}"

  process.stop
end