Skip to content

Commit

Permalink
Merge branch 'handleChangesOnDocker'
Browse files Browse the repository at this point in the history
  • Loading branch information
rdelcorro committed Dec 26, 2020
2 parents 6cf8cbd + 333070e commit 5bf6adb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 12 deletions.
13 changes: 9 additions & 4 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,20 @@ func (c *Config) dockerLabelsUpdate(labels map[string]map[string]string) {
// Check if the schedule has changed
if name == newJobsName {
found = true
// There is a slight race condition were a job can be canceled / restarted with a different schedule
// There is a slight race condition were a job can be canceled / restarted with different params
// so, lets take care of it by simply restarting
if newJob.GetSchedule() != j.GetSchedule() {
// Restart the job
// For the hash to work properly, we must fill the fields before calling it
defaults.SetDefaults(newJob)
newJob.Client = c.dockerHandler.GetInternalDockerClient()
newJob.Name = newJobsName
if newJob.Hash() != j.Hash() {
// Remove from the scheduler
c.sh.RemoveJob(j)
// Add the job back to the scheduler
newJob.buildMiddlewares()
c.sh.AddJob(newJob)
// Update the job config
c.ExecJobs[name] = newJob
c.sh.AddJob(j)
}
break
}
Expand Down
6 changes: 3 additions & 3 deletions core/job.go → core/bare_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

type BareJob struct {
Schedule string
Name string
Command string
Schedule string `hash:"true"`
Name string `hash:"true"`
Command string `hash:"true"`

middlewareContainer
running int32
Expand Down
File renamed without changes.
29 changes: 29 additions & 0 deletions core/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -295,3 +296,31 @@ func buildAuthConfiguration(registry string) docker.AuthConfiguration {

return auth
}

const HashmeTagName = "hash"

func getHash(t reflect.Type, v reflect.Value, hash *string) {
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
fieldv := v.Field(i)
kind := field.Type.Kind()

if kind == reflect.Struct {
getHash(field.Type, fieldv, hash)
continue
}

hashmeTag := field.Tag.Get(HashmeTagName)
if hashmeTag == "true" {
if kind == reflect.String {
*hash += fieldv.String()
} else if kind == reflect.Int32 || kind == reflect.Int || kind == reflect.Int64 || kind == reflect.Int16 || kind == reflect.Int8 {
*hash += strconv.FormatInt(fieldv.Int(), 10)
} else if kind == reflect.Bool {
*hash += strconv.FormatBool(fieldv.Bool())
} else {
panic("Unsupported field type")
}
}
}
}
14 changes: 11 additions & 3 deletions core/execjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"fmt"
"reflect"

docker "github.com/fsouza/go-dockerclient"
"github.com/gobs/args"
Expand All @@ -10,9 +11,9 @@ import (
type ExecJob struct {
BareJob `mapstructure:",squash"`
Client *docker.Client `json:"-"`
Container string
User string `default:"root"`
TTY bool `default:"false"`
Container string `hash:"true"`
User string `default:"root" hash:"true"`
TTY bool `default:"false" hash:"true"`
}

func NewExecJob(c *docker.Client) *ExecJob {
Expand All @@ -32,6 +33,13 @@ func (j *ExecJob) Run(ctx *Context) error {
return j.inspectExec(exec)
}

// Returns a hash of all the job attributes. Used to detect changes
func (j *ExecJob) Hash() string {
var hash string
getHash(reflect.TypeOf(j).Elem(), reflect.ValueOf(j).Elem(), &hash)
return hash
}

func (j *ExecJob) buildExec() (*docker.Exec, error) {
exec, err := j.Client.CreateExec(docker.CreateExecOptions{
AttachStdin: false,
Expand Down
4 changes: 2 additions & 2 deletions core/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ func (s *Scheduler) AddJob(j Job) error {
}
j.SetCronJobID(int(id)) // Cast to int in order to avoid pushing cron external to common
j.Use(s.Middlewares()...)
s.Logger.Noticef("New job registered %q - %q - %q - %v", j.GetName(), j.GetCommand(), j.GetSchedule(), id)
s.Logger.Noticef("New job registered %q - %q - %q - ID: %v", j.GetName(), j.GetCommand(), j.GetSchedule(), id)
return nil
}

func (s *Scheduler) RemoveJob(j Job) error {
s.Logger.Noticef("Job deregistered (will not fire again) %q - %q - %q", j.GetName(), j.GetCommand(), j.GetSchedule())
s.Logger.Noticef("Job deregistered (will not fire again) %q - %q - %q - ID: %v", j.GetName(), j.GetCommand(), j.GetSchedule(), j.GetCronJobID())
s.cron.Remove(cron.EntryID(j.GetCronJobID()))
return nil
}
Expand Down

0 comments on commit 5bf6adb

Please sign in to comment.