Store default Nomad job in apiClient to cache it

This commit is contained in:
sirkrypt0
2021-05-07 10:50:09 +02:00
committed by Konrad Hanff
parent 9a5d982dfc
commit dfd34144f8
2 changed files with 11 additions and 5 deletions

View File

@ -17,9 +17,9 @@ const (
TaskNameFormat = "%s-task"
)
func (apiClient *ApiClient) defaultJob() *nomadApi.Job {
func parseJob(jobHCL string) *nomadApi.Job {
config := jobspec2.ParseConfig{
Body: []byte(defaultJobHCL),
Body: []byte(jobHCL),
AllowFS: false,
Strict: true,
}
@ -119,14 +119,14 @@ func (apiClient *ApiClient) createJob(
networkAccess bool,
exposedPorts []uint16) *nomadApi.Job {
job := apiClient.defaultJob()
job := apiClient.defaultJob
job.ID = &id
job.Name = &id
var taskGroup = createTaskGroup(&job, fmt.Sprintf(TaskGroupNameFormat, id), prewarmingPoolSize)
configureTask(taskGroup, fmt.Sprintf(TaskNameFormat, id), cpuLimit, memoryLimit, image, networkAccess, exposedPorts)
return job
return &job
}
// LoadJobList loads the list of jobs from the Nomad api.

View File

@ -19,6 +19,8 @@ type ExecutorApi interface {
// ApiClient implements the ExecutorApi interface and can be used to perform different operations on the real Executor API and its return values.
type ApiClient struct {
apiQuerier
client *nomadApi.Client
defaultJob nomadApi.Job
}
// NewExecutorApi creates a new api client.
@ -35,8 +37,12 @@ func (apiClient *ApiClient) init(nomadURL *url.URL) (err error) {
Address: nomadURL.String(),
TLSConfig: &nomadApi.TLSConfig{},
})
if err != nil {
return err
}
apiClient.defaultJob = *parseJob(defaultJobHCL)
return nil
}
// LoadRunners loads the allocations of the specified job.
func (apiClient *ApiClient) LoadRunners(jobId string) (runnerIds []string, err error) {