Sha256: 88c4bfb7b33cc54a9ba8b1577e011b219eaa46f3504805d62fa62a98bf961632

Contents?: true

Size: 1.36 KB

Versions: 1

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

1 entries across 1 versions & 1 rubygems

Version Path
lnd-tool-0.1.0 lib/lnd/tool/daemon.rb