Module: LND::Tool::Daemon

Defined in:
lib/lnd/tool/daemon.rb

Overview

Daemon module for cli

Class Method Summary collapse

Class Method Details

.base_dirPathname

Get base directory path.

Returns:

  • (Pathname)

    directory path.



14
15
16
# File 'lib/lnd/tool/daemon.rb', line 14

def base_dir
  Pathname.new(File.expand_path("#{Dir.home}/.lnd-tool"))
end

.db_pathPathname

Get database file path.

Returns:

  • (Pathname)

    database file path.



20
21
22
# File 'lib/lnd/tool/daemon.rb', line 20

def db_path
  base_dir.join('storage.db')
end

.pid_file?Boolean

Check whether pid file exist?

Returns:

  • (Boolean)


32
33
34
# File 'lib/lnd/tool/daemon.rb', line 32

def pid_file?
  pid_path.file?
end

.pid_pathPathname

Get pid file path.

Returns:

  • (Pathname)

    pid file path.



26
27
28
# File 'lib/lnd/tool/daemon.rb', line 26

def pid_path
  base_dir.join('pid')
end

.running?Boolean

Check whether daemon running?

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/lnd/tool/daemon.rb', line 38

def running?
  base_dir.exist? && pid_file? && Process.kill(0, pid_path.read.to_i) == 1
rescue Errno::ESRCH
  false
end

.startObject

Start block program as daemon.



50
51
52
53
54
55
56
57
58
59
# File 'lib/lnd/tool/daemon.rb', line 50

def start
  base_dir.mkdir unless base_dir.exist?
  raise LND::Tool::Error, "process(#{pid_path.read.to_i}) already running." if running?

  Process.daemon(true)
  pid_path.write(Process.pid.to_s)
  yield
ensure
  pid_path.delete if pid_file?
end

.stopObject

Stop daemon.



45
46
47
# File 'lib/lnd/tool/daemon.rb', line 45

def stop
  Process.kill('KILL', pid_path.read.to_i) if running?
end