Only load available runners
This commit is contained in:
@@ -66,7 +66,7 @@ func (environment *NomadExecutionEnvironment) NextRunner() (r runner.Runner, err
|
|||||||
// Refresh Big ToDo: Improve this function!! State out that it also rescales the job; Provide context to be terminable...
|
// Refresh Big ToDo: Improve this function!! State out that it also rescales the job; Provide context to be terminable...
|
||||||
func (environment *NomadExecutionEnvironment) Refresh() {
|
func (environment *NomadExecutionEnvironment) Refresh() {
|
||||||
for {
|
for {
|
||||||
runners, err := environment.nomadApiClient.LoadRunners(environment.jobId)
|
runners, err := environment.nomadApiClient.LoadAvailableRunners(environment.jobId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Printf("Failed fetching runners")
|
log.WithError(err).Printf("Failed fetching runners")
|
||||||
break
|
break
|
||||||
|
@@ -66,7 +66,7 @@ func TestRefreshFetchRunners(t *testing.T) {
|
|||||||
// ToDo: Terminate Refresh when test finished (also in other tests)
|
// ToDo: Terminate Refresh when test finished (also in other tests)
|
||||||
go environment.Refresh()
|
go environment.Refresh()
|
||||||
_, _ = environment.NextRunner()
|
_, _ = environment.NextRunner()
|
||||||
apiMock.AssertCalled(t, "LoadRunners", jobId)
|
apiMock.AssertCalled(t, "LoadAvailableRunners", jobId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRefreshFetchesRunnersIntoChannel(t *testing.T) {
|
func TestRefreshFetchesRunnersIntoChannel(t *testing.T) {
|
||||||
@@ -96,7 +96,7 @@ func TestRefreshAddsRunnerToPool(t *testing.T) {
|
|||||||
|
|
||||||
func newRefreshMock(returnedRunnerIds []string, allRunners RunnerPool) (apiClient *mocks.ExecutorApi, environment *NomadExecutionEnvironment) {
|
func newRefreshMock(returnedRunnerIds []string, allRunners RunnerPool) (apiClient *mocks.ExecutorApi, environment *NomadExecutionEnvironment) {
|
||||||
apiClient = &mocks.ExecutorApi{}
|
apiClient = &mocks.ExecutorApi{}
|
||||||
apiClient.On("LoadRunners", jobId).Return(returnedRunnerIds, nil)
|
apiClient.On("LoadAvailableRunners", jobId).Return(returnedRunnerIds, nil)
|
||||||
apiClient.On("GetJobScale", jobId).Return(len(returnedRunnerIds), nil)
|
apiClient.On("GetJobScale", jobId).Return(len(returnedRunnerIds), nil)
|
||||||
apiClient.On("SetJobScaling", jobId, mock.AnythingOfType("int"), "Runner Requested").Return(nil)
|
apiClient.On("SetJobScaling", jobId, mock.AnythingOfType("int"), "Runner Requested").Return(nil)
|
||||||
environment = &NomadExecutionEnvironment{
|
environment = &NomadExecutionEnvironment{
|
||||||
|
@@ -71,7 +71,7 @@ func (_m *ExecutorApi) LoadJobList() ([]*api.JobListStub, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// LoadRunners provides a mock function with given fields: jobId
|
// LoadRunners provides a mock function with given fields: jobId
|
||||||
func (_m *ExecutorApi) LoadRunners(jobId string) ([]string, error) {
|
func (_m *ExecutorApi) LoadAvailableRunners(jobId string) ([]string, error) {
|
||||||
ret := _m.Called(jobId)
|
ret := _m.Called(jobId)
|
||||||
|
|
||||||
var r0 []string
|
var r0 []string
|
||||||
|
@@ -7,11 +7,18 @@ import (
|
|||||||
|
|
||||||
// ExecutorApi provides access to an container orchestration solution
|
// ExecutorApi provides access to an container orchestration solution
|
||||||
type ExecutorApi interface {
|
type ExecutorApi interface {
|
||||||
|
// LoadJobList loads the list of jobs from the Nomad api.
|
||||||
LoadJobList() (list []*nomadApi.JobListStub, err error)
|
LoadJobList() (list []*nomadApi.JobListStub, err error)
|
||||||
|
// GetJobScale returns the scale of the passed job.
|
||||||
GetJobScale(jobId string) (jobScale int, err error)
|
GetJobScale(jobId string) (jobScale int, err error)
|
||||||
|
// SetJobScaling sets the scaling count of the passed job to Nomad.
|
||||||
SetJobScaling(jobId string, count int, reason string) (err error)
|
SetJobScaling(jobId string, count int, reason string) (err error)
|
||||||
LoadRunners(jobId string) (runnerIds []string, err error)
|
|
||||||
|
// DeleteRunner deletes the runner with the given Id.
|
||||||
DeleteRunner(runnerId string) (err error)
|
DeleteRunner(runnerId string) (err error)
|
||||||
|
|
||||||
|
// LoadAvailableRunners loads all allocations of the specified job which are running and not about to get stopped.
|
||||||
|
LoadAvailableRunners(jobId string) (runnerIds []string, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApiClient provides access to the Nomad functionality
|
// ApiClient provides access to the Nomad functionality
|
||||||
@@ -36,13 +43,11 @@ func (apiClient *ApiClient) init(nomadURL *url.URL) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadJobList loads the list of jobs from the Nomad api.
|
|
||||||
func (apiClient *ApiClient) LoadJobList() (list []*nomadApi.JobListStub, err error) {
|
func (apiClient *ApiClient) LoadJobList() (list []*nomadApi.JobListStub, err error) {
|
||||||
list, _, err = apiClient.client.Jobs().List(nil)
|
list, _, err = apiClient.client.Jobs().List(nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetJobScale returns the scale of the passed job.
|
|
||||||
func (apiClient *ApiClient) GetJobScale(jobId string) (jobScale int, err error) {
|
func (apiClient *ApiClient) GetJobScale(jobId string) (jobScale int, err error) {
|
||||||
status, _, err := apiClient.client.Jobs().ScaleStatus(jobId, nil)
|
status, _, err := apiClient.client.Jobs().ScaleStatus(jobId, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -53,20 +58,21 @@ func (apiClient *ApiClient) GetJobScale(jobId string) (jobScale int, err error)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetJobScaling sets the scaling count of the passed job to Nomad.
|
|
||||||
func (apiClient *ApiClient) SetJobScaling(jobId string, count int, reason string) (err error) {
|
func (apiClient *ApiClient) SetJobScaling(jobId string, count int, reason string) (err error) {
|
||||||
_, _, err = apiClient.client.Jobs().Scale(jobId, jobId, &count, reason, false, nil, nil)
|
_, _, err = apiClient.client.Jobs().Scale(jobId, jobId, &count, reason, false, nil, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadRunners loads the allocations of the specified job.
|
func (apiClient *ApiClient) LoadAvailableRunners(jobId string) (runnerIds []string, err error) {
|
||||||
func (apiClient *ApiClient) LoadRunners(jobId string) (runnerIds []string, err error) {
|
|
||||||
list, _, err := apiClient.client.Jobs().Allocations(jobId, true, nil)
|
list, _, err := apiClient.client.Jobs().Allocations(jobId, true, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, stub := range list {
|
for _, stub := range list {
|
||||||
runnerIds = append(runnerIds, stub.ID)
|
// only add allocations which are running and not about to be stopped
|
||||||
|
if stub.ClientStatus == nomadApi.AllocClientStatusRunning && stub.DesiredStatus == nomadApi.AllocDesiredStatusRun {
|
||||||
|
runnerIds = append(runnerIds, stub.ID)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user