48 lines
1.4 KiB
Go
48 lines
1.4 KiB
Go
package service
|
|
|
|
import (
|
|
"time"
|
|
|
|
"browser.local/platform/domain"
|
|
"browser.local/platform/validator"
|
|
)
|
|
|
|
func (svc *CoreService) projectLifecycleJobResult(job domain.Job, stamp time.Time) error {
|
|
nextState, ok := lifecycleProjectedState(job.Capability, job.State)
|
|
if !ok || job.ServerInstanceID == "" {
|
|
return nil
|
|
}
|
|
instance, err := svc.store.ServerInstances().Get(job.ServerInstanceID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
instance.State = nextState
|
|
instance.UpdatedAt = stamp
|
|
if err := validator.ValidateServerInstance(instance); err != nil {
|
|
return err
|
|
}
|
|
return svc.store.ServerInstances().Update(instance)
|
|
}
|
|
|
|
func lifecycleProjectedState(capability string, jobState domain.JobState) (domain.ServerInstanceState, bool) {
|
|
if capability != domain.LifecycleCapabilityInstall && capability != domain.LifecycleCapabilityStart && capability != domain.LifecycleCapabilityStop {
|
|
return "", false
|
|
}
|
|
if jobState == domain.JobStateFailed || jobState == domain.JobStateCancelled {
|
|
return domain.ServerInstanceStateFailed, true
|
|
}
|
|
if jobState != domain.JobStateSucceeded {
|
|
return "", false
|
|
}
|
|
switch capability {
|
|
case domain.LifecycleCapabilityInstall:
|
|
return domain.ServerInstanceStateReady, true
|
|
case domain.LifecycleCapabilityStart:
|
|
return domain.ServerInstanceStateRunning, true
|
|
case domain.LifecycleCapabilityStop:
|
|
return domain.ServerInstanceStateStopped, true
|
|
default:
|
|
return "", false
|
|
}
|
|
}
|