Class: ParticlePi::Daemon

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Daemon

Returns a new instance of Daemon.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/particlepi/daemon.rb', line 13

def initialize(options = {})
  @options = options
  expand_directories
  @quit = false

  # Allow swapping out the exit behavior for testing
  @exit_with_error = lambda do |message|
    $stderr.puts message
    exit 1
  end
end

Instance Attribute Details

#exit_with_errorObject

Returns the value of attribute exit_with_error.



11
12
13
# File 'lib/particlepi/daemon.rb', line 11

def exit_with_error
  @exit_with_error
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/particlepi/daemon.rb', line 10

def options
  @options
end

Instance Method Details

#daemonize?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/particlepi/daemon.rb', line 50

def daemonize?
  options[:daemonize]
end

#logfileObject



54
55
56
# File 'lib/particlepi/daemon.rb', line 54

def logfile
  options[:logfile]
end

#pidfileObject



58
59
60
# File 'lib/particlepi/daemon.rb', line 58

def pidfile
  options[:pidfile]
end

#quit!Object



42
43
44
# File 'lib/particlepi/daemon.rb', line 42

def quit!
  @quit = true
end

#quit?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/particlepi/daemon.rb', line 46

def quit?
  @quit
end

#run! {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/particlepi/daemon.rb', line 25

def run!
  check_pid
  daemonize if daemonize?
  write_pid
  trap_signals

  if logfile
    redirect_output
  elsif daemonize?
    suppress_output
  end

  # Start the worker, passing the daemon install to be able to exit
  # gracefully by checking daemon.quit?
  yield(self) if block_given?
end