@@ -11,10 +11,12 @@ use crate::{
11
11
12
12
pub const SSH_CONNECTION_TIMEOUT_SECS : u64 = 30 ;
13
13
pub const SSH_MESSAGE_TIMEOUT_SECS : u64 = 10 ;
14
- pub const SSH_MAX_CONNECT_ATTEMPTS : usize = 3 ;
15
14
pub const SSH_CONNECT_DELAY_SECS : u64 = 5 ;
16
15
pub const SSH_MAX_EMPTY_MESSAGES : usize = 100 ;
17
16
17
+ pub const SSH_MAX_CONNECT_ATTEMPTS : u32 = 3 ;
18
+ pub const SSH_MAX_CONNECT_ATTEMPTS_PERSISTENT : u32 = 20 ;
19
+
18
20
mod common;
19
21
mod platform;
20
22
@@ -65,7 +67,7 @@ pub async fn command(args: Args) -> Result<()> {
65
67
let running_command = !args. command . is_empty ( ) ;
66
68
67
69
let mut spinner = create_spinner ( "Connecting to service..." . to_string ( ) ) ;
68
- let mut terminal_client = create_client ( & params, & mut spinner) . await ?;
70
+ let mut terminal_client = create_client ( & params, & mut spinner, None ) . await ?;
69
71
70
72
if running_command {
71
73
// Run single command
@@ -93,7 +95,13 @@ async fn run_persistent_session(params: &terminal::SSHConnectParams) -> Result<(
93
95
loop {
94
96
let mut spinner = create_spinner ( "Connecting to service..." . to_string ( ) ) ;
95
97
96
- let mut terminal_client = match create_client ( params, & mut spinner) . await {
98
+ let mut terminal_client = match create_client (
99
+ params,
100
+ & mut spinner,
101
+ Some ( SSH_MAX_CONNECT_ATTEMPTS_PERSISTENT ) ,
102
+ )
103
+ . await
104
+ {
97
105
Ok ( tc) => tc,
98
106
Err ( e) => {
99
107
fail_spinner ( & mut spinner, format ! ( "{}" , e) ) ;
@@ -103,14 +111,9 @@ async fn run_persistent_session(params: &terminal::SSHConnectParams) -> Result<(
103
111
104
112
// Start tmux session
105
113
initialize_shell ( & mut terminal_client, Some ( "bash" . to_string ( ) ) , & mut spinner) . await ?;
106
- terminal_client
107
- . send_data ( "exec tmux new-session -A -s railway\n " )
108
- . await ?;
109
114
110
- // Set tmux mouse mode on to enable proper scrolling
111
- tokio:: time:: sleep ( tokio:: time:: Duration :: from_millis ( 50 ) ) . await ;
112
115
terminal_client
113
- . send_data ( "tmux set -g mouse on; clear \n " )
116
+ . send_data ( "exec tmux new-session -A -s railway \\ ; set -g mouse on \n " )
114
117
. await ?;
115
118
116
119
// Resend the window size after starting a tmux session
@@ -132,9 +135,11 @@ async fn run_persistent_session(params: &terminal::SSHConnectParams) -> Result<(
132
135
println ! ( "Connection reset. Reconnecting..." ) ;
133
136
continue ;
134
137
}
135
- term => {
136
- eprintln ! ( "{}" , term. message( ) ) ;
137
- std:: process:: exit ( term. exit_code ( ) ) ;
138
+ SessionTermination :: SendError ( e)
139
+ | SessionTermination :: StdinError ( e)
140
+ | SessionTermination :: ServerError ( e) => {
141
+ println ! ( "Session error: {}. Reconnecting..." , e) ;
142
+ continue ;
138
143
}
139
144
} ;
140
145
}
@@ -149,7 +154,7 @@ async fn ensure_tmux_is_installed(params: &terminal::SSHConnectParams) -> Result
149
154
let command = "which tmux || (apt-get update && apt-get install -y tmux)" ;
150
155
151
156
let mut spinner = create_spinner ( "Installing tmux..." . to_string ( ) ) ;
152
- let mut terminal_client = create_client ( params, & mut spinner) . await ?;
157
+ let mut terminal_client = create_client ( params, & mut spinner, None ) . await ?;
153
158
154
159
let result =
155
160
execute_command_with_result ( & mut terminal_client, command. to_string ( ) , & mut spinner) . await ;
@@ -165,14 +170,16 @@ async fn ensure_tmux_is_installed(params: &terminal::SSHConnectParams) -> Result
165
170
async fn create_client (
166
171
params : & terminal:: SSHConnectParams ,
167
172
spinner : & mut ProgressBar ,
173
+ max_attempts : Option < u32 > ,
168
174
) -> Result < TerminalClient > {
169
175
let configs = Configs :: new ( ) ?;
170
176
let token = configs
171
177
. get_railway_auth_token ( )
172
178
. context ( "No authentication token found. Please login first with 'railway login'" ) ?;
173
179
174
180
let ws_url = format ! ( "wss://{}" , configs. get_relay_host_path( ) ) ;
175
- let terminal_client = create_terminal_client ( & ws_url, & token, params, spinner) . await ?;
181
+ let terminal_client =
182
+ create_terminal_client ( & ws_url, & token, params, spinner, max_attempts) . await ?;
176
183
177
184
Ok ( terminal_client)
178
185
}
0 commit comments