wip: feat(task): added AutoStart and AutoRestart on ServiceTask

This commit is contained in:
Suyono 2024-01-01 11:35:18 +11:00
parent 5bae155b3b
commit 98d57cda84
3 changed files with 109 additions and 7 deletions

View File

@ -8,7 +8,7 @@ import (
type Tasks interface { type Tasks interface {
List() []Task List() []Task
Services() []Task Services() []ServiceTask
Crones() []CronTask Crones() []CronTask
Get(string) (Task, error) Get(string) (Task, error)
} }
@ -25,7 +25,7 @@ type Task interface {
Environ() []string Environ() []string
Setsid() bool Setsid() bool
UserGroup() UserGroup UserGroup() UserGroup
Background() bool Background() bool //NOTE: implies using wmpidproxy
WorkingDir() string WorkingDir() string
Status() TaskStatus Status() TaskStatus
} }
@ -35,6 +35,12 @@ type CronTask interface {
TimeToRun(time.Time) bool TimeToRun(time.Time) bool
} }
type ServiceTask interface {
Task
AutoStart() bool
AutoRestart() bool
}
type Config interface { type Config interface {
Tasks() Tasks Tasks() Tasks
} }

View File

@ -5,13 +5,13 @@ import (
) )
type Tasks struct { type Tasks struct {
services []wminit.Task services []wminit.ServiceTask
crones []wminit.CronTask crones []wminit.CronTask
} }
func NewTasks() *Tasks { func NewTasks() *Tasks {
return &Tasks{ return &Tasks{
services: make([]wminit.Task, 0), services: make([]wminit.ServiceTask, 0),
crones: make([]wminit.CronTask, 0), crones: make([]wminit.CronTask, 0),
} }
} }
@ -28,15 +28,22 @@ func (ts *Tasks) AddV0Cron(schedule CronSchedule, path string) {
CronSchedule: schedule, CronSchedule: schedule,
name: path, name: path,
command: []string{path}, command: []string{path},
hasRun: false,
}) })
} }
func (ts *Tasks) List() []wminit.Task { func (ts *Tasks) List() []wminit.Task {
panic("not implemented") retval := make([]wminit.Task, 0, len(ts.services)+len(ts.crones))
return nil for _, s := range ts.services {
retval = append(retval, s.(wminit.Task))
}
for _, c := range ts.crones {
retval = append(retval, c.(wminit.Task))
}
return retval
} }
func (ts *Tasks) Services() []wminit.Task { func (ts *Tasks) Services() []wminit.ServiceTask {
return ts.services return ts.services
} }
@ -93,3 +100,13 @@ func (t *Task) Status() wminit.TaskStatus {
panic("not implemented") panic("not implemented")
return nil return nil
} }
func (t *Task) AutoStart() bool {
panic("not implemented")
return false
}
func (t *Task) AutoRestart() bool {
panic("not implemented")
return false
}

79
task/task_test.go Normal file
View File

@ -0,0 +1,79 @@
package task
import (
wminit "gitea.suyono.dev/suyono/wingmate/init"
"github.com/stretchr/testify/assert"
"testing"
)
func TestServicesV0(t *testing.T) {
service := "/path/to/executable"
tasks := NewTasks()
tasks.AddV0Service(service)
assert.Equal(t, tasks.Services()[0].Name(), service)
assert.ElementsMatch(t, tasks.Services()[0].Command(), []string{service})
}
func TestCronV0(t *testing.T) {
cron := "/path/to/executable"
tasks := NewTasks()
tasks.AddV0Cron(CronSchedule{
Minute: NewCronAnySpec(),
Hour: NewCronAnySpec(),
DoM: NewCronAnySpec(),
Month: NewCronAnySpec(),
DoW: NewCronAnySpec(),
}, cron)
assert.Equal(t, tasks.Crones()[0].Name(), cron)
assert.ElementsMatch(t, tasks.Crones()[0].Command(), []string{cron})
}
func TestTasks_List(t *testing.T) {
tasks := NewTasks()
tasks.services = []wminit.ServiceTask{
&Task{
name: "one",
command: []string{"/path/to/executable"},
},
&Task{
name: "two",
command: []string{"/path/to/executable"},
},
}
tasks.crones = []wminit.CronTask{
&Cron{
CronSchedule: CronSchedule{
Minute: NewCronAnySpec(),
Hour: NewCronAnySpec(),
DoM: NewCronAnySpec(),
Month: NewCronAnySpec(),
DoW: NewCronAnySpec(),
},
name: "cron-one",
command: []string{"/path/to/executable"},
},
&Cron{
CronSchedule: CronSchedule{
Minute: NewCronAnySpec(),
Hour: NewCronAnySpec(),
DoM: NewCronAnySpec(),
Month: NewCronAnySpec(),
DoW: NewCronAnySpec(),
},
name: "cron-two",
command: []string{"/path/to/executable"},
},
}
tl := tasks.List()
tnames := make([]string, 0)
testNames := []string{"one", "two", "cron-one", "cron-two"}
for _, ti := range tl {
tnames = append(tnames, ti.Name())
}
assert.ElementsMatch(t, testNames, tnames)
}