wip: refactor for new config format
This commit is contained in:
139
task/cron.go
Normal file
139
task/cron.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
wminit "gitea.suyono.dev/suyono/wingmate/init"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CronSchedule struct {
|
||||
Minute CronTimeSpec
|
||||
Hour CronTimeSpec
|
||||
DoM CronTimeSpec
|
||||
Month CronTimeSpec
|
||||
DoW CronTimeSpec
|
||||
}
|
||||
|
||||
type CronTimeSpec interface {
|
||||
Match(uint8) bool
|
||||
}
|
||||
|
||||
type CronAnySpec struct {
|
||||
}
|
||||
|
||||
func NewCronAnySpec() *CronAnySpec {
|
||||
return &CronAnySpec{}
|
||||
}
|
||||
|
||||
func (cas *CronAnySpec) Match(u uint8) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
type CronExactSpec struct {
|
||||
value uint8
|
||||
}
|
||||
|
||||
func NewCronExactSpec(v uint8) *CronExactSpec {
|
||||
return &CronExactSpec{
|
||||
value: v,
|
||||
}
|
||||
}
|
||||
|
||||
func (ces *CronExactSpec) Match(u uint8) bool {
|
||||
return u == ces.value
|
||||
}
|
||||
|
||||
type CronMultiOccurrenceSpec struct {
|
||||
values []uint8
|
||||
}
|
||||
|
||||
func NewCronMultiOccurrenceSpec(v ...uint8) *CronMultiOccurrenceSpec {
|
||||
retval := &CronMultiOccurrenceSpec{}
|
||||
if len(v) > 0 {
|
||||
retval.values = make([]uint8, len(v))
|
||||
copy(retval.values, v)
|
||||
}
|
||||
|
||||
return retval
|
||||
}
|
||||
|
||||
func (cms *CronMultiOccurrenceSpec) Match(u uint8) bool {
|
||||
for _, v := range cms.values {
|
||||
if v == u {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
type Cron struct {
|
||||
CronSchedule
|
||||
name string
|
||||
command []string
|
||||
lastRun time.Time
|
||||
hasRun bool //NOTE: make sure initialised as false
|
||||
}
|
||||
|
||||
func (c *Cron) Name() string {
|
||||
return c.name
|
||||
}
|
||||
|
||||
func (c *Cron) Command() []string {
|
||||
retval := make([]string, len(c.command))
|
||||
copy(retval, c.command)
|
||||
return retval
|
||||
}
|
||||
|
||||
func (c *Cron) Environ() []string {
|
||||
panic("not implemented")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cron) Setsid() bool {
|
||||
panic("not implemented")
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Cron) UserGroup() wminit.UserGroup {
|
||||
panic("not implemented")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cron) Background() bool {
|
||||
panic("not implemented")
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Cron) WorkingDir() string {
|
||||
panic("not implemented")
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c *Cron) Status() wminit.TaskStatus {
|
||||
panic("not implemented")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cron) TimeToRun(now time.Time) bool {
|
||||
if c.Minute.Match(uint8(now.Minute())) &&
|
||||
c.Hour.Match(uint8(now.Hour())) &&
|
||||
c.DoM.Match(uint8(now.Day())) &&
|
||||
c.Month.Match(uint8(now.Month())) &&
|
||||
c.DoW.Match(uint8(now.Weekday())) {
|
||||
|
||||
if c.hasRun {
|
||||
if now.Sub(c.lastRun) <= time.Minute && now.Minute() == c.lastRun.Minute() {
|
||||
return false
|
||||
} else {
|
||||
c.lastRun = now
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
c.lastRun = now
|
||||
c.hasRun = true
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
99
task/task.go
Normal file
99
task/task.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
wminit "gitea.suyono.dev/suyono/wingmate/init"
|
||||
)
|
||||
|
||||
type Tasks struct {
|
||||
//services []wminit.Path
|
||||
//cron []wminit.Cron
|
||||
services []wminit.Task
|
||||
crones []wminit.CronTask
|
||||
}
|
||||
|
||||
func NewTasks() *Tasks {
|
||||
return &Tasks{
|
||||
services: make([]wminit.Task, 0),
|
||||
crones: make([]wminit.CronTask, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (ts *Tasks) AddV0Service(path string) {
|
||||
ts.services = append(ts.services, &Task{
|
||||
name: path,
|
||||
command: []string{path},
|
||||
})
|
||||
}
|
||||
|
||||
func (ts *Tasks) AddV0Cron(schedule CronSchedule, path string) {
|
||||
ts.crones = append(ts.crones, &Cron{
|
||||
CronSchedule: schedule,
|
||||
name: path,
|
||||
command: []string{path},
|
||||
})
|
||||
}
|
||||
|
||||
func (ts *Tasks) List() []wminit.Task {
|
||||
panic("not implemented")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ts *Tasks) Services() []wminit.Task {
|
||||
panic("not implemented")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ts *Tasks) Crones() []wminit.CronTask {
|
||||
panic("not implemented")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ts *Tasks) Get(name string) (wminit.Task, error) {
|
||||
panic("not implemented")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
type Task struct {
|
||||
name string
|
||||
command []string
|
||||
}
|
||||
|
||||
func (t *Task) Name() string {
|
||||
return t.name
|
||||
}
|
||||
|
||||
func (t *Task) Command() []string {
|
||||
retval := make([]string, len(t.command))
|
||||
copy(retval, t.command)
|
||||
return retval
|
||||
}
|
||||
|
||||
func (t *Task) Environ() []string {
|
||||
panic("not implemented")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Task) Setsid() bool {
|
||||
panic("not implemented")
|
||||
return false
|
||||
}
|
||||
|
||||
func (t *Task) UserGroup() wminit.UserGroup {
|
||||
panic("not implemented")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Task) Background() bool {
|
||||
panic("not implemented")
|
||||
return false
|
||||
}
|
||||
|
||||
func (t *Task) WorkingDir() string {
|
||||
panic("not implemented")
|
||||
return ""
|
||||
}
|
||||
|
||||
func (t *Task) Status() wminit.TaskStatus {
|
||||
panic("not implemented")
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user