Class: Tkellem::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/tkellem/daemon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Daemon

Returns a new instance of Daemon.



11
12
13
14
15
16
# File 'lib/tkellem/daemon.rb', line 11

def initialize(args)
  @args = args
  @options = {
    :path => File.expand_path("~/.tkellem/"),
  }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/tkellem/daemon.rb', line 9

def options
  @options
end

Instance Method Details

#runObject



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
55
56
57
58
59
60
61
# File 'lib/tkellem/daemon.rb', line 18

def run
  op = OptionParser.new do |opts|
    opts.banner = "Usage #{$0} <command> <options>"
    opts.separator %{\nWhere <command> is one of:
start      start the jobs daemon
stop       stop the jobs daemon
run        start and run in the foreground
restart    stop and then start the jobs daemon
status     show daemon status
admin      run admin commands as if connected to the tkellem console
}

    opts.separator "\n<options>"
    opts.on("-p", "--path", "Use alternate folder for tkellem data (default #{options[:path]})") { |p| options[:path] = p }
    opts.on_tail("-h", "--help", "Show this message") { puts opts; exit }
  end
  op.parse!(@args)

  FileUtils.mkdir_p(path)
  File.chmod(0700, path)
  command = @args.shift
  case command
  when 'start'
    abort_if_running
    daemonize
    start
  when 'stop'
    stop
  when 'run'
    start
  when 'status'
    exit(status ? 0 : 1)
  when 'restart'
    stop if status(false)
    daemonize
    start
  when 'admin'
    admin
  when nil
    puts op
  else
    raise("Unknown command: #{command.inspect}")
  end
end