Class: Legion::CLI::ServiceCommand

Inherits:
Thor
  • Object
show all
Defined in:
lib/legion/cli/service_command.rb

Constant Summary collapse

SERVICE_LABEL =
'homebrew.mxcl.legionio'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/legion/cli/service_command.rb', line 13

def self.exit_on_failure?
  true
end

Instance Method Details

#restartObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/legion/cli/service_command.rb', line 76

def restart
  out = Output::Formatter.new(json: options[:json], color: !options[:no_color])
  ensure_macos!(out)

  uid = ::Process.uid
  target = "gui/#{uid}"

  Open3.capture2e('launchctl', 'bootout', "#{target}/#{SERVICE_LABEL}")
  sleep 1

  plist = plist_path
  Open3.capture2e('launchctl', 'bootstrap', target, plist) if File.exist?(plist)

  _, status = Open3.capture2e('launchctl', 'kickstart', '-k', "#{target}/#{SERVICE_LABEL}")
  if status.success?
    out.success('Legion service restarted')
  else
    out.error('Failed to restart Legion service')
    raise SystemExit, 1
  end

  poll_ready(out)
end

#startObject



27
28
29
30
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
# File 'lib/legion/cli/service_command.rb', line 27

def start
  out = Output::Formatter.new(json: options[:json], color: !options[:no_color])
  ensure_macos!(out)

  plist = plist_path
  unless File.exist?(plist)
    out.error("Service plist not found at #{plist}")
    out.info('Run: brew install legionio')
    raise SystemExit, 1
  end

  uid = ::Process.uid
  target = "gui/#{uid}"

  if service_loaded?(target)
    out.info('Service already loaded, kicking...')
  else
    _, status = Open3.capture2e('launchctl', 'bootstrap', target, plist)
    out.warn('bootstrap failed (may already be loaded), attempting kickstart anyway') unless status.success?
  end

  _, status = Open3.capture2e('launchctl', 'kickstart', '-k', "#{target}/#{SERVICE_LABEL}")
  if status.success?
    out.success('Legion service started')
  else
    out.error('Failed to kickstart Legion service')
    raise SystemExit, 1
  end

  poll_ready(out)
end

#statusObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/legion/cli/service_command.rb', line 101

def status
  out = Output::Formatter.new(json: options[:json], color: !options[:no_color])
  ensure_macos!(out)

  uid = ::Process.uid
  target = "gui/#{uid}"
  output, status = Open3.capture2e('launchctl', 'print', "#{target}/#{SERVICE_LABEL}")

  unless status.success?
    out.info('Service is not loaded')
    return
  end

  state = output[/state = (.+)/, 1] || 'unknown'
  pid = output[/pid = (\d+)/, 1]
  runs = output[/runs = (\d+)/, 1]

  if options[:json]
    puts Legion::JSON.dump({ state: state, pid: pid&.to_i, runs: runs&.to_i })
  else
    out.info("State: #{state}")
    out.info("PID: #{pid}") if pid
    out.info("Runs: #{runs}") if runs
  end
end

#stopObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/legion/cli/service_command.rb', line 60

def stop
  out = Output::Formatter.new(json: options[:json], color: !options[:no_color])
  ensure_macos!(out)

  uid = ::Process.uid
  target = "gui/#{uid}"

  _, status = Open3.capture2e('launchctl', 'bootout', "#{target}/#{SERVICE_LABEL}")
  if status.success?
    out.success('Legion service stopped')
  else
    out.warn('Service was not loaded (already stopped?)')
  end
end