Sha256: fe226c813036126b477bc1edf6dee0bd4a638fce1a0d543f99565ec8539d54bb

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

require 'pathname'

module LND
  module Tool
    # Daemon module for cli
    module Daemon

      module_function

      # Get base directory path.
      # @return [Pathname] directory path.
      def base_dir
        Pathname.new(File.expand_path("#{Dir.home}/.lnd-tool"))
      end

      # Get database file path.
      # @return [Pathname] database file path.
      def db_path
        base_dir.join('storage.db')
      end

      # Get pid file path.
      # @return [Pathname] pid file path.
      def pid_path
        base_dir.join('pid')
      end

      # Check whether pid file exist?
      # @return [Boolean]
      def pid_file?
        pid_path.file?
      end

      # Check whether daemon running?
      # @return [Boolean]
      def running?
        base_dir.exist? && pid_file? && Process.kill(0, pid_path.read.to_i) == 1
      rescue Errno::ESRCH
        false
      end

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

      # Start block program as daemon.
      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

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lnd-tool-0.3.0 lib/lnd/tool/daemon.rb
lnd-tool-0.2.0 lib/lnd/tool/daemon.rb