wip: prepareCommand for service completed; next cron

This commit is contained in:
Suyono 2024-03-24 12:54:37 +00:00
parent 8f68c4ace9
commit a63646aab2
5 changed files with 81 additions and 26 deletions

View File

@ -1,11 +1,11 @@
package main package main
import ( import (
"sync"
"gitea.suyono.dev/suyono/wingmate/config" "gitea.suyono.dev/suyono/wingmate/config"
wminit "gitea.suyono.dev/suyono/wingmate/init" wminit "gitea.suyono.dev/suyono/wingmate/init"
"gitea.suyono.dev/suyono/wingmate/task" "gitea.suyono.dev/suyono/wingmate/task"
"github.com/spf13/viper"
"sync"
) )
type wConfig struct { type wConfig struct {
@ -18,21 +18,8 @@ func (c *wConfig) Tasks() wminit.Tasks {
return c.tasks return c.tasks
} }
func (c *wConfig) WMPidProxyPath() string {
c.viperMtx.Lock()
defer c.viperMtx.Unlock()
return viper.GetString(config.PidProxyPathConfig)
}
func (c *wConfig) WMExecPath() string {
c.viperMtx.Lock()
defer c.viperMtx.Unlock()
return viper.GetString(config.ExecPathConfig)
}
func (c *wConfig) Reload() error { func (c *wConfig) Reload() error {
//NOTE: for future use when reloading is possible
return nil return nil
} }
@ -47,6 +34,7 @@ func convert(cfg *config.Config) *wConfig {
st := task.NewServiceTask(s.Name).SetCommand(s.Command...).SetEnv(s.Environ...) st := task.NewServiceTask(s.Name).SetCommand(s.Command...).SetEnv(s.Environ...)
st.SetFlagSetsid(s.Setsid).SetWorkingDir(s.WorkingDir) st.SetFlagSetsid(s.Setsid).SetWorkingDir(s.WorkingDir)
st.SetUser(s.User).SetGroup(s.Group).SetStartSecs(s.StartSecs).SetPidFile(s.PidFile) st.SetUser(s.User).SetGroup(s.Group).SetStartSecs(s.StartSecs).SetPidFile(s.PidFile)
st.SetConfig(cfg)
retval.tasks.AddService(st) retval.tasks.AddService(st)
} }
@ -66,6 +54,7 @@ func convert(cfg *config.Config) *wConfig {
ct := task.NewCronTask(c.Name).SetCommand(c.Command...).SetEnv(c.Environ...) 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.SetFlagSetsid(c.Setsid).SetWorkingDir(c.WorkingDir).SetUser(c.User).SetGroup(c.Group)
ct.SetSchedule(schedule) ct.SetSchedule(schedule)
ct.SetConfig(cfg)
retval.tasks.AddCron(ct) retval.tasks.AddCron(ct)
} }

View File

