Class: Soba::Services::TestProcessManager

Inherits:
Object
  • Object
show all
Defined in:
lib/soba/services/test_process_manager.rb

Constant Summary collapse

TEST_PID_DIR =
'/tmp/soba-test-pids'

Instance Method Summary collapse

Instance Method Details

#cleanup_test_processes(test_id, timeout: 10) ⇒ Object



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
# File 'lib/soba/services/test_process_manager.rb', line 34

def cleanup_test_processes(test_id, timeout: 10)
  pid_manager = create_test_pid_manager(test_id)
  cleaned_processes = []

  pid = pid_manager.read
  return { success: true, cleaned_processes: cleaned_processes } unless pid

  if pid_manager.running?
    begin
      # Graceful termination
      Process.kill('TERM', pid)

      # Wait for graceful shutdown
      wait_time = 0
      while wait_time < timeout && pid_manager.running?
        sleep(0.1)
        wait_time += 0.1
      end

      # Force kill if still running
      if pid_manager.running?
        Process.kill('KILL', pid)
        sleep(0.1) # Brief wait for force kill
      end

      cleaned_processes << pid
    rescue Errno::ESRCH, Errno::EPERM
      # Process already dead or no permission
    end
  end

  # Clean up PID file
  pid_manager.delete

  { success: true, cleaned_processes: cleaned_processes }
rescue StandardError => e
  { success: false, error: e.message, cleaned_processes: cleaned_processes }
end

#create_test_pid_manager(test_id) ⇒ Object



29
30
31
32
# File 'lib/soba/services/test_process_manager.rb', line 29

def create_test_pid_manager(test_id)
  pid_file = test_pid_file_path(test_id)
  PidManager.new(pid_file)
end

#ensure_test_environmentObject



73
74
75
76
77
78
79
80
81
# File 'lib/soba/services/test_process_manager.rb', line 73

def ensure_test_environment
  if test_mode?
    FileUtils.mkdir_p(TEST_PID_DIR)
  end

  { success: true, test_mode: test_mode? }
rescue StandardError => e
  { success: false, error: e.message, test_mode: test_mode? }
end

#generate_test_idObject



21
22
23
# File 'lib/soba/services/test_process_manager.rb', line 21

def generate_test_id
  "#{Process.pid}-#{SecureRandom.hex(4)}"
end

#generate_test_session_name(repository) ⇒ Object



16
17
18
19
# File 'lib/soba/services/test_process_manager.rb', line 16

def generate_test_session_name(repository)
  sanitized_repo = repository.gsub(/[\/._]/, '-')
  "soba-test-#{sanitized_repo}-#{generate_test_id}"
end

#test_mode?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/soba/services/test_process_manager.rb', line 12

def test_mode?
  ENV['SOBA_TEST_MODE'] == 'true'
end

#test_pid_file_path(test_id) ⇒ Object



25
26
27
# File 'lib/soba/services/test_process_manager.rb', line 25

def test_pid_file_path(test_id)
  "#{TEST_PID_DIR}/#{test_id}.pid"
end