Browse Source

logging control, check task for minimal count of params, new inArray() func

tags/version-1.0.1
Vladimir Smagin 4 months ago
parent
commit
fa112de1a8
8 changed files with 60 additions and 13 deletions
  1. 1
    1
      build.sh
  2. 5
    1
      config.go
  3. 1
    0
      configs/test1.yml
  4. 1
    1
      debian/DEBIAN/control
  5. 28
    6
      file_utils.go
  6. 17
    4
      gogocron.go
  7. 7
    0
      readme.md
  8. 0
    0
      timing.go

+ 1
- 1
build.sh View File

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

+ 5
- 1
config.go View File

@@ -13,7 +13,11 @@ func loadConfig() cronTasks {
13 13
 	for _, configFile := range configFiles {
14 14
 		config := readConfigFile(configFile)
15 15
 		//log.Printf("Parsed %#v", config)
16
-		tasks = append(tasks, config)
16
+		if config.Name != "" && len(config.Commands) > 0 {
17
+			tasks = append(tasks, config)
18
+		} else {
19
+			log.Printf("Config file %v malformed", configFile)
20
+		}
17 21
 	}
18 22
 	return tasks
19 23
 }

+ 1
- 0
configs/test1.yml View File

@@ -1,6 +1,7 @@
1 1
 ---
2 2
 name: "Print base64 of 20 random symbols"
3 3
 runsecond: "*/5"
4
+logtype: disabled
4 5
 env:
5 6
   - TESTVAR="test variable"
6 7
 commands: 

+ 1
- 1
debian/DEBIAN/control View File

@@ -1,5 +1,5 @@
1 1
 Package: gogocron
2
-Version: 1.0
2
+Version: 1.0.1
3 3
 Section: base
4 4
 Priority: optional
5 5
 Architecture: all

+ 28
- 6
file_utils.go View File

@@ -7,6 +7,7 @@ import (
7 7
 	"log"
8 8
 	"os"
9 9
 	"os/exec"
10
+	"reflect"
10 11
 	"syscall"
11 12
 	"time"
12 13
 
@@ -50,25 +51,46 @@ func readConfigFile(path string) cronTask {
50 51
 	return config
51 52
 }
52 53
 
54
+func inArray(val interface{}, array interface{}) (exists bool, index int) {
55
+	exists = false
56
+	index = -1
57
+
58
+	switch reflect.TypeOf(array).Kind() {
59
+	case reflect.Slice:
60
+		s := reflect.ValueOf(array)
61
+
62
+		for i := 0; i < s.Len(); i++ {
63
+			if reflect.DeepEqual(val, s.Index(i).Interface()) == true {
64
+				index = i
65
+				exists = true
66
+				return
67
+			}
68
+		}
69
+	}
70
+
71
+	return
72
+}
73
+
53 74
 // run commands
54 75
 func runCmd(ctx context.Context, env []string, cmdname string, params ...string) error {
55
-	log.Println("Executing", cmdname, params)
76
+	//log.Println("Executing", cmdname, params)
56 77
 	cmd := exec.Command(cmdname, params...)
57 78
 
58 79
 	// set env variables
59 80
 	cmd.Env = append(cmd.Env, os.Environ()...)
60 81
 	cmd.Env = append(cmd.Env, env...)
61 82
 
62
-	// set stdout, stderr
63
-	cmd.Stdout = os.Stdout
64
-	cmd.Stderr = os.Stderr
65
-
66 83
 	// run su command in PTY
67 84
 	f, err := pty.Start(cmd)
68 85
 	if err != nil {
69 86
 		panic(err)
70 87
 	}
71
-	io.Copy(os.Stdout, f)
88
+
89
+	// set stdout, stderr
90
+	if v, _ := inArray("LOGTYPE=full", env); v == true {
91
+		io.Copy(os.Stdout, f)
92
+		io.Copy(os.Stderr, f)
93
+	}
72 94
 
73 95
 	// Use a channel to signal completion so we can use a select statement
74 96
 	done := make(chan error)

+ 17
- 4
gogocron.go View File

@@ -21,6 +21,7 @@ type cronTask struct {
21 21
 	TimeOut   string   `yaml:"timeout,omitempty"`   // exec with timeout, seconds
22 22
 	Env       []string `yaml:"env,omitempty"`       // array of env variables
23 23
 	Commands  []string `yaml:"commands"`            // array of commands to exec
24
+	LogType   string   `yaml:"logtype,omitempty"`   // type of logging: info, full, disabled
24 25
 }
25 26
 
26 27
 type cronTasks []cronTask
@@ -65,8 +66,8 @@ func filterTasksToExecute(tasks cronTasks) cronTasks {
65 66
 
66 67
 // executeTasks runs batch of planned tasks on go routines with context
67 68
 func executeTasks(tasks cronTasks) {
68
-	now := time.Now()
69
-	log.Printf("Execute started: %v", now)
69
+	//now := time.Now()
70
+	//log.Printf("Execute started: %v", now)
70 71
 
71 72
 	// run every task in own context
72 73
 	ctx, ctxCancel := context.WithCancel(context.Background())
@@ -74,8 +75,20 @@ func executeTasks(tasks cronTasks) {
74 75
 
75 76
 	// iterate tasks and run them in go routine
76 77
 	for _, task := range tasks {
77
-		log.Printf("task: %#v Timeout: %#v", task.Name, task.TimeOut)
78
-		log.Printf("commands: %#v", task.Commands)
78
+
79
+		switch task.LogType {
80
+		case "full":
81
+			task.Env = append(task.Env, "LOGTYPE=full")
82
+		case "disabled":
83
+			task.Env = append(task.Env, "LOGTYPE=disabled")
84
+		default:
85
+			task.LogType = "info"
86
+			task.Env = append(task.Env, "LOGTYPE=info")
87
+		}
88
+
89
+		if task.LogType != "disabled" {
90
+			log.Printf("TASK: %#v TIMEOUT: %#v COMMANDS: %#v", task.Name, task.TimeOut, task.Commands)
91
+		}
79 92
 
80 93
 		if task.TimeOut != "" {
81 94
 			timeout, err := time.ParseDuration(task.TimeOut)

+ 7
- 0
readme.md View File

@@ -83,6 +83,13 @@ commands:
83 83
   - echo $TESTVAR
84 84
 ```
85 85
 
86
+## Logging of task
87
+
88
+`logtype` can accept:
89
+ - info (default, logging only fact of run)
90
+ - full (log fact of run and stdout/stderr, useful for debugging)
91
+ - disabled (completely disable logs)
92
+
86 93
 ## Sample config file
87 94
 
88 95
 ```

cron.go → timing.go View File


Loading…
Cancel
Save