Skip to content

Commit f3e73d3

Browse files
committed
feat: config hot-reload - fix merge conflicts
1 parent e5af39a commit f3e73d3

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

proxy/process.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ func isValidTransition(from, to ProcessState) bool {
119119
return to == StateStopping
120120
case StateStopping:
121121
return to == StateStopped || to == StateShutdown
122-
case StateFailed, StateShutdown:
123-
return false // No transitions allowed from these states
122+
case StateFailed:
123+
return to == StateStopped // Allow resetting a failed process
124+
case StateShutdown:
125+
return false // No transitions from shutdown
124126
}
125127
return false
126128
}
@@ -412,7 +414,20 @@ func (p *Process) ProxyRequest(w http.ResponseWriter, r *http.Request) {
412414

413415
// prevent new requests from being made while stopping or irrecoverable
414416
currentState := p.CurrentState()
415-
if currentState == StateFailed || currentState == StateShutdown || currentState == StateStopping {
417+
418+
if currentState == StateFailed {
419+
p.proxyLogger.Infof("<%s> Attempting to recover from failed state.", p.ID)
420+
// Attempt to reset the state to stopped so it can be restarted
421+
if _, err := p.swapState(StateFailed, StateStopped); err != nil {
422+
p.proxyLogger.Errorf("<%s> Failed to reset state from Failed to Stopped: %v. Current state: %s", p.ID, err, p.CurrentState())
423+
http.Error(w, fmt.Sprintf("Process failed and could not be reset, state is %s", p.CurrentState()), http.StatusServiceUnavailable)
424+
return
425+
}
426+
p.proxyLogger.Infof("<%s> Process state reset to Stopped from Failed.", p.ID)
427+
currentState = p.CurrentState() // Re-fetch current state, should now be StateStopped
428+
}
429+
430+
if currentState == StateShutdown || currentState == StateStopping {
416431
http.Error(w, fmt.Sprintf("Process can not ProxyRequest, state is %s", currentState), http.StatusServiceUnavailable)
417432
return
418433
}

proxy/process_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,16 @@ func TestProcess_BrokenModelConfig(t *testing.T) {
109109
req := httptest.NewRequest("GET", "/", nil)
110110
w := httptest.NewRecorder()
111111
process.ProxyRequest(w, req)
112-
assert.Equal(t, http.StatusBadGateway, w.Code)
113-
assert.Contains(t, w.Body.String(), "unable to start process")
112+
assert.Equal(t, http.StatusBadGateway, w.Code) // First attempt fails to start
113+
assert.Contains(t, w.Body.String(), "unable to start process: start() failed: exec: \"nonexistent-command\": executable file not found in $PATH")
114+
assert.Equal(t, StateFailed, process.CurrentState()) // Should be in failed state after first attempt
114115

116+
// Second request should also attempt to start and fail similarly due to recovery logic
115117
w = httptest.NewRecorder()
116118
process.ProxyRequest(w, req)
117-
assert.Equal(t, http.StatusServiceUnavailable, w.Code)
118-
assert.Contains(t, w.Body.String(), "Process can not ProxyRequest, state is failed")
119+
assert.Equal(t, http.StatusBadGateway, w.Code) // Second attempt also fails to start
120+
assert.Contains(t, w.Body.String(), "unable to start process: start() failed: exec: \"nonexistent-command\": executable file not found in $PATH")
121+
assert.Equal(t, StateFailed, process.CurrentState()) // Should end up in failed state again
119122
}
120123

121124
func TestProcess_UnloadAfterTTL(t *testing.T) {
@@ -266,7 +269,7 @@ func TestProcess_SwapState(t *testing.T) {
266269
{"Ready to Starting", StateReady, StateReady, StateStarting, ErrInvalidStateTransition, StateReady},
267270
{"Ready to Failed", StateReady, StateReady, StateFailed, ErrInvalidStateTransition, StateReady},
268271
{"Stopping to Ready", StateStopping, StateStopping, StateReady, ErrInvalidStateTransition, StateStopping},
269-
{"Failed to Stopped", StateFailed, StateFailed, StateStopped, ErrInvalidStateTransition, StateFailed},
272+
{"Failed to Stopped", StateFailed, StateFailed, StateStopped, nil, StateStopped},
270273
{"Failed to Starting", StateFailed, StateFailed, StateStarting, ErrInvalidStateTransition, StateFailed},
271274
{"Shutdown to Stopped", StateShutdown, StateShutdown, StateStopped, ErrInvalidStateTransition, StateShutdown},
272275
{"Shutdown to Starting", StateShutdown, StateShutdown, StateStarting, ErrInvalidStateTransition, StateShutdown},

0 commit comments

Comments
 (0)