Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: implement exit on error on the on_init stage #95

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
timeout-minutes: 60
strategy:
matrix:
php: [ "8.3" ]
php: [ "8.4" ]
go: [ stable ]
os: [ "ubuntu-latest" ]
steps:
Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type InitConfig struct {
Env map[string]string `mapstructure:"env"`
// Env represents UID
User string `mapstructure:"user"`
// ExitOnError defines if the RR should exit if the command fails
ExitOnError bool `mapstructure:"exit_on_error"`
}

// RPCConfig should be in sync with rpc/config.go
Expand Down
3 changes: 3 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,7 @@ github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down Expand Up @@ -1066,6 +1067,7 @@ golang.org/x/oauth2 v0.19.0 h1:9+E/EZBCbTLNrbN35fHv/a/d/mOBatymz1zbtQrXpIg=
golang.org/x/oauth2 v0.19.0/go.mod h1:vYi7skDa1x015PmRRYZ7+s1cWyPgrPiSYRe4rnsexc8=
golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs=
golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -1130,6 +1132,7 @@ golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
12 changes: 8 additions & 4 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func newCommand(log *zap.Logger, cfg *InitConfig) *command {

func (b *command) start() error {
const op = errors.Op("server_on_init")
stopCh := make(chan struct{}, 1)
stopCh := make(chan error, 1)

cmd := b.createProcess(b.cfg.Env, b.cfg.Command)

Expand All @@ -48,22 +48,26 @@ func (b *command) start() error {
errW := cmd.Wait()
if errW != nil {
b.log.Error("process wait", zap.Error(errW))
stopCh <- errW
return
}

stopCh <- struct{}{}
stopCh <- nil
}()

select {
case <-timer.C:
err = cmd.Process.Kill()
if err != nil {
b.log.Error("process killed", zap.Error(err))
return err
}

return nil

case <-stopCh:
case err := <-stopCh:
timer.Stop()
return nil
return err
}
}

Expand Down
5 changes: 5 additions & 0 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ func (p *Plugin) Serve() chan error {
err := newCommand(p.log, p.cfg.OnInit).start()
if err != nil {
p.log.Error("on_init was finished with errors", zap.Error(err))
// if exit_on_error is set, we should return error and stop the server
if p.cfg.OnInit.ExitOnError {
errCh <- err
return errCh
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions tests/configs/.rr-on-init-error.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: '3'

server:
on_init:
command: "foooooo"
exec_timeout: 200s
exit_on_error: true

command: "php some-random-command"

logs:
mode: development
level: debug
25 changes: 25 additions & 0 deletions tests/server_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,31 @@ func TestAppPipesException(t *testing.T) {
_ = container.Stop()
}

func TestAppTCPOnInitError(t *testing.T) {
cont := endure.New(slog.LevelDebug)

// config plugin
vp := &config.Plugin{
Version: "v2024.1.0",
Path: "configs/.rr-on-init-error.yaml",
}

err := cont.Register(vp)
require.NoError(t, err)

err = cont.RegisterAll(
&logger.Plugin{},
&server.Plugin{},
)
require.NoError(t, err)

err = cont.Init()
require.NoError(t, err)

_, err = cont.Serve()
require.Error(t, err)
}

func TestAppTCPOnInit(t *testing.T) {
cont := endure.New(slog.LevelDebug)

Expand Down
Loading