revamp: start simple

This commit is contained in:
2023-12-06 17:01:43 +11:00
parent 4f54db3cbd
commit dbb703db61
28 changed files with 214 additions and 1381 deletions

7
init/cron.go Normal file
View File

@@ -0,0 +1,7 @@
package init
import "sync"
func (i *Init) cron(wg *sync.WaitGroup, cron Cron) {
defer wg.Done()
}

80
init/init.go Normal file
View File

@@ -0,0 +1,80 @@
package init
import (
"gitea.suyono.dev/suyono/wingmate"
"sync"
)
type Path interface {
Path() string
}
type CronExactSpec interface {
CronTimeSpec
Value() uint8
}
type CronMultipleOccurrenceSpec interface {
CronTimeSpec
MultipleValues() []uint8
}
type CronTimeSpec interface {
Type() wingmate.CronTimeType
}
type Cron interface {
Minute() CronTimeSpec
Hour() CronTimeSpec
DayOfMonth() CronTimeSpec
Month() CronTimeSpec
DayOfWeek() CronTimeSpec
Command() Path
}
type Config interface {
Services() []Path
Cron() []Cron
}
type Init struct {
config Config
}
func NewInit(config Config) *Init {
return &Init{
config: config,
}
}
func (i *Init) Start() {
var (
wg *sync.WaitGroup
signalTrigger chan any
sighandlerExit chan any
)
signalTrigger = make(chan any)
sighandlerExit = make(chan any)
wg = &sync.WaitGroup{}
wg.Add(1)
go i.sighandler(wg, signalTrigger, sighandlerExit)
for _, s := range i.config.Services() {
wg.Add(1)
go func(p Path) {
for {
if err := i.service(wg, p); err != nil {
wingmate.Log().Error().Msgf("starting service %s error %#v", p.Path(), err)
}
}
}(s)
}
for _, c := range i.config.Cron() {
wg.Add(1)
go i.cron(wg, c)
}
wg.Wait()
}

21
init/service.go Normal file
View File

@@ -0,0 +1,21 @@
package init
import (
"os/exec"
"sync"
)
func (i *Init) service(wg *sync.WaitGroup, path Path) error {
defer wg.Done()
var (
err error
)
cmd := exec.Command(path.Path())
if err = cmd.Run(); err != nil {
return err
}
return nil
}

31
init/sighandler.go Normal file
View File

@@ -0,0 +1,31 @@
package init
import (
"golang.org/x/sys/unix"
"os"
"os/signal"
"sync"
)
func (i *Init) sighandler(wg *sync.WaitGroup, trigger chan<- any, selfExit <-chan any) {
defer wg.Wait()
c := make(chan os.Signal, 1)
signal.Notify(c, unix.SIGINT, unix.SIGTERM, unix.SIGCHLD)
signal:
for {
select {
case s := <-c:
switch s {
case unix.SIGTERM, unix.SIGINT:
close(trigger)
case unix.SIGCHLD:
// do nothing
}
case <-selfExit:
break signal
}
}
}