2020--- @param timeout number
2121--- @return Promise<string | nil>
2222local function try_custom_server (base_url , timeout )
23- local promise = Promise .new ()
2423 local health_url = base_url .. ' /global/health'
2524
2625 log .debug (' try_custom_server: checking health at %s' , health_url )
2726
28- curl .request ({
29- url = health_url ,
30- method = ' GET' ,
31- timeout = timeout * 1000 ,
32- proxy = ' ' , -- Disable proxy for health check
33- callback = function (response )
34- if response and response .status >= 200 and response .status < 300 then
35- local success , health_data = pcall (vim .json .decode , response .body )
36- if success and health_data then
37- log .debug (' try_custom_server: health check passed' )
38- promise :resolve (base_url )
39- return
40- end
41- end
42-
43- local err_msg =
44- string.format (' Health check failed at %s (status: %d)' , health_url , response and response .status or 0 )
45- log .debug (' try_custom_server: %s' , err_msg )
46- promise :reject (err_msg )
47- end ,
48- on_error = function (err )
49- log .debug (' try_custom_server: error connecting to %s: %s' , health_url , vim .inspect (err ))
50- promise :reject (err )
51- end ,
52- })
27+ return opencode_server .health_check (health_url , timeout * 1000 ):and_then (function (healthy )
28+ if healthy then
29+ log .debug (' try_custom_server: health check passed' )
30+ return base_url
31+ end
5332
54- return promise
33+ local err_msg = string.format (' Health check failed at %s' , health_url )
34+ log .debug (' try_custom_server: %s' , err_msg )
35+ return Promise .new ():reject (err_msg )
36+ end )
5537end
5638
5739--- @param response { status : integer , body : string }
@@ -185,15 +167,9 @@ local function resolve_port()
185167 return existing or math.random (1024 , 65535 )
186168end
187169
188- --- Ensure the opencode server is running, starting it if necessary.
189- --- @return Promise<OpencodeServer>
190- function M .ensure_server ()
170+ local function _start_server ()
191171 local promise = Promise .new ()
192172
193- if state .opencode_server and state .opencode_server :is_running () then
194- return promise :resolve (state .opencode_server )
195- end
196-
197173 local custom_url = config .server .url
198174 if not custom_url then
199175 log .debug (' ensure_server: server.url not configured, spawning local server' )
@@ -218,20 +194,36 @@ function M.ensure_server()
218194 return promise
219195end
220196
221- local function retry_connect ( base_url , timeout , max_retries , on_success , on_failure )
222- local function attempt ( retry_count )
223- vim . defer_fn ( function ()
224- try_custom_server ( base_url , timeout ): and_then ( on_success ): catch ( function ( err )
225- if retry_count < max_retries then
226- attempt ( retry_count + 1 )
227- else
228- log . error ( ' try_connect_to_custom_server: exhausted %d retries: %s ' , max_retries , vim . inspect ( err ))
229- on_failure ( err )
230- end
231- end )
232- end , retry_count * ( config . server . retry_delay or 2000 ) )
197+ --- Ensure the opencode server is running, starting it if necessary.
198+ --- @return Promise<OpencodeServer>
199+ function M . ensure_server ()
200+ if state . opencode_server and state . opencode_server : is_running () then
201+ return state . opencode_server : check_health (): and_then ( function ( healthy )
202+ if healthy then
203+ return state . opencode_server
204+ end
205+ log . warn ( ' ensure_server: cached server unhealthy, reconnecting ' )
206+ state . jobs . clear_server ()
207+ return _start_server ( )
208+ end )
233209 end
234- attempt (1 )
210+
211+ return _start_server ()
212+ end
213+
214+ local function retry_connect (base_url , timeout , max_retries , on_success , on_failure )
215+ local delay = config .server .retry_delay or 2000
216+ Promise .delay (delay )
217+ :and_then (function ()
218+ return Promise .retry (function ()
219+ return try_custom_server (base_url , timeout )
220+ end , max_retries , delay )
221+ end )
222+ :and_then (on_success )
223+ :catch (function (err )
224+ log .error (' try_connect_to_custom_server: exhausted %d retries: %s' , max_retries , vim .inspect (err ))
225+ on_failure (err )
226+ end )
235227end
236228
237229local function spawn_and_retry (base_url , custom_port , custom_url , promise , timeout )
0 commit comments