wip: feat(wingmate): convert from new config to task

This commit is contained in:
Suyono 2024-01-12 11:33:06 +11:00
parent 1926598c0f
commit 2c9bc8b56d
9 changed files with 64 additions and 40 deletions

View File

@ -3,7 +3,7 @@ all:
go build -v go build -v
clean: clean:
rm version.txt echo "dev" > version.txt
go clean -i -cache -testcache go clean -i -cache -testcache
install: install:

View File

@ -3,7 +3,7 @@ all:
go build -v go build -v
clean: clean:
rm version.txt echo "dev" > version.txt
go clean -i -cache -testcache go clean -i -cache -testcache
install: install:

View File

@ -3,7 +3,7 @@ all:
go build -v go build -v
clean: clean:
rm version.txt echo "dev" > version.txt
go clean -i -cache -testcache go clean -i -cache -testcache
install: install:

View File

@ -3,7 +3,7 @@ all:
go build -v go build -v
clean: clean:
rm version.txt echo "dev" > version.txt
go clean -i -cache -testcache go clean -i -cache -testcache
install: install:

View File

@ -19,26 +19,47 @@ func convert(cfg *config.Config) *wConfig {
tasks: task.NewTasks(), tasks: task.NewTasks(),
} }
for _, s := range cfg.Service {
st := task.NewServiceTask(s.Name).SetCommand(s.Command...).SetEnv(s.Environ...)
st.SetFlagSetsid(s.Setsid).SetWorkingDir(s.WorkingDir)
st.SetUser(s.User).SetGroup(s.Group).SetStartSecs(s.StartSecs).SetPidFile(s.PidFile)
retval.tasks.AddService(st)
}
for _, s := range cfg.ServiceV0 { for _, s := range cfg.ServiceV0 {
retval.tasks.AddV0Service(s) retval.tasks.AddV0Service(s)
} }
var schedule task.CronSchedule var schedule task.CronSchedule
for _, c := range cfg.CronV0 { for _, c := range cfg.CronV0 {
schedule.Minute = convertSchedule(c.Minute) schedule = configToTaskCronSchedule(c.CronSchedule)
schedule.Hour = convertSchedule(c.Hour)
schedule.DoM = convertSchedule(c.DoM)
schedule.Month = convertSchedule(c.Month)
schedule.DoW = convertSchedule(c.DoW)
retval.tasks.AddV0Cron(schedule, c.Command) retval.tasks.AddV0Cron(schedule, c.Command)
} }
for _, c := range cfg.Cron {
schedule = configToTaskCronSchedule(c.CronSchedule)
ct := task.NewCronTask(c.Name).SetCommand(c.Command...).SetEnv(c.Environ...)
ct.SetFlagSetsid(c.Setsid).SetWorkingDir(c.WorkingDir).SetUser(c.User).SetGroup(c.Group)
ct.SetSchedule(schedule)
retval.tasks.AddCron(ct)
}
return retval return retval
} }
func convertSchedule(cfg config.CronTimeSpec) task.CronTimeSpec { func configToTaskCronSchedule(cfgSchedule config.CronSchedule) (taskSchedule task.CronSchedule) {
taskSchedule.Minute = configToTaskCronTimeSpec(cfgSchedule.Minute)
taskSchedule.Hour = configToTaskCronTimeSpec(cfgSchedule.Hour)
taskSchedule.DoM = configToTaskCronTimeSpec(cfgSchedule.DoM)
taskSchedule.Month = configToTaskCronTimeSpec(cfgSchedule.Month)
taskSchedule.DoW = configToTaskCronTimeSpec(cfgSchedule.DoW)
return
}
func configToTaskCronTimeSpec(cfg config.CronTimeSpec) task.CronTimeSpec {
switch v := cfg.(type) { switch v := cfg.(type) {
case *config.SpecAny: case *config.SpecAny:
return task.NewCronAnySpec() return task.NewCronAnySpec()

View File

@ -17,11 +17,7 @@ type CronTimeSpec interface {
} }
type Cron struct { type Cron struct {
Minute CronTimeSpec CronSchedule
Hour CronTimeSpec
DoM CronTimeSpec
Month CronTimeSpec
DoW CronTimeSpec
Command string Command string
} }

1
config/util.go Normal file
View File

@ -0,0 +1 @@
package config

View File

@ -133,13 +133,13 @@ func (c *CronTask) Command() []string {
} }
func (c *CronTask) Environ() []string { func (c *CronTask) Environ() []string {
panic("not implemented") retval := make([]string, len(c.environ))
return nil copy(retval, c.environ)
return retval
} }
func (c *CronTask) Setsid() bool { func (c *CronTask) Setsid() bool {
panic("not implemented") return c.setsid
return false
} }
func (c *CronTask) UserGroup() wminit.UserGroup { func (c *CronTask) UserGroup() wminit.UserGroup {
@ -147,11 +147,11 @@ func (c *CronTask) UserGroup() wminit.UserGroup {
} }
func (c *CronTask) WorkingDir() string { func (c *CronTask) WorkingDir() string {
panic("not implemented") return c.workingDir
return ""
} }
func (c *CronTask) Status() wminit.TaskStatus { func (c *CronTask) Status() wminit.TaskStatus {
//TODO: implement me!
panic("not implemented") panic("not implemented")
return nil return nil
} }

View File

@ -55,6 +55,7 @@ func (ts *Tasks) Crones() []wminit.CronTask {
} }
func (ts *Tasks) Get(name string) (wminit.Task, error) { func (ts *Tasks) Get(name string) (wminit.Task, error) {
//TODO: implement me!
panic("not implemented") panic("not implemented")
return nil, nil return nil, nil
} }
@ -67,6 +68,7 @@ type ServiceTask struct {
background bool background bool
workingDir string workingDir string
startSecs uint startSecs uint
pidFile string
userGroup userGroup
} }
@ -93,11 +95,6 @@ func (t *ServiceTask) SetFlagSetsid(flag bool) *ServiceTask {
return t return t
} }
func (t *ServiceTask) SetFlagBackground(flag bool) *ServiceTask {
t.background = flag
return t
}
func (t *ServiceTask) SetWorkingDir(path string) *ServiceTask { func (t *ServiceTask) SetWorkingDir(path string) *ServiceTask {
t.workingDir = path t.workingDir = path
return t return t
@ -118,6 +115,16 @@ func (t *ServiceTask) SetStartSecs(secs uint) *ServiceTask {
return t return t
} }
func (t *ServiceTask) SetPidFile(path string) *ServiceTask {
t.pidFile = path
if len(path) > 0 {
t.background = true
} else {
t.background = false
}
return t
}
func (t *ServiceTask) Name() string { func (t *ServiceTask) Name() string {
return t.name return t.name
} }
@ -129,13 +136,13 @@ func (t *ServiceTask) Command() []string {
} }
func (t *ServiceTask) Environ() []string { func (t *ServiceTask) Environ() []string {
panic("not implemented") retval := make([]string, len(t.environ))
return nil copy(retval, t.environ)
return retval
} }
func (t *ServiceTask) Setsid() bool { func (t *ServiceTask) Setsid() bool {
panic("not implemented") return t.setsid
return false
} }
func (t *ServiceTask) UserGroup() wminit.UserGroup { func (t *ServiceTask) UserGroup() wminit.UserGroup {
@ -143,38 +150,37 @@ func (t *ServiceTask) UserGroup() wminit.UserGroup {
} }
func (t *ServiceTask) Background() bool { func (t *ServiceTask) Background() bool {
panic("not implemented") return t.background
return false
} }
func (t *ServiceTask) WorkingDir() string { func (t *ServiceTask) WorkingDir() string {
panic("not implemented") return t.workingDir
return ""
} }
func (t *ServiceTask) Status() wminit.TaskStatus { func (t *ServiceTask) Status() wminit.TaskStatus {
//TODO: implement me!
panic("not implemented") panic("not implemented")
return nil return nil
} }
func (t *ServiceTask) AutoStart() bool { func (t *ServiceTask) AutoStart() bool {
//TODO: implement me!
panic("not implemented") panic("not implemented")
return false return false
} }
func (t *ServiceTask) AutoRestart() bool { func (t *ServiceTask) AutoRestart() bool {
//TODO: implement me!
panic("not implemented") panic("not implemented")
return false return false
} }
func (t *ServiceTask) StartSecs() uint { func (t *ServiceTask) StartSecs() uint {
panic("not implemented") return t.startSecs
return 0
} }
func (t *ServiceTask) PidFile() string { func (t *ServiceTask) PidFile() string {
panic("not implemented") return t.pidFile
return ""
} }
type userGroup struct { type userGroup struct {