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

Add Stop() for uber/fx hook #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add Stop() for uber/fx hook #5

wants to merge 1 commit into from

Conversation

isfk
Copy link

@isfk isfk commented Apr 8, 2024

I used emque as regular Go package in my project like this(project used uber/fx):

https://pkg.go.dev/go.uber.org/fx#StopHook

func NewBroker(lc fx.Lifecycle, log *zap.Logger, conf *configv1.Config) *Broker {
	srvOptions := []server.Option{
		server.WithAddress(":8081"),
	}

	server := httpsrv.New(srvOptions...)

	lc.Append(fx.Hook{
		OnStart: func(ctx context.Context) error {
			log.Sugar().Infof("Starting Broker server at %v", 8081)
			go func() {
				if err := server.Run(); err != nil {
					log.Sugar().Errorf("Broker server err: %v", err)
				}
			}()
			return nil
		},
		OnStop: func(ctx context.Context) error {
			log.Sugar().Infof("Stopping Broker server")
			return server.Stop()
		},
	})

	return &Broker{}
}

So i add Stop() to Server

type Server interface {
	Run() error
	Stop() error
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant