Skip to content

Commit

Permalink
added new methods for nomad
Browse files Browse the repository at this point in the history
  • Loading branch information
Allan-Nava authored Oct 29, 2024
1 parent 6906961 commit a0b92bd
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 3 deletions.
15 changes: 15 additions & 0 deletions nomad/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package nomad

type ScaleRequest struct {
Count int `json:"Count,omitempty"`
Target ScaleGroupRequest `json:"Target"`
}

type ScaleGroupRequest struct {
Group string `json:"Group,omitempty"`
}

type RunJobRequest struct {
Job JobDefinition `json:"Job"`
Format string `json:"Format,omitempty"`
}
121 changes: 118 additions & 3 deletions nomad/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,35 @@ type IService interface {
GetDefinition(jobid, region string) (*JobDefinition, error)
AllocationStats(allocID, region string) (*ResourceUsage, error)
GetAllocations(clientID, region string) (*NomadAllocations, error)
DeleteJob(jobid, region string, purge bool) error
ScaleJob(jobid string, count int, region string) error
RunJob(definition JobDefinition, region string) error
RestartJob(jobid, region string) error
}

type service struct {
client *resty.Client
logger *zap.SugaredLogger
}

func NewService(baseUrl string, logger *zap.SugaredLogger) IService {
type Options struct {
BaseUrl string
LogLevel string
Logger *zap.SugaredLogger
}

func NewService(options Options) IService {
client := resty.New()
client.SetBaseURL(baseUrl)
client.SetBaseURL(options.BaseUrl)
if options.LogLevel == "debug" {
client.SetDebug(true)
}
if options.Logger == nil {
options.Logger = zap.NewNop().Sugar()
}
return &service{
client: client,
logger: logger,
logger: options.Logger,
}
}

Expand Down Expand Up @@ -103,3 +119,102 @@ func (s *service) GetAllocations(clientID, region string) (*NomadAllocations, er
}
return &obj, nil
}


func (s *service) RestartJob(jobid, region string) error {
err := s.ScaleJob(jobid, 0, region)
if err != nil {
s.logger.Errorf("Error scaling job while stopping: %v", err)
return err
}

go func() {
time.Sleep(time.Second * 1)

Check failure on line 132 in nomad/service.go

View workflow job for this annotation

GitHub Actions / build (1.22.x)

undefined: time

Check failure on line 132 in nomad/service.go

View workflow job for this annotation

GitHub Actions / build (1.20.x)

undefined: time

Check failure on line 132 in nomad/service.go

View workflow job for this annotation

GitHub Actions / build (1.21.x)

undefined: time

Check failure on line 132 in nomad/service.go

View workflow job for this annotation

GitHub Actions / build (1.22.x)

undefined: time
err = s.ScaleJob(jobid, 1, region)
if err != nil {
s.logger.Errorf("Error restarting job while starting: %v", err)
}
}()

return nil
}

func (s *service) DeleteJob(jobid, region string, purge bool) error {
params := map[string]string{
"region": region,
"purge": strconv.FormatBool(purge),

Check failure on line 145 in nomad/service.go

View workflow job for this annotation

GitHub Actions / build (1.22.x)

undefined: strconv

Check failure on line 145 in nomad/service.go

View workflow job for this annotation

GitHub Actions / build (1.20.x)

undefined: strconv

Check failure on line 145 in nomad/service.go

View workflow job for this annotation

GitHub Actions / build (1.21.x)

undefined: strconv

Check failure on line 145 in nomad/service.go

View workflow job for this annotation

GitHub Actions / build (1.22.x)

undefined: strconv
}

resp, err := s.client.
R().
SetQueryParams(params).
Delete("v1/job/" + jobid)

if err != nil {
s.logger.Errorf("Error stopping job: %v", err)
return err
}
if resp.IsError() {
return fmt.Errorf("StopJob error %s", resp.String())
}

return nil
}

func (s *service) ScaleJob(jobid string, count int, region string) error {
params := map[string]string{
"region": region,
}

request := ScaleRequest{
Count: count,
Target: ScaleGroupRequest{
Group: "restreamer",
},
}

resp, err := s.client.
R().
SetQueryParams(params).
SetBody(request).
SetHeader("Content-Type", "application/json").
Post("v1/job/" + jobid + "scale")

if err != nil {
s.logger.Errorf("Error starting job: %v", err)
return err
}
if resp.IsError() {
return fmt.Errorf("StartJob error %s", resp.String())
}

return nil
}

func (s *service) RunJob(definition JobDefinition, region string) error {
params := map[string]string{
"region": region,
}

request := RunJobRequest{
Job: definition,
Format: "json",
}

resp, err := s.client.
R().
SetQueryParams(params).
SetBody(request).
SetHeader("content-type", "application/json").
Post("v1/jobs")

if err != nil {
s.logger.Errorf("Error starting job: %v", err)
return err
}
if resp.IsError() {
return fmt.Errorf("StartJob error %s", resp.String())
}

return nil
}

0 comments on commit a0b92bd

Please sign in to comment.