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

0
scheduler/errors.go Normal file → Executable file
View File

0
scheduler/errors_test.go Normal file → Executable file
View File

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

24
scheduler/job_test.go Normal file → Executable file
View File

@@ -3,9 +3,12 @@ package scheduler
import (
"bytes"
"io/ioutil"
"local/firestormy/config"
"local/logb"
"local/storage"
"os"
"testing"
"time"
)
func TestNewBashJobBadCron(t *testing.T) {
@@ -16,6 +19,7 @@ func TestNewBashJobBadCron(t *testing.T) {
}
func TestNewBashJobAndRun(t *testing.T) {
config.Store = storage.NewMap()
cases := []struct {
sched string
cmd string
@@ -45,12 +49,7 @@ func TestNewBashJobAndRun(t *testing.T) {
t.Error(err)
continue
}
func() {
defer func() {
recover()
}()
j.Run()
}()
j.Run()
if !bytes.Contains(b.Bytes(), []byte(c.out)) {
t.Errorf("(%s, %s) => %s", c.sched, c.cmd, b.Bytes())
}
@@ -58,6 +57,7 @@ func TestNewBashJobAndRun(t *testing.T) {
}
func TestJobEncodeDecode(t *testing.T) {
config.Store = storage.NewMap()
buff, clean := captureLog()
defer clean()
@@ -89,6 +89,18 @@ func TestJobEncodeDecode(t *testing.T) {
if k.Name != j.Name {
t.Error(k.Name, "vs", j.Name)
}
if diff := k.LastRun.Unix() - j.LastRun.Unix(); (diff > 0 && diff < int64(time.Hour)) || (diff < 0 && -1*diff < int64(time.Hour)) {
t.Error(j.LastRun, "vs", k.LastRun)
}
if k.LastStatus != j.LastStatus {
t.Error(j.LastStatus, "vs", k.LastStatus)
}
if k.LastRuntime != j.LastRuntime {
t.Error(j.LastRuntime, "vs", k.LastRuntime)
}
if string(k.LastOutput) != string(j.LastOutput) {
t.Error(j.LastOutput, "vs", k.LastOutput)
}
k.foo()
if !bytes.Contains(buff.Bytes(), []byte(os.Getenv("HOSTNAME"))) {

0
scheduler/parser.go Normal file → Executable file
View File

0
scheduler/parser_test.go Normal file → Executable file
View File

20
scheduler/runner.go Normal file → Executable file
View File

@@ -5,3 +5,23 @@ type Runner int
const (
Bash Runner = iota + 1
)
func (r Runner) String() string {
switch r {
case Bash:
return "bash"
default:
return ""
}
}
func NewRunner(s string) Runner {
for _, r := range []Runner{
Bash,
} {
if r.String() == s {
return r
}
}
return 0
}

9
scheduler/scheduler.go Normal file → Executable file
View File

@@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"local/firestormy/config"
"local/firestormy/config/ns"
"local/firestormy/logger"
"regexp"
"strings"
@@ -117,7 +118,7 @@ func (s *Scheduler) Stop() error {
}
func (s *Scheduler) List() ([]*Job, error) {
entries, err := config.Store.List(nil)
entries, err := config.Store.List(ns.Jobs)
if err != nil {
return nil, err
}
@@ -139,7 +140,7 @@ func (s *Scheduler) List() ([]*Job, error) {
}
func (s *Scheduler) loadJobFromStore(k string) (*Job, error) {
b, err := config.Store.Get(k)
b, err := config.Store.Get(k, ns.Jobs...)
if err != nil {
return nil, err
}
@@ -160,7 +161,7 @@ func (s *Scheduler) Add(j *Job) error {
if err != nil {
return err
}
if err := config.Store.Set(j.Name, b); err != nil {
if err := config.Store.Set(j.Name, b, ns.Jobs...); err != nil {
return err
}
s.running[j.Name] = entryID
@@ -178,7 +179,7 @@ func (s *Scheduler) Remove(j *Job) error {
if was == is {
return ErrJobNotFound
}
return config.Store.Set(j.Name, nil)
return config.Store.Set(j.Name, nil, ns.Jobs...)
}
func (s *Scheduler) getEntry(j *Job) (cron.EntryID, bool) {

0
scheduler/scheduler_test.go Normal file → Executable file
View File