@ -2,12 +2,14 @@ package config
import ( import (
"errors" "errors"
"gitea.suyono.dev/suyono/wingmate"
"github.com/spf13/viper"
"golang.org/x/sys/unix"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"gitea.suyono.dev/suyono/wingmate"
"github.com/spf13/viper"
"golang.org/x/sys/unix"
) )
const ( const (
@ -36,6 +38,7 @@ type Config struct {
CronV0 []*Cron CronV0 []*Cron
Service []ServiceTask Service []ServiceTask
Cron []CronTask Cron []CronTask
viperMtx *sync.Mutex
} }
type Task struct { type Task struct {
@ -156,3 +159,17 @@ func Read() (*Config, error) {
return outConfig, nil return outConfig, nil
} }
func (c *Config) WMPidProxyPath() string {
c.viperMtx.Lock()
defer c.viperMtx.Unlock()
return viper.GetString(PidProxyPathConfig)
}
func (c *Config) WMExecPath() string {
c.viperMtx.Lock()
defer c.viperMtx.Unlock()
return viper.GetString(ExecPathConfig)
}

View File

@ -48,8 +48,6 @@ type ServiceTask interface {
type Config interface { type Config interface {
Tasks() Tasks Tasks() Tasks
WMPidProxyPath() string
WMExecPath() string
} }
type Init struct { type Init struct {

View File

@ -1,8 +1,9 @@
package task package task
import ( import (
wminit "gitea.suyono.dev/suyono/wingmate/init"
"time" "time"
wminit "gitea.suyono.dev/suyono/wingmate/init"
) )
type CronSchedule struct { type CronSchedule struct {
@ -76,6 +77,7 @@ type CronTask struct {
workingDir string workingDir string
lastRun time.Time lastRun time.Time
hasRun bool //NOTE: make sure initialised as false hasRun bool //NOTE: make sure initialised as false
config config
} }
func NewCronTask(name string) *CronTask { func NewCronTask(name string) *CronTask {
@ -122,6 +124,11 @@ func (c *CronTask) SetSchedule(schedule CronSchedule) *CronTask {
return c return c
} }
func (c *CronTask) SetConfig(config config) *CronTask {
c.config = config
return c
}
func (c *CronTask) Name() string { func (c *CronTask) Name() string {
return c.name return c.name
} }

View File

@ -2,9 +2,15 @@ package task
import ( import (
"fmt" "fmt"
wminit "gitea.suyono.dev/suyono/wingmate/init" wminit "gitea.suyono.dev/suyono/wingmate/init"
) )
type config interface {
WMPidProxyPath() string
WMExecPath() string
}
type Tasks struct { type Tasks struct {
services []wminit.ServiceTask services []wminit.ServiceTask
crones []wminit.CronTask crones []wminit.CronTask
@ -63,12 +69,14 @@ func (ts *Tasks) Get(name string) (wminit.Task, error) {
type ServiceTask struct { type ServiceTask struct {
name string name string
command []string command []string
cmdLine []string
environ []string environ []string
setsid bool setsid bool
background bool background bool
workingDir string workingDir string
startSecs uint startSecs uint
pidFile string pidFile string
config config
userGroup userGroup
} }
@ -125,6 +133,11 @@ func (t *ServiceTask) SetPidFile(path string) *ServiceTask {
return t return t
} }
func (t *ServiceTask) SetConfig(config config) *ServiceTask {
t.config = config
return t
}
func (t *ServiceTask) Validate() error { func (t *ServiceTask) Validate() error {
// call this function for validate the field // call this function for validate the field
return validate( /* input the validators here */ ) return validate( /* input the validators here */ )
@ -134,17 +147,48 @@ func (t *ServiceTask) Name() string {
return t.name return t.name
} }
func (t *ServiceTask) prepareCommandLine() []string {
if len(t.cmdLine) > 0 {
return t.cmdLine
}
t.cmdLine = make([]string, 0)
if t.background {
t.cmdLine = append(t.cmdLine, t.config.WMPidProxyPath(), "--pid-file", t.pidFile, "--")
}
if t.setsid || t.UserGroup().IsSet() {
t.cmdLine = append(t.cmdLine, t.config.WMExecPath())
if t.setsid {
t.cmdLine = append(t.cmdLine, "--setsid")
}
if t.UserGroup().IsSet() {
t.cmdLine = append(t.cmdLine, "--user", t.UserGroup().String())
}
t.cmdLine = append(t.cmdLine, "--")
}
t.cmdLine = append(t.cmdLine, t.command...)
return t.cmdLine
}
func (t *ServiceTask) Command() string { func (t *ServiceTask) Command() string {
return t.command[0] cl := t.prepareCommandLine()
return cl[0]
} }
func (t *ServiceTask) Arguments() []string { func (t *ServiceTask) Arguments() []string {
if len(t.command) == 1 { cl := t.prepareCommandLine()
if len(cl) == 1 {
return nil return nil
} }
retval := make([]string, len(t.command)-1) retval := make([]string, len(cl)-1)
copy(retval, t.command[1:]) copy(retval, cl[1:])
return retval return retval
} }