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
import (
"sync"
"gitea.suyono.dev/suyono/wingmate/config"
wminit "gitea.suyono.dev/suyono/wingmate/init"
"gitea.suyono.dev/suyono/wingmate/task"
"github.com/spf13/viper"
"sync"
)
type wConfig struct {
@ -18,21 +18,8 @@ func (c *wConfig) Tasks() wminit.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 {
//NOTE: for future use when reloading is possible
return nil
}
@ -47,6 +34,7 @@ func convert(cfg *config.Config) *wConfig {
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)
st.SetConfig(cfg)
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.SetFlagSetsid(c.Setsid).SetWorkingDir(c.WorkingDir).SetUser(c.User).SetGroup(c.Group)
ct.SetSchedule(schedule)
ct.SetConfig(cfg)
retval.tasks.AddCron(ct)
}

View File

@ -2,12 +2,14 @@ package config
import (
"errors"
"gitea.suyono.dev/suyono/wingmate"
"github.com/spf13/viper"
"golang.org/x/sys/unix"
"os"
"path/filepath"
"strings"
"sync"
"gitea.suyono.dev/suyono/wingmate"
"github.com/spf13/viper"
"golang.org/x/sys/unix"
)
const (
@ -36,6 +38,7 @@ type Config struct {
CronV0 []*Cron
Service []ServiceTask
Cron []CronTask
viperMtx *sync.Mutex
}
type Task struct {
@ -156,3 +159,17 @@ func Read() (*Config, error) {
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 {
Tasks() Tasks
WMPidProxyPath() string
WMExecPath() string
}
type Init struct {

View File

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

View File

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