Module: RubyProgress::Daemon

Defined in:
lib/ruby-progress/daemon.rb

Overview

Daemon helpers for backgrounding progress indicators. Provides minimal daemonization, PID file management, and simple control-message signaling.

Class Method Summary collapse

Class Method Details

.control_message_file(pid_file) ⇒ Object



16
17
18
# File 'lib/ruby-progress/daemon.rb', line 16

def control_message_file(pid_file)
  "#{pid_file}.msg"
end

.default_pid_fileObject



12
13
14
# File 'lib/ruby-progress/daemon.rb', line 12

def default_pid_file
  '/tmp/ruby-progress/progress.pid'
end

.show_status(pid_file) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby-progress/daemon.rb', line 20

def show_status(pid_file)
  if File.exist?(pid_file)
    pid = File.read(pid_file).strip
    running = system("ps -p #{pid} > /dev/null")
    puts(running ? "Daemon running (pid #{pid})" : 'PID file present but process not running')
    exit(running ? 0 : 1)
  else
    puts 'Daemon not running'
    exit 1
  end
end

.stop_daemon_by_pid_file(pid_file, message: nil, checkmark: false, error: false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby-progress/daemon.rb', line 32

def stop_daemon_by_pid_file(pid_file, message: nil, checkmark: false, error: false)
  unless File.exist?(pid_file)
    puts "PID file #{pid_file} not found"
    exit 1
  end

  pid = File.read(pid_file).strip.to_i

  # Write control message file if provided
  if message || error
    cmf = control_message_file(pid_file)
    payload = { checkmark: checkmark, success: !error }
    payload[:message] = message if message
    File.write(cmf, payload.to_json)
  end

  begin
    Process.kill('USR1', pid)
    sleep 0.5
    FileUtils.rm_f(pid_file)
  rescue Errno::ESRCH
    puts "Process #{pid} not found (may have already stopped)"
    FileUtils.rm_f(pid_file)
    exit 1
  rescue Errno::EPERM
    puts "Permission denied sending signal to process #{pid}"
    exit 1
  end
end