Refactor instance status management: replace Running boolean with InstanceStatus enum and update related methods

This commit is contained in:
2025-08-27 19:44:38 +02:00
parent 615c2ac54e
commit 1443746add
10 changed files with 111 additions and 45 deletions

View File

@@ -150,7 +150,7 @@ func (im *instanceManager) Shutdown() {
wg.Add(len(im.instances))
for name, inst := range im.instances {
if !inst.Running {
if !inst.IsRunning() {
wg.Done() // If instance is not running, just mark it as done
continue
}
@@ -234,7 +234,7 @@ func (im *instanceManager) loadInstance(name, path string) error {
// Restore persisted fields that NewInstance doesn't set
inst.Created = persistedInstance.Created
inst.Running = persistedInstance.Running
inst.SetStatus(persistedInstance.Status)
// Check for port conflicts and add to maps
if inst.GetOptions() != nil && inst.GetOptions().Port > 0 {
@@ -254,7 +254,7 @@ func (im *instanceManager) autoStartInstances() {
im.mu.RLock()
var instancesToStart []*instance.Process
for _, inst := range im.instances {
if inst.Running && // Was running when persisted
if inst.IsRunning() && // Was running when persisted
inst.GetOptions() != nil &&
inst.GetOptions().AutoRestart != nil &&
*inst.GetOptions().AutoRestart {
@@ -266,7 +266,7 @@ func (im *instanceManager) autoStartInstances() {
for _, inst := range instancesToStart {
log.Printf("Auto-starting instance %s", inst.Name)
// Reset running state before starting (since Start() expects stopped instance)
inst.Running = false
inst.SetStatus(instance.Stopped)
if err := inst.Start(); err != nil {
log.Printf("Failed to auto-start instance %s: %v", inst.Name, err)
}

View File

@@ -59,7 +59,7 @@ func TestCreateInstance_Success(t *testing.T) {
if inst.Name != "test-instance" {
t.Errorf("Expected instance name 'test-instance', got %q", inst.Name)
}
if inst.Running {
if inst.GetStatus() != instance.Stopped {
t.Error("New instance should not be running")
}
if inst.GetOptions().Port != 8080 {
@@ -357,7 +357,7 @@ func TestTimeoutFunctionality(t *testing.T) {
inst.SetTimeProvider(mockTime)
// Set instance to running state so timeout logic can work
inst.Running = true
inst.SetStatus(instance.Running)
// Simulate instance being "running" for timeout check (without actual process)
// We'll test the ShouldTimeout logic directly
@@ -377,7 +377,7 @@ func TestTimeoutFunctionality(t *testing.T) {
}
// Reset running state to avoid shutdown issues
inst.Running = false
inst.SetStatus(instance.Stopped)
// Test that instance without timeout doesn't timeout
noTimeoutOptions := &instance.CreateInstanceOptions{
@@ -393,7 +393,7 @@ func TestTimeoutFunctionality(t *testing.T) {
}
noTimeoutInst.SetTimeProvider(mockTime)
noTimeoutInst.Running = true // Set to running for timeout check
noTimeoutInst.SetStatus(instance.Running) // Set to running for timeout check
noTimeoutInst.UpdateLastRequestTime()
// Even with time advanced, should not timeout
@@ -402,7 +402,7 @@ func TestTimeoutFunctionality(t *testing.T) {
}
// Reset running state to avoid shutdown issues
noTimeoutInst.Running = false
noTimeoutInst.SetStatus(instance.Stopped)
}
func TestConcurrentAccess(t *testing.T) {

View File

@@ -109,7 +109,7 @@ func (im *instanceManager) UpdateInstance(name string, options *instance.CreateI
}
// Check if instance is running before updating options
wasRunning := instance.Running
wasRunning := instance.IsRunning()
// If the instance is running, stop it first
if wasRunning {
@@ -147,7 +147,7 @@ func (im *instanceManager) DeleteInstance(name string) error {
return fmt.Errorf("instance with name %s not found", name)
}
if instance.Running {
if instance.IsRunning() {
return fmt.Errorf("instance with name %s is still running, stop it before deleting", name)
}
@@ -173,7 +173,7 @@ func (im *instanceManager) StartInstance(name string) (*instance.Process, error)
if !exists {
return nil, fmt.Errorf("instance with name %s not found", name)
}
if instance.Running {
if instance.IsRunning() {
return instance, fmt.Errorf("instance with name %s is already running", name)
}
@@ -200,7 +200,7 @@ func (im *instanceManager) StopInstance(name string) (*instance.Process, error)
if !exists {
return nil, fmt.Errorf("instance with name %s not found", name)
}
if !instance.Running {
if !instance.IsRunning() {
return instance, fmt.Errorf("instance with name %s is already stopped", name)
}