Fix List of the Environments with fetch.
Before the List function dropped all idleRunners of all environments when fetch was set. Additionally, the replaced environment was not destroyed properly so that a goroutine for it and for all its idle runners remained running.
This commit is contained in:

committed by
Sebastian Serth

parent
809ca0321d
commit
b2898f9183
@ -87,14 +87,50 @@ func (m *NomadEnvironmentManager) Get(id dto.EnvironmentID, fetch bool) (
|
|||||||
|
|
||||||
func (m *NomadEnvironmentManager) List(fetch bool) ([]runner.ExecutionEnvironment, error) {
|
func (m *NomadEnvironmentManager) List(fetch bool) ([]runner.ExecutionEnvironment, error) {
|
||||||
if fetch {
|
if fetch {
|
||||||
err := m.Load()
|
if err := m.fetchEnvironments(); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return m.runnerManager.ListEnvironments(), nil
|
return m.runnerManager.ListEnvironments(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *NomadEnvironmentManager) fetchEnvironments() error {
|
||||||
|
remoteEnvironmentList, err := m.api.LoadEnvironmentJobs()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed fetching environments: %w", err)
|
||||||
|
}
|
||||||
|
remoteEnvironments := make(map[string]*nomadApi.Job)
|
||||||
|
|
||||||
|
// Update local environments from remote environments.
|
||||||
|
for _, job := range remoteEnvironmentList {
|
||||||
|
remoteEnvironments[*job.ID] = job
|
||||||
|
id, err := nomad.EnvironmentIDFromTemplateJobID(*job.ID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("cannot parse environment id: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if localEnvironment, ok := m.runnerManager.GetEnvironment(id); ok {
|
||||||
|
fetchedEnvironment := newNomadEnvironmentFromJob(job, m.api)
|
||||||
|
localEnvironment.SetConfigFrom(fetchedEnvironment)
|
||||||
|
// We destroy only this (second) local reference to the environment.
|
||||||
|
if err = fetchedEnvironment.Delete(true); err != nil {
|
||||||
|
log.WithError(err).Warn("Failed to remove environment locally")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
m.runnerManager.StoreEnvironment(newNomadEnvironmentFromJob(job, m.api))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove local environments that are not remote environments.
|
||||||
|
for _, localEnvironment := range m.runnerManager.ListEnvironments() {
|
||||||
|
if _, ok := remoteEnvironments[localEnvironment.ID().ToString()]; !ok {
|
||||||
|
err := localEnvironment.Delete(true)
|
||||||
|
log.WithError(err).Warn("Failed to remove environment locally")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m *NomadEnvironmentManager) CreateOrUpdate(
|
func (m *NomadEnvironmentManager) CreateOrUpdate(
|
||||||
id dto.EnvironmentID, request dto.ExecutionEnvironmentRequest, ctx context.Context) (created bool, err error) {
|
id dto.EnvironmentID, request dto.ExecutionEnvironmentRequest, ctx context.Context) (created bool, err error) {
|
||||||
// Check if execution environment is already existing (in the local memory).
|
// Check if execution environment is already existing (in the local memory).
|
||||||
|
Reference in New Issue
Block a user