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

fix defunct processes and deregister server before it stops #536

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 10 additions & 10 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,7 @@ import (
"context"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"

"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/pydio/cells/v4/common"
"github.com/pydio/cells/v4/common/broker"
clientcontext "github.com/pydio/cells/v4/common/client/context"
Expand All @@ -49,6 +40,13 @@ import (
servercontext "github.com/pydio/cells/v4/common/server/context"
servicecontext "github.com/pydio/cells/v4/common/service/context"
"github.com/pydio/cells/v4/common/utils/filex"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"net/url"
"os"
"path/filepath"
"strings"
)

var (
Expand Down Expand Up @@ -281,7 +279,9 @@ ENVIRONMENT
managerLogger.Info("Stopping discovery services now")
discovery.StopAll()
}

if !runtime.IsFork() {
managerLogger.Info("All servers stopped")
}
return nil
},
}
Expand Down
2 changes: 1 addition & 1 deletion common/runtime/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (m *manager) ServeAll(oo ...server.ServeOption) {

func (m *manager) StopAll() {
eg := &errgroup.Group{}
for _, srv := range m.serversWithStatus(registry.StatusReady) {
for _, srv := range m.servers {
func(sr server.Server) {
eg.Go(func() error {
if err := m.stopServer(sr, registry.WithDeregisterFull()); err != nil {
Expand Down
14 changes: 6 additions & 8 deletions common/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,17 +262,11 @@ func (s *service) Stop(oo ...registry.RegisterOption) error {
}
}

if s.Opts.serverStop != nil {
if err := s.Opts.serverStop(refCtx); err != nil {
return err
}
}

opts := &registry.RegisterOptions{}
for _, o := range oo {
o(opts)
}

// deregister before stop
if reg := s.Opts.GetRegistry(); reg != nil {
// Deregister to make sure to break existing links
if err := reg.Deregister(s, registry.WithRegisterFailFast()); err != nil {
Expand All @@ -285,7 +279,11 @@ func (s *service) Stop(oo ...registry.RegisterOption) error {
s.Opts.Logger().Warn("no registry attached")
}

s.Opts.Logger().Info("stopped")
if s.Opts.serverStop != nil {
if err := s.Opts.serverStop(refCtx); err != nil {
return err
}
}

return nil
}
Expand Down
10 changes: 9 additions & 1 deletion common/utils/fork/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ type Process struct {
lastErr error
cmd *exec.Cmd
ctx context.Context
// waitChan chan of waiting for process to complete stopping.
waitChan chan struct{}
}

func NewProcess(ctx context.Context, serviceNames []string, oo ...Option) *Process {
Expand All @@ -73,6 +75,7 @@ func NewProcess(ctx context.Context, serviceNames []string, oo ...Option) *Proce
o: &Options{
watch: func(event string, p *Process) {},
},
waitChan: make(chan struct{}),
}
for _, opt := range oo {
opt(p.o)
Expand All @@ -97,10 +100,15 @@ func (p *Process) Stop() {
p.cmd.Process.Kill()
}
}
// wait for sub processes to complete.
<-p.waitChan
}

func (p *Process) StartAndWait(retry ...int) error {

// wait for all process (including the retries) to complete.
if len(retry) < 1 || retry[0] >= p.o.retries {
defer close(p.waitChan)
}
cmd := exec.Command(os.Args[0], p.buildForkStartParams()...)
if err := p.pipeOutputs(cmd); err != nil {
p.lastErr = err
Expand Down
4 changes: 3 additions & 1 deletion gateway/data/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package gateway
import (
"context"
"fmt"
servicecontext "github.com/pydio/cells/v4/common/service/context"
"net/http"
"net/http/httputil"
"net/url"
Expand Down Expand Up @@ -77,8 +78,9 @@ func init() {
service.Context(ctx),
service.Tag(common.ServiceTagGateway),
service.Description("S3 Gateway to tree service"),
service.Fork(true),
service.WithHTTP(func(c context.Context, mux server.HttpMux) error {

c = servicecontext.WithServiceName(ctx, common.ServiceGatewayData)
console.Printf = func(format string, data ...interface{}) {
if strings.HasPrefix(format, "WARNING: ") {
format = strings.TrimPrefix(format, "WARNING: ")
Expand Down