소스 검색

added gracefulstop period

tags/version-1.1.1
Vladimir Smagin 1 개월 전
부모
커밋
c3f3e72f52
7개의 변경된 파일64개의 추가작업 그리고 31개의 파일을 삭제
  1. 1
    1
      build.sh
  2. 4
    2
      config.go
  3. 1
    0
      configs/test1.yml
  4. 1
    1
      debian/DEBIAN/control
  5. 16
    3
      file_utils.go
  6. 21
    15
      gogocron.go
  7. 20
    9
      readme.md

+ 1
- 1
build.sh 파일 보기

@@ -1,6 +1,6 @@
1 1
 #!/bin/bash
2 2
 
3
-VERSION="1.1"
3
+VERSION="1.1.1"
4 4
 
5 5
 # build gogocron
6 6
 go build

+ 4
- 2
config.go 파일 보기

@@ -53,13 +53,15 @@ func readTaskConfigFile(path string) cronTask {
53 53
 		log.Fatalf("Error opening file %v", err)
54 54
 	}
55 55
 
56
-	//log.Printf(string(yamldata))
57
-
58 56
 	marshErr := yaml.Unmarshal(yamldata, &config)
59 57
 	if marshErr != nil {
60 58
 		log.Fatalf("Task file malformed: %v", marshErr)
61 59
 	}
62 60
 
61
+	if config.GracefulStop == "" {
62
+		config.GracefulStop = "10s"
63
+	}
64
+
63 65
 	return config
64 66
 }
65 67
 

+ 1
- 0
configs/test1.yml 파일 보기

@@ -3,6 +3,7 @@ name: "TASK #1"
3 3
 runsecond: "*/5"
4 4
 logtype: full
5 5
 user: vlad
6
+gracefulstop: 30s
6 7
 env:
7 8
   - TESTVAR="test variable"
8 9
 commands: 

+ 1
- 1
debian/DEBIAN/control 파일 보기

@@ -1,5 +1,5 @@
1 1
 Package: gogocron
2
-Version: 1.1
2
+Version: 1.1.1
3 3
 Section: base
4 4
 Priority: optional
5 5
 Installed-Size: 3 MB

+ 16
- 3
file_utils.go 파일 보기

