Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Make job-local work with docker labels #71

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ func (c *Config) dockerLabelsUpdate(labels map[string]map[string]string) {
var parsedLabelConfig Config
parsedLabelConfig.buildFromDockerLabels(labels)

// -- Refresh ExecJobs --

// Calculate the delta
for name, j := range c.ExecJobs {
// this prevents deletion of jobs that were added by reading a configuration file
Expand Down Expand Up @@ -175,6 +177,62 @@ func (c *Config) dockerLabelsUpdate(labels map[string]map[string]string) {
}
}

// -- Refresh LocalJobs --

// Calculate the delta
for name, j := range c.LocalJobs {
// this prevents deletion of jobs that were added by reading a configuration file
if !j.FromDockerLabel {
continue
}

found := false
for newJobsName, newJob := range parsedLabelConfig.LocalJobs {
// 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 different params
// so, lets take care of it by simply restarting
// For the hash to work properly, we must fill the fields before calling it
defaults.SetDefaults(newJob)
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.LocalJobs[name] = newJob
}
break
}
}
if !found {
// Remove the job
c.sh.RemoveJob(j)
delete(c.LocalJobs, name)
}
}

// Check for aditions
for newJobsName, newJob := range parsedLabelConfig.LocalJobs {
found := false
for name := range c.LocalJobs {
if name == newJobsName {
found = true
break
}
}
if !found {
defaults.SetDefaults(newJob)
newJob.Name = newJobsName
newJob.buildMiddlewares()
c.sh.AddJob(newJob)
c.LocalJobs[newJobsName] = newJob
}
}

}

// ExecJobConfig contains all configuration params needed to build a ExecJob
Expand Down Expand Up @@ -231,6 +289,7 @@ type LocalJobConfig struct {
middlewares.SaveConfig `mapstructure:",squash"`
middlewares.MailConfig `mapstructure:",squash"`
middlewares.GotifyConfig `mapstructure:",squash"`
FromDockerLabel bool `mapstructure:"fromDockerLabel"`
}

func (c *LocalJobConfig) buildMiddlewares() {
Expand Down
1 change: 1 addition & 0 deletions cli/docker-labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (c *Config) buildFromDockerLabels(labels map[string]map[string]string) erro
case jobType == jobLocal && isServiceContainer:
if _, ok := localJobs[jobName]; !ok {
localJobs[jobName] = make(map[string]interface{})
localJobs[jobName]["fromDockerLabel"] = true
}
setJobParam(localJobs[jobName], jopParam, v)
case jobType == jobServiceRun && isServiceContainer:
Expand Down
8 changes: 8 additions & 0 deletions core/localjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"os/exec"
"reflect"

"github.com/gobs/args"
)
Expand All @@ -16,6 +17,13 @@ func NewLocalJob() *LocalJob {
return &LocalJob{}
}

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

func (j *LocalJob) Run(ctx *Context) error {
cmd, err := j.buildCommand(ctx)
if err != nil {
Expand Down
Loading