Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to change cleanup time after signal #25

Merged
merged 1 commit into from
Apr 28, 2022
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
6 changes: 3 additions & 3 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ func (a *Agent) Status() *models.Status {
}

// Signal sends the signal to the processes running
// if processes do not terminate for 120 seconds,
// cancel all processes which will send signal -1 to the processes.
// if processes do not terminate after MaxCleanUp time,
// will send signal -1 to the processes.
func (a *Agent) Signal(sig os.Signal) {
log.Printf("Sending %s signal to running child processes.", sig)
done := make(chan bool)
Expand All @@ -112,7 +112,7 @@ func (a *Agent) Signal(sig os.Signal) {
select {
case <-done:
log.Printf("All child processes have been terminated.")
case <-time.After(time.Second * 120):
case <-time.After(a.DAG.MaxCleanUpTime):
a.Cancel(sig)
default:
log.Printf("Waiting for child processes to exit...")
Expand Down
10 changes: 9 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Config struct {
MaxActiveRuns int
Params []string
DefaultParams string
MaxCleanUpTime time.Duration
}

type HandlerOn struct {
Expand Down Expand Up @@ -79,6 +80,9 @@ func (c *Config) setup(file string) {
if c.HistRetentionDays == 0 {
c.HistRetentionDays = 7
}
if c.MaxCleanUpTime == 0 {
c.MaxCleanUpTime = time.Minute * 5
}
dir := path.Dir(file)
for _, step := range c.Steps {
c.setupStep(step, dir)
Expand Down Expand Up @@ -109,7 +113,7 @@ func (c *Config) Clone() *Config {
}

func (c *Config) String() string {
ret := fmt.Sprintf("{\n")
ret := "{\n"
ret = fmt.Sprintf("%s\tName: %s\n", ret, c.Name)
ret = fmt.Sprintf("%s\tDescription: %s\n", ret, strings.TrimSpace(c.Description))
ret = fmt.Sprintf("%s\tEnv: %v\n", ret, strings.Join(c.Env, ", "))
Expand Down Expand Up @@ -230,6 +234,10 @@ func buildFromDefinition(def *configDefinition, file string, globalConfig *Confi
c.Preconditions = loadPreCondition(def.Preconditions)
c.MaxActiveRuns = def.MaxActiveRuns

if def.MaxCleanUpTimeSec != nil {
c.MaxCleanUpTime = time.Second * time.Duration(*def.MaxCleanUpTimeSec)
}

return c, nil
}

Expand Down
8 changes: 0 additions & 8 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,3 @@ func TestAssertStepDefinition(t *testing.T) {
_, err = loader.Load(path.Join(testDir, "config_err_step_no_command.yaml"), "")
require.Equal(t, err, fmt.Errorf("step command must be specified."))
}

func TestReadConfig(t *testing.T) {
f, err := config.ReadConfig(testConfig)
require.NoError(t, err)
if len(f) == 0 {
t.Error("reading yaml file failed")
}
}
1 change: 1 addition & 0 deletions internal/config/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type configDefinition struct {
Preconditions []*conditionDef
MaxActiveRuns int
Params string
MaxCleanUpTimeSec *int
}

type conditionDef struct {
Expand Down
14 changes: 12 additions & 2 deletions internal/config/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ func TestLoadConfig(t *testing.T) {
Failure: stepm[constants.OnFailure],
Cancel: stepm[constants.OnCancel],
},
MaxCleanUpTime: time.Second * 500,
}
assert.Equal(t, cfg, want)
assert.Equal(t, want, cfg)
}

func TestLoadGlobalConfig(t *testing.T) {
Expand Down Expand Up @@ -161,5 +162,14 @@ func TestLoadGlobalConfig(t *testing.T) {
},
Preconditions: []*config.Condition{},
}
assert.Equal(t, cfg, want)
assert.Equal(t, want, cfg)
}

func TestLoadDeafult(t *testing.T) {
loader := config.NewConfigLoader()
cfg, err := loader.Load(path.Join(testDir, "config_default.yaml"), "")
require.NoError(t, err)

assert.Equal(t, time.Minute*5, cfg.MaxCleanUpTime)
assert.Equal(t, 7, cfg.HistRetentionDays)
}
4 changes: 4 additions & 0 deletions tests/testdata/config_default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: test DAG
steps:
- name: "1"
command: "true"
1 change: 1 addition & 0 deletions tests/testdata/config_load.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ handlerOn:
command: "onFailure.sh"
cancel:
command: "onCancel.sh"
maxCleanupTimeSec: 500

steps:
- name: "1"
Expand Down