summaryrefslogtreecommitdiff
path: root/plugins/service
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/service')
-rw-r--r--plugins/service/config.go14
-rw-r--r--plugins/service/interface.go1
-rw-r--r--plugins/service/plugin.go20
-rw-r--r--plugins/service/process.go34
4 files changed, 44 insertions, 25 deletions
diff --git a/plugins/service/config.go b/plugins/service/config.go
index b1099e06..871c8f76 100644
--- a/plugins/service/config.go
+++ b/plugins/service/config.go
@@ -4,11 +4,11 @@ import "time"
// Service represents particular service configuration
type Service struct {
- Command string `mapstructure:"command"`
- ProcessNum int `mapstructure:"process_num"`
- ExecTimeout time.Duration `mapstructure:"exec_timeout"`
- RestartAfterExit bool `mapstructure:"restart_after_exit"`
- RestartDelay time.Duration `mapstructure:"restart_delay"`
+ Command string `mapstructure:"command"`
+ ProcessNum int `mapstructure:"process_num"`
+ ExecTimeout time.Duration `mapstructure:"exec_timeout"`
+ RemainAfterExit bool `mapstructure:"remain_after_exit"`
+ RestartSec uint64 `mapstructure:"restart_sec"`
}
// Config for the services
@@ -24,9 +24,9 @@ func (c *Config) InitDefault() {
val.ProcessNum = 1
c.Services[k] = val
}
- if v.RestartDelay == 0 {
+ if v.RestartSec == 0 {
val := c.Services[k]
- val.RestartDelay = time.Minute
+ val.RestartSec = 30
c.Services[k] = val
}
}
diff --git a/plugins/service/interface.go b/plugins/service/interface.go
deleted file mode 100644
index 6d43c336..00000000
--- a/plugins/service/interface.go
+++ /dev/null
@@ -1 +0,0 @@
-package service
diff --git a/plugins/service/plugin.go b/plugins/service/plugin.go
index 60ed46c3..91e47e86 100644
--- a/plugins/service/plugin.go
+++ b/plugins/service/plugin.go
@@ -4,6 +4,7 @@ import (
"sync"
"github.com/spiral/errors"
+ "github.com/spiral/roadrunner/v2/pkg/process"
"github.com/spiral/roadrunner/v2/plugins/config"
"github.com/spiral/roadrunner/v2/plugins/logger"
)
@@ -54,9 +55,9 @@ func (service *Plugin) Serve() chan error {
for i := 0; i < service.cfg.Services[k].ProcessNum; i++ {
// create processor structure, which will process all the services
service.processes = append(service.processes, NewServiceProcess(
- service.cfg.Services[k].RestartAfterExit,
+ service.cfg.Services[k].RemainAfterExit,
service.cfg.Services[k].ExecTimeout,
- service.cfg.Services[k].RestartDelay,
+ service.cfg.Services[k].RestartSec,
service.cfg.Services[k].Command,
service.logger,
errCh,
@@ -64,6 +65,7 @@ func (service *Plugin) Serve() chan error {
}
}
+ // start all processes
for i := 0; i < len(service.processes); i++ {
service.processes[i].start()
}
@@ -72,6 +74,20 @@ func (service *Plugin) Serve() chan error {
return errCh
}
+func (service *Plugin) Workers() []process.State {
+ service.Lock()
+ defer service.Unlock()
+ states := make([]process.State, 0, len(service.processes))
+ for i := 0; i < len(service.processes); i++ {
+ st, err := process.GeneralProcessState(service.processes[i].Pid)
+ if err != nil {
+ continue
+ }
+ states = append(states, st)
+ }
+ return states
+}
+
func (service *Plugin) Stop() error {
service.Lock()
defer service.Unlock()
diff --git a/plugins/service/process.go b/plugins/service/process.go
index 06d0b4c2..49219eb0 100644
--- a/plugins/service/process.go
+++ b/plugins/service/process.go
@@ -20,15 +20,16 @@ type Process struct {
command *exec.Cmd
// rawCmd from the plugin
rawCmd string
+ Pid int
// root plugin error chan
errCh chan error
// logger
log logger.Logger
- ExecTimeout time.Duration
- RestartAfterExit bool
- RestartDelay time.Duration
+ ExecTimeout time.Duration
+ RemainAfterExit bool
+ RestartSec uint64
// process start time
startTime time.Time
@@ -36,14 +37,14 @@ type Process struct {
}
// NewServiceProcess constructs service process structure
-func NewServiceProcess(restartAfterExit bool, execTimeout, restartDelay time.Duration, command string, l logger.Logger, errCh chan error) *Process {
+func NewServiceProcess(restartAfterExit bool, execTimeout time.Duration, restartDelay uint64, command string, l logger.Logger, errCh chan error) *Process {
return &Process{
- rawCmd: command,
- RestartDelay: restartDelay,
- ExecTimeout: execTimeout,
- RestartAfterExit: restartAfterExit,
- errCh: errCh,
- log: l,
+ rawCmd: command,
+ RestartSec: restartDelay,
+ ExecTimeout: execTimeout,
+ RemainAfterExit: restartAfterExit,
+ errCh: errCh,
+ log: l,
}
}
@@ -70,10 +71,11 @@ func (p *Process) start() {
// start process waiting routine
go p.wait()
- // startExec
+ // execHandler checks for the execTimeout
go p.execHandler()
// save start time
p.startTime = time.Now()
+ p.Pid = p.command.Process.Pid
}
// create command for the process
@@ -93,12 +95,14 @@ func (p *Process) createProcess() {
// wait process for exit
func (p *Process) wait() {
// Wait error doesn't matter here
- _ = p.command.Wait()
-
+ err := p.command.Wait()
+ if err != nil {
+ p.log.Error("process wait error", "error", err)
+ }
// wait for restart delay
- if p.RestartAfterExit {
+ if p.RemainAfterExit {
// wait for the delay
- time.Sleep(p.RestartDelay)
+ time.Sleep(time.Second * time.Duration(p.RestartSec))
// and start command again
p.start()
}