Class: Soba::Commands::Stop

Inherits:
Object
  • Object
show all
Defined in:
lib/soba/commands/stop.rb

Instance Method Summary collapse

Instance Method Details

#execute(_global_options = {}, options = {}, _args = []) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/soba/commands/stop.rb', line 10

def execute(_global_options = {}, options = {}, _args = [])
  # Allow test environment to override PID file path
  pid_file = ENV['SOBA_TEST_PID_FILE'] || File.expand_path('~/.soba/soba.pid')
  pid_manager = Soba::Services::PidManager.new(pid_file)

  pid = pid_manager.read

  unless pid
    puts "No daemon process is running"
    return 1
  end

  unless pid_manager.running?
    Soba.logger.info "Daemon was not running (stale PID file cleaned up)"
    pid_manager.delete
    return 0
  end

  puts "Stopping daemon (PID: #{pid})..."

  # Create PID-based stopping file for graceful shutdown
  # This ensures each process has its own stopping file to avoid conflicts
  stopping_file = File.expand_path("~/.soba/stopping.#{pid}")
  FileUtils.mkdir_p(File.dirname(stopping_file))
  FileUtils.touch(stopping_file)

  begin
    # Check if force option is specified
    if options[:force]
      # Force kill immediately
      Soba.logger.warn "Forcefully terminating daemon (PID: #{pid})"
      Process.kill('KILL', pid)
      sleep 1
      cleanup_tmux_sessions
      pid_manager.delete
      FileUtils.rm_f(stopping_file)
      Soba.logger.warn "Daemon forcefully terminated"
      return 0
    end

    # Send SIGTERM for graceful shutdown
    Process.kill('TERM', pid)
    Soba.logger.info "Sent SIGTERM signal, waiting for daemon to terminate"

    # Use custom timeout if specified
    timeout_value = options[:timeout] || 30

    # Wait for process to terminate gracefully
    if wait_for_termination(pid, timeout: timeout_value)
      Soba.logger.info "Daemon stopped successfully"
      cleanup_tmux_sessions
      pid_manager.delete
      FileUtils.rm_f(stopping_file)
      0
    else
      # Force kill if not terminated
      Soba.logger.warn "Daemon did not stop gracefully, forcefully terminating"
      Process.kill('KILL', pid)
      sleep 1
      cleanup_tmux_sessions
      pid_manager.delete
      FileUtils.rm_f(stopping_file)
      Soba.logger.warn "Daemon forcefully terminated"
      0
    end
  rescue Errno::ESRCH
    # Process doesn't exist
    Soba.logger.info "Process not found (already terminated)"
    pid_manager.delete
    FileUtils.rm_f(stopping_file)
    0
  rescue Errno::EPERM
    # Permission denied
    puts "Permission denied: unable to stop daemon (PID: #{pid})"
    puts "You may need to run this command with appropriate permissions"
    FileUtils.rm_f(stopping_file)
    1
  rescue StandardError => e
    Soba.logger.error "Error stopping daemon: #{e.message}"
    FileUtils.rm_f(stopping_file)
    1
  end
end