Browse Source

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

tags/version-1.0.1
Vladimir Smagin 11 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 @@
#!/bin/bash

VERSION="1.0"
VERSION="1.0.1"

# build gogocron
go build

+ 5
- 1
config.go View File

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

+ 1
- 0
configs/test1.yml View File

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

+ 1
- 1
debian/DEBIAN/control View File

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

+ 28
- 6
file_utils.go View File

@@ -7,6 +7,7 @@ import (
"log"
"os"
"os/exec"
"reflect"
"syscall"
"time"

@@ -50,25 +51,46 @@ func readConfigFile(path string) cronTask {
return config
}

func inArray(val interface{}, array interface{}) (exists bool, index int) {
exists = false
index = -1

switch reflect.TypeOf(array).Kind() {
case reflect.Slice:
s := reflect.ValueOf(array)

for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(val, s.Index(i).Interface()) == true {
index = i
exists = true
return
}
}
}

return
}

// run commands
func runCmd(ctx context.Context, env []string, cmdname string, params ...string) error {
log.Println("Executing", cmdname, params)
//log.Println("Executing", cmdname, params)
cmd := exec.Command(cmdname, params...)

// set env variables
cmd.Env = append(cmd.Env, os.Environ()...)
cmd.Env = append(cmd.Env, env...)

// set stdout, stderr
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

// run su command in PTY
f, err := pty.Start(cmd)
if err != nil {
panic(err)
}
io.Copy(os.Stdout, f)

// set stdout, stderr
if v, _ := inArray("LOGTYPE=full", env); v == true {
io.Copy(os.Stdout, f)
io.Copy(os.Stderr, f)
}

// Use a channel to signal completion so we can use a select statement
done := make(chan error)

+ 17
- 4
gogocron.go View File

@@ -21,6 +21,7 @@ type cronTask struct {
TimeOut string `yaml:"timeout,omitempty"` // exec with timeout, seconds
Env []string `yaml:"env,omitempty"` // array of env variables
Commands []string `yaml:"commands"` // array of commands to exec
LogType string `yaml:"logtype,omitempty"` // type of logging: info, full, disabled
}

type cronTasks []cronTask
@@ -65,8 +66,8 @@ func filterTasksToExecute(tasks cronTasks) cronTasks {

// executeTasks runs batch of planned tasks on go routines with context
func executeTasks(tasks cronTasks) {
now := time.Now()
log.Printf("Execute started: %v", now)
//now := time.Now()
//log.Printf("Execute started: %v", now)

// run every task in own context
ctx, ctxCancel := context.WithCancel(context.Background())
@@ -74,8 +75,20 @@ func executeTasks(tasks cronTasks) {

// iterate tasks and run them in go routine
for _, task := range tasks {
log.Printf("task: %#v Timeout: %#v", task.Name, task.TimeOut)
log.Printf("commands: %#v", task.Commands)

switch task.LogType {
case "full":
task.Env = append(task.Env, "LOGTYPE=full")
case "disabled":
task.Env = append(task.Env, "LOGTYPE=disabled")
default:
task.LogType = "info"
task.Env = append(task.Env, "LOGTYPE=info")
}

if task.LogType != "disabled" {
log.Printf("TASK: %#v TIMEOUT: %#v COMMANDS: %#v", task.Name, task.TimeOut, task.Commands)
}

if task.TimeOut != "" {
timeout, err := time.ParseDuration(task.TimeOut)

+ 7
- 0
readme.md View File

@@ -83,6 +83,13 @@ commands:
- echo $TESTVAR
```

## Logging of task

`logtype` can accept:
- info (default, logging only fact of run)
- full (log fact of run and stdout/stderr, useful for debugging)
- disabled (completely disable logs)

## Sample config file

```

cron.go → timing.go View File


Loading…
Cancel
Save