@@ -53,6 +53,20 @@ func inArray(val interface{}, array interface{}) (exists bool, index int) {
53 53
 
54 54
 // run commands
55 55
 func runCmd(ctx context.Context, env []string, cmdname string, params ...string) error {
56
+
57
+	// Read graceful stop value from context
58
+	var gracefulStop string
59
+	if v := ctx.Value("gracefulstopvalue"); v != nil {
60
+		gracefulStop = v.(string)
61
+	} else {
62
+		gracefulStop = "10s"
63
+	}
64
+
65
+	gracefulStopSeconds, err := time.ParseDuration(gracefulStop)
66
+	if err != nil {
67
+		log.Fatalln("Error parsing gracefulstop value as time duration")
68
+	}
69
+
56 70
 	//log.Println("Executing", cmdname, params)
57 71
 	cmd := exec.Command(cmdname, params...)
58 72
 
@@ -82,9 +96,8 @@ func runCmd(ctx context.Context, env []string, cmdname string, params ...string)
82 96
 	case <-ctx.Done():
83 97
 		// Timeout happened first, kill the process and print a message.
84 98
 		cmd.Process.Signal(syscall.SIGTERM)
85
-		// graceful stop 10 seconds
86
-		// if task must be killed instead of normal termination
87
-		time.AfterFunc(10*time.Second, func() {
99
+		// gracefulStopSeconds is a time between SIGTERM and SIGKILL, default 10s, minimum 1s
100
+		time.AfterFunc(gracefulStopSeconds, func() {
88 101
 			if !cmd.ProcessState.Exited() {
89 102
 				cmd.Process.Kill()
90 103
 			}

+ 21
- 15
gogocron.go 파일 보기

@@ -31,18 +31,19 @@ type cronConfig struct {
31 31
 }
32 32
 
33 33
 type cronTask struct {
34
-	Name      string   `yaml:"name"`                // name of task
35
-	AsUser    string   `yaml:"user,omitempty"`      // run as user
36
-	RunSecond string   `yaml:"runsecond,omitempty"` // second
37
-	RunMinute string   `yaml:"runminute,omitempty"` // minute
38
-	RunHour   string   `yaml:"runhour,omitempty"`   // hour
39
-	RunDom    string   `yaml:"rundom,omitempty"`    // day of month
40
-	RunMonth  string   `yaml:"runmonth,omitempty"`  // month
41
-	RunDow    string   `yaml:"rundow,omitempty"`    // day of week
42
-	TimeOut   string   `yaml:"timeout,omitempty"`   // exec with timeout, seconds
43
-	Env       []string `yaml:"env,omitempty"`       // array of env variables
44
-	Commands  []string `yaml:"commands"`            // array of commands to exec
45
-	LogType   string   `yaml:"logtype,omitempty"`   // type of logging: info, full, disabled
34
+	Name         string   `yaml:"name"`                   // name of task
35
+	AsUser       string   `yaml:"user,omitempty"`         // run as user
36
+	RunSecond    string   `yaml:"runsecond,omitempty"`    // second
37
+	RunMinute    string   `yaml:"runminute,omitempty"`    // minute
38
+	RunHour      string   `yaml:"runhour,omitempty"`      // hour
39
+	RunDom       string   `yaml:"rundom,omitempty"`       // day of month
40
+	RunMonth     string   `yaml:"runmonth,omitempty"`     // month
41
+	RunDow       string   `yaml:"rundow,omitempty"`       // day of week
42
+	TimeOut      string   `yaml:"timeout,omitempty"`      // exec with timeout, seconds
43
+	Env          []string `yaml:"env,omitempty"`          // array of env variables
44
+	Commands     []string `yaml:"commands"`               // array of commands to exec
45
+	LogType      string   `yaml:"logtype,omitempty"`      // type of logging: info, full, disabled
46
+	GracefulStop string   `yaml:"gracefulstop,omitempty"` // seconds between task term and kill
46 47
 }
47 48
 
48 49
 type cronTasks []cronTask
@@ -133,6 +134,11 @@ func executeTask(ctx context.Context, task cronTask) {
133 134
 	// flag to stop timed out tasks
134 135
 	timeoutFlag := false
135 136
 
137
+	// send gracefulstop with context
138
+	type contextKey string
139
+	gracefulStop := contextKey("gracefulstopvalue")
140
+	ctxRunCmd := context.WithValue(ctx, gracefulStop, task.GracefulStop)
141
+
136 142
 	// iterate commands list
137 143
 	for _, cmd := range task.Commands {
138 144
 		if timeoutFlag {
@@ -140,19 +146,19 @@ func executeTask(ctx context.Context, task cronTask) {
140 146
 		}
141 147
 
142 148
 		select {
143
-		case <-ctx.Done():
149
+		case <-ctxRunCmd.Done():
144 150
 			log.Printf("Timeout exceeded for task %v", task.Name)
145 151
 			timeoutFlag = true
146 152
 		default:
147 153
 			// su required to run as other users, requires pty to run
148
-			runCmd(ctx, task.Env, "su", "-c", cmd, task.AsUser)
154
+			runCmd(ctxRunCmd, task.Env, "su", "-c", cmd, task.AsUser)
149 155
 		}
150 156
 	}
151 157
 
152 158
 }
153 159
 
154 160
 func main() {
155
-	log.Println("gogocron 1.1 by Vladimir Smagin (http://gogocron.blindage.org)")
161
+	log.Println("gogocron 1.1.1 by Vladimir Smagin (http://gogocron.blindage.org)")
156 162
 
157 163
 	// Load config at startup, once
158 164
 	gogoConfig := loadConfig()

+ 20
- 9
readme.md 파일 보기

@@ -13,12 +13,7 @@ Cron daemon with crontab syntax, small interval and execution timeout for each t
13 13
  - run task commands as other user
14 14
  - timeout for whole task with default graceful period 10s
15 15
  - SIGHUP process to reload config files
16
-
17
-### Planned
18
-
19
- - *1.1.1*: Adjustable graceful timeout period for each task, current default is 10s. 
20
-
21
-   By default if task not die after SIGTERM in 10s cron send SIGKILL signal. In future you can adjust this value for each task.
16
+ - adjustable graceful timeout period for each command in task
22 17
 
23 18
 ## Installation
24 19
 
@@ -105,16 +100,32 @@ rundow    // day of week
105 100
 
106 101
 #### Timeout
107 102
 
108
-Golang's time.Duration definition, string.
103
+If whole task (not one command in task) running more than timeout cron send SIGTERM signal, by default timeout is turned off.
104
+
105
+```
106
+timeout: 3h12m4s
107
+```
108
+
109
+It's a golang's [time.Duration](https://golang.org/pkg/time/#Duration) definition, string:
109 110
 
110 111
  - 1s - one second
111 112
  - 1m10s - one minute and 10 seconds
112 113
 
114
+
115
+#### Graceful stop
116
+
117
+By default if task's command not die after SIGTERM in 10s cron send SIGKILL signal. You may need this if your program receiving signals but internal queue not empty for immidiately stop.
118
+
119
+You can change this time with:
120
+
113 121
 ```
114
-timeout: 3h12m4s
122
+gracefulstop: 30s
115 123
 ```
116 124
 
117
-By default if task not die after SIGTERM in 10s cron send SIGKILL signal.
125
+It's a golang's [time.Duration](https://golang.org/pkg/time/#Duration) definition, string:
126
+
127
+ - 1s - one second
128
+ - 1m10s - one minute and 10 seconds
118 129
 
119 130
 #### ENV
120 131
 

Loading…
취소
저장