Class: TinyDaemon::ProcessFile

Inherits:
Struct
  • Object
show all
Defined in:
lib/tiny_daemon/process_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#app_nameObject

Returns the value of attribute app_name

Returns:

  • (Object)

    the current value of app_name



2
3
4
# File 'lib/tiny_daemon/process_file.rb', line 2

def app_name
  @app_name
end

#pid_dirObject

Returns the value of attribute pid_dir

Returns:

  • (Object)

    the current value of pid_dir



2
3
4
# File 'lib/tiny_daemon/process_file.rb', line 2

def pid_dir
  @pid_dir
end

Instance Method Details

#cleanObject



26
27
28
# File 'lib/tiny_daemon/process_file.rb', line 26

def clean
  FileUtils.rm(pid_file_path) if File.exists?(pid_file_path)
end

#getObject



16
17
18
19
20
21
22
23
24
# File 'lib/tiny_daemon/process_file.rb', line 16

def get
  pid = if File.exists?(pid_file_path)
    IO.read(pid_file_path).strip.to_i
  else
    0
  end

  pid == 0 ? nil : pid
end

#pid_file_pathObject



3
4
5
# File 'lib/tiny_daemon/process_file.rb', line 3

def pid_file_path
  File.join(self.pid_dir, "#{self.app_name}.pid")
end

#store(pid) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/tiny_daemon/process_file.rb', line 7

def store(pid)
  raise "#{pid_file_path} already exists. The process might already run. Remove it manually if it doesn't." if File.exists?(pid_file_path)

  FileUtils.mkdir_p(self.pid_dir)
  File.open(pid_file_path, 'w') do |f| 
    f.write("#{pid}")
  end
end