Fix job encode decode

This commit is contained in:
bel
2020-03-15 16:13:44 +00:00
parent dbf023e61b
commit e17e266712
111 changed files with 232 additions and 41 deletions

55
scheduler/job.go Normal file → Executable file
View File

@@ -4,18 +4,26 @@ import (
"bytes"
"encoding/gob"
"fmt"
"local/firestormy/config"
"local/firestormy/config/ns"
"local/firestormy/logger"
"os/exec"
"strings"
"time"
"github.com/google/uuid"
)
type Job struct {
Name string
Schedule string
Raw string
Runner Runner
foo func()
Name string
Schedule string
Raw string
Runner Runner
foo func()
LastStatus int
LastOutput string
LastRuntime time.Duration
LastRun time.Time
}
func NewJob(runner Runner, schedule, raw string) (*Job, error) {
@@ -31,20 +39,33 @@ func newBashJob(schedule, sh string) (*Job, error) {
if !validCron(schedule) {
return nil, ErrBadCron
}
return &Job{
j := &Job{
Name: uuid.New().String(),
Schedule: schedule,
Raw: sh,
Runner: Bash,
foo: func() {
cmd := exec.Command("bash", "-c", sh)
out, err := cmd.CombinedOutput()
if err != nil {
panic(err)
}
logger.New().Info(fmt.Sprintf("executed %s: %s", sh, out))
},
}, nil
}
j.foo = func() {
cmd := exec.Command("bash", "-c", sh)
j.LastRun = time.Now()
start := time.Now()
out, err := cmd.CombinedOutput()
j.LastRuntime = time.Since(start)
if err != nil {
out = []byte(fmt.Sprintf("error running command: %v: %v", err, out))
}
j.LastOutput = strings.TrimSpace(string(out))
if cmd != nil && cmd.ProcessState != nil {
j.LastStatus = cmd.ProcessState.ExitCode()
}
logger.New().Info(fmt.Sprintf("%+v", j))
b, err := j.Encode()
if err == nil {
// TODO webpage doenst load post SET despite this returning nil
config.Store.Set(j.Name, b, ns.Jobs...)
}
}
return j, nil
}
func (j *Job) Run() {
@@ -68,6 +89,10 @@ func (j *Job) Decode(b []byte) error {
k, err := NewJob(j.Runner, j.Schedule, j.Raw)
if err == nil {
k.Name = j.Name
k.LastStatus = j.LastStatus
k.LastOutput = j.LastOutput
k.LastRuntime = j.LastRuntime
k.LastRun = j.LastRun
*j = *k
}
return err