fix(task/cron): use the correct pointer to build structure
feat(init): included enviroment variable and working directory test(cron): wip
This commit is contained in:
parent
f2bfd6e60b
commit
3bdca8c540
@ -7,38 +7,37 @@ import (
|
||||
"os/exec"
|
||||
|
||||
"gitea.suyono.dev/suyono/wingmate"
|
||||
"gitea.suyono.dev/suyono/wingmate/cmd/cli"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
const (
|
||||
// DummyPath = "/workspaces/wingmate/cmd/experiment/dummy/dummy"
|
||||
DummyPath = "/usr/local/bin/wmdummy"
|
||||
EnvDummyPath = "DUMMY_PATH"
|
||||
EnvLog = "LOG"
|
||||
EnvLogMessage = "LOG_MESSAGE"
|
||||
EnvDefaultLogMessage = "oneshot executed"
|
||||
EnvInstanceNum = "INSTANCE_NUM"
|
||||
EnvDefaultInstances = -1
|
||||
EnvDefaultInstances = 0
|
||||
)
|
||||
|
||||
func main() {
|
||||
viper.SetEnvPrefix(wingmate.EnvPrefix)
|
||||
viper.BindEnv(EnvDummyPath)
|
||||
viper.BindEnv(EnvLog)
|
||||
viper.BindEnv(EnvLogMessage)
|
||||
viper.BindEnv(EnvInstanceNum)
|
||||
viper.SetDefault(EnvDummyPath, DummyPath)
|
||||
viper.SetDefault(EnvLogMessage, EnvDefaultLogMessage)
|
||||
viper.SetDefault(EnvInstanceNum, EnvDefaultInstances)
|
||||
|
||||
exePath := viper.GetString(EnvDummyPath)
|
||||
_, childArgs, err := cli.SplitArgs(os.Args)
|
||||
if err != nil {
|
||||
log.Printf("splitargs: %+v", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
logPath := viper.GetString(EnvLog)
|
||||
logMessage := viper.GetString(EnvLogMessage)
|
||||
log.Println("log path:", logPath)
|
||||
if logPath != "" {
|
||||
var (
|
||||
err error
|
||||
file *os.File
|
||||
)
|
||||
|
||||
@ -53,10 +52,12 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
StartInstances(exePath)
|
||||
if len(childArgs) > 0 {
|
||||
StartInstances(childArgs[0], childArgs[1:]...)
|
||||
}
|
||||
}
|
||||
|
||||
func StartInstances(exePath string) {
|
||||
func StartInstances(exePath string, args ...string) {
|
||||
num := (rand.Uint32() % 16) + 16
|
||||
|
||||
iNum := viper.GetInt(EnvInstanceNum)
|
||||
@ -70,7 +71,7 @@ func StartInstances(exePath string) {
|
||||
err error
|
||||
)
|
||||
for ctr = 0; ctr < num; ctr++ {
|
||||
cmd = exec.Command(exePath)
|
||||
cmd = exec.Command(exePath, args...)
|
||||
if err = cmd.Start(); err != nil {
|
||||
log.Printf("failed to run %s: %+v\n", exePath, err)
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ func main() {
|
||||
|
||||
t = time.NewTicker(time.Second * 5)
|
||||
for {
|
||||
cmd = exec.Command(exePath)
|
||||
cmd = exec.Command(exePath, "--", "wmdummy")
|
||||
if err = cmd.Run(); err != nil {
|
||||
log.Printf("failed to run %s: %+v\n", exePath, err)
|
||||
} else {
|
||||
|
@ -25,3 +25,35 @@ service:
|
||||
- "--pid-file"
|
||||
- "/var/run/wmbg.pid"
|
||||
pidfile: "/var/run/wmbg.pid"
|
||||
|
||||
cron:
|
||||
cron1:
|
||||
command:
|
||||
- "wmoneshot"
|
||||
- "--"
|
||||
- "sleep"
|
||||
- "5"
|
||||
schedule: "*/5 * * * *"
|
||||
environ:
|
||||
- "WINGMATE_LOG=/var/log/cron1.log"
|
||||
- "WINGMATE_LOG_MESSAGE=cron executed in minute 5,10,15,20,25,30,35,40,45,50,55"
|
||||
cron2:
|
||||
command:
|
||||
- "wmoneshot"
|
||||
- "--"
|
||||
- "sleep"
|
||||
- "5"
|
||||
schedule: "17,42 */2 * * *"
|
||||
environ:
|
||||
- "WINGMATE_LOG=/var/log/cron2.log"
|
||||
- "WINGMATE_LOG_MESSAGE=cron scheduled using 17,42 */2 * * *"
|
||||
cron3:
|
||||
command:
|
||||
- "wmoneshot"
|
||||
- "--"
|
||||
- "sleep"
|
||||
- "5"
|
||||
schedule: "7,19,23,47 22 * * *"
|
||||
environ:
|
||||
- "WINGMATE_LOG=/var/log/cron3.log"
|
||||
- "WINGMATE_LOG_MESSAGE=cron scheduled using 7,19,23,47 22 * * *"
|
||||
|
10
init/cron.go
10
init/cron.go
@ -2,6 +2,7 @@ package init
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sync"
|
||||
"time"
|
||||
@ -34,6 +35,15 @@ cron:
|
||||
goto fail
|
||||
}
|
||||
cmd = exec.Command(cron.Command(), cron.Arguments()...)
|
||||
cmd.Env = os.Environ()
|
||||
if cron.EnvLen() > 0 {
|
||||
cmd.Env = append(cmd.Env, cron.Environ()...)
|
||||
}
|
||||
|
||||
if len(cron.WorkingDir()) > 0 {
|
||||
cmd.Dir = cron.WorkingDir()
|
||||
}
|
||||
|
||||
iwg = &sync.WaitGroup{}
|
||||
|
||||
if stdout, err = cmd.StdoutPipe(); err != nil {
|
||||
|
@ -25,6 +25,7 @@ type Task interface {
|
||||
Name() string
|
||||
Command() string
|
||||
Arguments() []string
|
||||
EnvLen() int
|
||||
Environ() []string
|
||||
Setsid() bool
|
||||
UserGroup() UserGroup
|
||||
|
@ -3,6 +3,7 @@ package init
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sync"
|
||||
"time"
|
||||
@ -39,6 +40,15 @@ service:
|
||||
goto fail
|
||||
}
|
||||
cmd = exec.Command(task.Command(), task.Arguments()...)
|
||||
cmd.Env = os.Environ()
|
||||
if task.EnvLen() > 0 {
|
||||
cmd.Env = append(cmd.Env, task.Environ()...)
|
||||
}
|
||||
|
||||
if len(task.WorkingDir()) > 0 {
|
||||
cmd.Dir = task.WorkingDir()
|
||||
}
|
||||
|
||||
iwg = &sync.WaitGroup{}
|
||||
|
||||
if stdout, err = cmd.StdoutPipe(); err != nil {
|
||||
|
23
task/cron.go
23
task/cron.go
@ -4,9 +4,10 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"gitea.suyono.dev/suyono/wingmate"
|
||||
"time"
|
||||
|
||||
"gitea.suyono.dev/suyono/wingmate"
|
||||
|
||||
wminit "gitea.suyono.dev/suyono/wingmate/init"
|
||||
)
|
||||
|
||||
@ -154,14 +155,14 @@ func (c *CronTask) Equals(another *CronTask) bool {
|
||||
|
||||
cmpStruct := func(p *CronTask) ([]byte, error) {
|
||||
s := &toCompare{
|
||||
Name: c.Name(),
|
||||
Command: c.Command(),
|
||||
Arguments: c.Arguments(),
|
||||
Environ: c.Environ(),
|
||||
Setsid: c.Setsid(),
|
||||
UserGroup: c.UserGroup().String(),
|
||||
WorkingDir: c.WorkingDir(),
|
||||
Schedule: c.cronScheduleString,
|
||||
Name: p.Name(),
|
||||
Command: p.Command(),
|
||||
Arguments: p.Arguments(),
|
||||
Environ: p.Environ(),
|
||||
Setsid: p.Setsid(),
|
||||
UserGroup: p.UserGroup().String(),
|
||||
WorkingDir: p.WorkingDir(),
|
||||
Schedule: p.cronScheduleString,
|
||||
}
|
||||
|
||||
return json.Marshal(s)
|
||||
@ -238,6 +239,10 @@ func (c *CronTask) Arguments() []string {
|
||||
return retval
|
||||
}
|
||||
|
||||
func (c *CronTask) EnvLen() int {
|
||||
return len(c.environ)
|
||||
}
|
||||
|
||||
func (c *CronTask) Environ() []string {
|
||||
retval := make([]string, len(c.environ))
|
||||
copy(retval, c.environ)
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"gitea.suyono.dev/suyono/wingmate"
|
||||
|
||||
wminit "gitea.suyono.dev/suyono/wingmate/init"
|
||||
@ -293,6 +294,10 @@ func (t *ServiceTask) Arguments() []string {
|
||||
return retval
|
||||
}
|
||||
|
||||
func (t *ServiceTask) EnvLen() int {
|
||||
return len(t.environ)
|
||||
}
|
||||
|
||||
func (t *ServiceTask) Environ() []string {
|
||||
retval := make([]string, len(t.environ))
|
||||
copy(retval, t.environ)
|
||||
|
Loading…
x
Reference in New Issue
Block a user