Fix tests for log levels and work on adding title
parent
1fbac0740a
commit
a544dcdde7
|
|
@ -13,7 +13,7 @@ func New() Logger {
|
||||||
func (l Logger) Info(m string, args ...interface{}) {
|
func (l Logger) Info(m string, args ...interface{}) {
|
||||||
args = append([]interface{}{m}, args...)
|
args = append([]interface{}{m}, args...)
|
||||||
switch m {
|
switch m {
|
||||||
case "wake":
|
case "wake", "result":
|
||||||
logb.Verbose(args...)
|
logb.Verbose(args...)
|
||||||
case "run":
|
case "run":
|
||||||
logb.Debug(args...)
|
logb.Debug(args...)
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"local/firestormy/config"
|
"local/firestormy/config"
|
||||||
"local/firestormy/config/ns"
|
"local/firestormy/config/ns"
|
||||||
|
"local/firestormy/logger"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -14,6 +15,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Job struct {
|
type Job struct {
|
||||||
|
Title string
|
||||||
Name string
|
Name string
|
||||||
Schedule string
|
Schedule string
|
||||||
Raw string
|
Raw string
|
||||||
|
|
@ -34,7 +36,7 @@ func NewJob(runner Runner, schedule, raw string) (*Job, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newBashJob(schedule, sh string) (*Job, error) {
|
func newBashJob(schedule, sh string, title ...string) (*Job, error) {
|
||||||
if !validCron(schedule) {
|
if !validCron(schedule) {
|
||||||
return nil, ErrBadCron
|
return nil, ErrBadCron
|
||||||
}
|
}
|
||||||
|
|
@ -44,6 +46,11 @@ func newBashJob(schedule, sh string) (*Job, error) {
|
||||||
Raw: sh,
|
Raw: sh,
|
||||||
Runner: Bash,
|
Runner: Bash,
|
||||||
}
|
}
|
||||||
|
if len(title) == 0 {
|
||||||
|
j.Title = j.Name
|
||||||
|
} else {
|
||||||
|
j.Title = title[0]
|
||||||
|
}
|
||||||
j.foo = func() {
|
j.foo = func() {
|
||||||
cmd := exec.Command("bash", "-c", sh)
|
cmd := exec.Command("bash", "-c", sh)
|
||||||
j.LastRun = time.Now()
|
j.LastRun = time.Now()
|
||||||
|
|
@ -62,6 +69,7 @@ func newBashJob(schedule, sh string) (*Job, error) {
|
||||||
// TODO webpage doenst load post SET despite this returning nil
|
// TODO webpage doenst load post SET despite this returning nil
|
||||||
config.Store.Set(j.Name, b, ns.Jobs...)
|
config.Store.Set(j.Name, b, ns.Jobs...)
|
||||||
}
|
}
|
||||||
|
logger.New().Info("result", fmt.Sprintf("%+v", j))
|
||||||
}
|
}
|
||||||
return j, nil
|
return j, nil
|
||||||
}
|
}
|
||||||
|
|
@ -87,6 +95,7 @@ func (j *Job) Decode(b []byte) error {
|
||||||
k, err := NewJob(j.Runner, j.Schedule, j.Raw)
|
k, err := NewJob(j.Runner, j.Schedule, j.Raw)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
k.Name = j.Name
|
k.Name = j.Name
|
||||||
|
k.Title = j.Title
|
||||||
k.LastStatus = j.LastStatus
|
k.LastStatus = j.LastStatus
|
||||||
k.LastOutput = j.LastOutput
|
k.LastOutput = j.LastOutput
|
||||||
k.LastRuntime = j.LastRuntime
|
k.LastRuntime = j.LastRuntime
|
||||||
|
|
|
||||||
|
|
@ -89,6 +89,9 @@ func TestJobEncodeDecode(t *testing.T) {
|
||||||
if k.Name != j.Name {
|
if k.Name != j.Name {
|
||||||
t.Error(k.Name, "vs", j.Name)
|
t.Error(k.Name, "vs", j.Name)
|
||||||
}
|
}
|
||||||
|
if k.Title != j.Title {
|
||||||
|
t.Error(k.Title, "vs", j.Title)
|
||||||
|
}
|
||||||
if diff := k.LastRun.Unix() - j.LastRun.Unix(); (diff > 0 && diff < int64(time.Hour)) || (diff < 0 && -1*diff < int64(time.Hour)) {
|
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)
|
t.Error(j.LastRun, "vs", k.LastRun)
|
||||||
}
|
}
|
||||||
|
|
@ -109,6 +112,7 @@ func TestJobEncodeDecode(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func captureLog() (*bytes.Buffer, func()) {
|
func captureLog() (*bytes.Buffer, func()) {
|
||||||
|
logb.Set(logb.VERBOSE)
|
||||||
was := logb.Writer()
|
was := logb.Writer()
|
||||||
wase := os.Stderr
|
wase := os.Stderr
|
||||||
f, _ := ioutil.TempFile(os.TempDir(), "test.newBashJobAndRun")
|
f, _ := ioutil.TempFile(os.TempDir(), "test.newBashJobAndRun")
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type upsertRequest struct {
|
type upsertRequest struct {
|
||||||
|
Title string `json:"title"`
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Language string `json:"language"`
|
Language string `json:"language"`
|
||||||
Cron string `json:"cron"`
|
Cron string `json:"cron"`
|
||||||
|
|
@ -35,6 +36,9 @@ func (u *upsertRequest) validate() error {
|
||||||
} else if _, err := config.Store.Get(u.ID, ns.Jobs...); err != nil {
|
} else if _, err := config.Store.Get(u.ID, ns.Jobs...); err != nil {
|
||||||
return fmt.Errorf("ID provided but not accessible: %v", err)
|
return fmt.Errorf("ID provided but not accessible: %v", err)
|
||||||
}
|
}
|
||||||
|
if u.Title == "" {
|
||||||
|
u.Title = u.ID
|
||||||
|
}
|
||||||
if u.Language == "" {
|
if u.Language == "" {
|
||||||
return errors.New("language required")
|
return errors.New("language required")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue