Class: Necro::Runner

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

Class Method Summary collapse

Class Method Details

.add_command(selected_command) ⇒ Object



70
71
72
73
74
75
# File 'lib/necro/runner.rb', line 70

def self.add_command(selected_command)
  socket = UNIXSocket.open(Necro::CommandListener::Server::SOCKET_PATH)
  socket.puts("add #{selected_command.command_key}")
  socket.flush()
  socket.close()
end

.refresh_command(selected_command) ⇒ Object



84
85
86
87
88
89
# File 'lib/necro/runner.rb', line 84

def self.refresh_command(selected_command)
  socket = UNIXSocket.open(Necro::CommandListener::Server::SOCKET_PATH)
  socket.puts("reload #{selected_command.command_key}")
  socket.flush()
  socket.close()
end

.remove_command(selected_command) ⇒ Object



77
78
79
80
81
82
# File 'lib/necro/runner.rb', line 77

def self.remove_command(selected_command)
  socket = UNIXSocket.open(Necro::CommandListener::Server::SOCKET_PATH)
  socket.puts("remove #{selected_command.command_key} #{selected_command.signal}")
  socket.flush()
  socket.close()
end

.run(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/necro/runner.rb', line 7

def self.run(args)

  selected_command = nil
  
  Slop.parse(args, help: true) do
    on :v, "Print the version" do
      $stdout.puts Necro::VERSION
    end

    command 'start' do
      banner "Usage : necro start config.ini \n Start Necro Process Manager"
      run do |cmd_opts, cmd_args|
        selected_command = OpenStruct.new(:command => 'start', :file => cmd_args.first)
      end
    end

    command 'add' do
      banner "Usage : necro add process_label \n Start the process with given process_label"
      run do |cmd_opts, cmd_args|
        selected_command = OpenStruct.new(:command => 'add', :command_key => cmd_args.first)
      end
    end

    command 'remove' do
      banner "Usage : necro remove process_label \n Stop the process with given label"
      on :s, :signal=, "Signal to send for killing the process, default is SIGINT", as: String

      run do |cmd_opts, cmd_args|
        signal_to_use = cmd_opts.to_hash[:signal] || 'INT'
        selected_command = OpenStruct.new(
          :command => 'remove', 
          :command_key => cmd_args.first,
          :signal => signal_to_use
        )
      end
    end
  end
  run_command(selected_command)
end

.run_command(selected_command) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/necro/runner.rb', line 47

def self.run_command(selected_command)
  return unless selected_command
  case selected_command.command
  when 'start'
    start_server(selected_command)
  when 'add'
    add_command(selected_command)
  when 'remove'
    remove_command(selected_command)
  else
    $stdout.puts "Invalid command"
  end
end

.start_server(selected_command) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/necro/runner.rb', line 61

def self.start_server(selected_command)
  config = Necro::Config.new(selected_command.file)
  Necro.const_set(:CONFIG, config)
  warn_about_terminal_notifier()
  commander = Necro::Commander.new()
  Necro.const_set(:COMMANDER, commander)
  commander.start_manager()
end

.warn_about_terminal_notifierObject



91
92
93
94
95
96
97
98
# File 'lib/necro/runner.rb', line 91

def self.warn_about_terminal_notifier
  if RUBY_PLATFORM.downcase.include?("darwin")
    command_path = `which terminal-notifier`
    if !command_path || command_path.empty?
      $stdout.puts("You can enable OSX notification for processes by installing terminal-notification gem".red)
    end
  end
end