Class: Soba::Services::TmuxSessionManager
- Inherits:
-
Object
- Object
- Soba::Services::TmuxSessionManager
- Defined in:
- lib/soba/services/tmux_session_manager.rb
Instance Method Summary collapse
- #create_issue_window(session_name:, issue_number:) ⇒ Object
- #create_phase_pane(session_name:, window_name:, phase:, vertical: true, max_panes: 3, max_retries: 3) ⇒ Object
- #find_issue_window(repository_name, issue_number) ⇒ Object
- #find_or_create_repository_session ⇒ Object
- #find_repository_session ⇒ Object
-
#find_repository_session_by_pid(repository) ⇒ Object
Find repository session by PID.
-
#initialize(config: nil, tmux_client: nil, lock_manager: nil, test_process_manager: nil) ⇒ TmuxSessionManager
constructor
A new instance of TmuxSessionManager.
- #list_issue_windows(repository_name) ⇒ Object
-
#session_exists?(session_name) ⇒ Boolean
Check if a session exists.
Constructor Details
#initialize(config: nil, tmux_client: nil, lock_manager: nil, test_process_manager: nil) ⇒ TmuxSessionManager
Returns a new instance of TmuxSessionManager.
12 13 14 15 16 17 |
# File 'lib/soba/services/tmux_session_manager.rb', line 12 def initialize(config: nil, tmux_client: nil, lock_manager: nil, test_process_manager: nil) @config = config @tmux_client = tmux_client || Soba::Infrastructure::TmuxClient.new @lock_manager = lock_manager || Soba::Infrastructure::LockManager.new @test_process_manager = test_process_manager || TestProcessManager.new end |
Instance Method Details
#create_issue_window(session_name:, issue_number:) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/soba/services/tmux_session_manager.rb', line 53 def create_issue_window(session_name:, issue_number:) window_name = "issue-#{issue_number}" lock_name = "window-#{session_name}-#{window_name}" @lock_manager.with_lock(lock_name, timeout: 5) do # Double check for existing window to prevent duplicates if @tmux_client.window_exists?(session_name, window_name) { success: true, window_name: window_name, created: false } else if @tmux_client.create_window(session_name, window_name) # Verify creation was successful if @tmux_client.window_exists?(session_name, window_name) { success: true, window_name: window_name, created: true } else { success: false, error: "Window creation verification failed for issue #{issue_number}" } end else { success: false, error: "Failed to create window for issue #{issue_number}" } end end end rescue Soba::Infrastructure::LockTimeoutError => e { success: false, error: "Lock acquisition failed: #{e.}" } end |
#create_phase_pane(session_name:, window_name:, phase:, vertical: true, max_panes: 3, max_retries: 3) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/soba/services/tmux_session_manager.rb', line 78 def create_phase_pane(session_name:, window_name:, phase:, vertical: true, max_panes: 3, max_retries: 3) # 前提条件チェック unless @tmux_client.session_exists?(session_name) return { success: false, error: "Session does not exist: #{session_name}" } end unless @tmux_client.window_exists?(session_name, window_name) return { success: false, error: "Window does not exist: #{window_name}" } end # tmuxサーバーの応答性チェック if @tmux_client.list_sessions.nil? return { success: false, error: 'tmux server is not responding' } end # 現在のペイン一覧を取得 existing_panes = @tmux_client.list_panes(session_name, window_name) # 最大ペイン数を超えている場合、古いペインを削除 if existing_panes.size >= max_panes # start_timeでソート(古い順) sorted_panes = existing_panes.sort_by { |p| p[:start_time] } # 最大ペイン数-1になるまで古いペインを削除 panes_to_remove = sorted_panes.take(existing_panes.size - max_panes + 1) panes_to_remove.each do |pane| @tmux_client.kill_pane(pane[:id]) end end # リトライロジック付きでペインを作成 pane_id = nil error_details = nil retry_count = 0 retry_delays = [0.5, 1, 2] # 指数バックオフ max_retries.times do |attempt| result = @tmux_client.split_window( session_name: session_name, window_name: window_name, vertical: vertical ) # 結果を確認 if result.is_a?(Array) pane_id, error_details = result else pane_id = result end if pane_id # 成功した場合はループを抜ける break else retry_count = attempt + 1 if error_details && retry_count < max_retries Soba.logger.warn( "Pane creation failed (attempt #{retry_count}/#{max_retries}): " \ "#{error_details[:stderr]} (exit status: #{error_details[:exit_status]})" ) sleep(retry_delays[attempt] || 2) end end end if pane_id # レイアウトを調整 @tmux_client.select_layout(session_name, window_name, 'even-horizontal') { success: true, pane_id: pane_id, phase: phase } else = "Failed to create pane for phase #{phase}" if error_details += ": #{error_details[:stderr]}" Soba.logger.error( "Pane creation failed after #{retry_count} retries: " \ "#{error_details[:stderr]} (exit status: #{error_details[:exit_status]})" ) end { success: false, error: } end end |
#find_issue_window(repository_name, issue_number) ⇒ Object
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/soba/services/tmux_session_manager.rb', line 161 def find_issue_window(repository_name, issue_number) session_name = generate_session_name(repository_name) window_name = "issue-#{issue_number}" if @tmux_client.session_exists?(session_name) && @tmux_client.window_exists?(session_name, window_name) "#{session_name}:#{window_name}" else nil end end |
#find_or_create_repository_session ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/soba/services/tmux_session_manager.rb', line 19 def find_or_create_repository_session repository = Configuration.config.github.repository return { success: false, error: 'Repository configuration not found' } if repository.blank? # Convert repository name to session-safe format with PID session_name = generate_session_name(repository) if @tmux_client.session_exists?(session_name) { success: true, session_name: session_name, created: false } else if @tmux_client.create_session(session_name) { success: true, session_name: session_name, created: true } else { success: false, error: 'Failed to create repository session' } end end end |
#find_repository_session ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/soba/services/tmux_session_manager.rb', line 38 def find_repository_session repository = Configuration.config.github.repository return { success: false, error: 'Repository configuration not found' } if repository.blank? # Convert repository name to session-safe format with PID session_name = generate_session_name(repository) if @tmux_client.session_exists?(session_name) { success: true, session_name: session_name, exists: true } else { success: true, session_name: session_name, exists: false } end end |
#find_repository_session_by_pid(repository) ⇒ Object
Find repository session by PID
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/soba/services/tmux_session_manager.rb', line 208 def find_repository_session_by_pid(repository) require_relative 'pid_manager' # Get PID file path for this repository pid_file = File.join(Dir.home, '.soba', 'pids', "#{repository.gsub('/', '-')}.pid") pid_manager = PidManager.new(pid_file) pid = pid_manager.read unless pid return { success: true, session_name: nil, exists: false } end # Generate session name with PID session_name = "soba-#{repository.gsub(/[\/._]/, '-')}-#{pid}" if @tmux_client.session_exists?(session_name) { success: true, session_name: session_name, exists: true } else # Clean up stale PID file pid_manager.delete { success: true, session_name: nil, exists: false } end end |
#list_issue_windows(repository_name) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/soba/services/tmux_session_manager.rb', line 172 def list_issue_windows(repository_name) session_name = generate_session_name(repository_name) return [] unless @tmux_client.session_exists?(session_name) windows = @tmux_client.list_windows(session_name) issue_windows = windows.select { |window| window.start_with?('issue-') } issue_windows.map do |window| issue_number = begin window.match(/issue-(\d+)/)[1] rescue nil end next unless issue_number # Try to fetch issue title from GitHub title = begin fetch_issue_title(repository_name, issue_number) rescue nil end { window: window, title: title, } end.compact end |
#session_exists?(session_name) ⇒ Boolean
Check if a session exists
203 204 205 |
# File 'lib/soba/services/tmux_session_manager.rb', line 203 def session_exists?(session_name) @tmux_client.session_exists?(session_name) end |