Class: OsCtld::Cli::Supervisor

Inherits:
Object
  • Object
show all
Defined in:
lib/osctld/cli/supervisor.rb

Defined Under Namespace

Classes: Options

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



16
17
18
# File 'lib/osctld/cli/supervisor.rb', line 16

def opts
  @opts
end

Class Method Details

.runObject



7
8
9
10
11
12
13
14
# File 'lib/osctld/cli/supervisor.rb', line 7

def self.run
  s = new
  s.parse

  return Cli::Daemon.run(s.opts) unless s.supervise?

  s.supervise
end

Instance Method Details

#cleanupObject



111
112
113
114
115
116
117
118
119
# File 'lib/osctld/cli/supervisor.rb', line 111

def cleanup
  FileUtils.rm_f(Daemon::SOCKET)

  Dir.glob(File.join(RunState::USER_CONTROL_DIR, '*.sock')).each do |f|
    File.unlink(f)
  end

  FileUtils.rm_f(SendReceive::SOCKET)
end

#parseObject



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
46
47
48
49
50
51
52
53
54
# File 'lib/osctld/cli/supervisor.rb', line 18

def parse
  @opts = Options.new(true, nil, :stdout, 'daemon')

  OptionParser.new do |opts|
    opts.on('--[no-]supervisor', 'Toggle osctld supervisor process (enabled by default)') do |v|
      @opts.supervisor = v
    end

    opts.on('-c', '--config FILE', 'Config file') do |v|
      @opts.config = v
    end

    opts.on('-l', '--log LOGGER', %w[syslog stdout]) do |v|
      @opts.log = v.to_sym
    end

    opts.on(
      '--log-facility FACILITY',
      'Syslog facility, see man syslog(3), lowercase without LOG_ prefix'
    ) do |v|
      @opts.log_facility = v
    end

    opts.on('-h', '--help', 'Prints help message and exit') do
      puts opts
      exit
    end
  end.parse!

  if @opts.config.nil?
    warn 'Provide option --config FILE'
    warn opts
    exit(false)
  end

  @opts
end

#superviseObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/osctld/cli/supervisor.rb', line 60

def supervise
  Process.setproctitle('osctld: supervisor')
  OsCtl::Lib::Logger.setup(opts.log, facility: opts.log_facility)

  out_r, out_w = IO.pipe

  pid = Process.fork do
    out_r.close

    $stdin.close
    $stdout.reopen(out_w)
    $stderr.reopen(out_w)

    Process.exec(
      File.expand_path($0),
      '--no-supervisor',
      '--config', opts.config,
      '--log', opts.log.to_s,
      '--log-facility', opts.log_facility,
      pgroup: true
    )
  end

  out_w.close

  Signal.trap('CHLD') do
    out_r.close
  end

  %w[INT TERM HUP].each do |sig|
    Signal.trap(sig) do
      Process.kill(sig, pid)
    end
  end

  begin
    out_r.each_line do |line|
      OsCtl::Lib::Logger.log(:unknown, line)
    rescue EncodingError
      next
    end
  rescue IOError
    # pass
  ensure
    _, status = Process.wait2(pid)
    cleanup

    exit(status.exitstatus || 0)
  end
end

#supervise?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/osctld/cli/supervisor.rb', line 56

def supervise?
  opts.supervisor
end