Skip to content

Quit event #1523

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

Open
wants to merge 13 commits into
base: master
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
21 changes: 20 additions & 1 deletion cmd/state-svc/internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,25 @@ import (
type Resolver struct {
cfg *config.Instance
cache *cache.Cache
done chan bool
}

// var _ genserver.ResolverRoot = &Resolver{} // Must implement ResolverRoot

func New(cfg *config.Instance) *Resolver {
func New(cfg *config.Instance, done chan bool) *Resolver {
return &Resolver{
cfg,
cache.New(12*time.Hour, time.Hour),
done,
}
}

// Seems gqlgen supplies this so you can separate your resolver and query resolver logic
// So far no need for this, so we're pointing back at ourselves..
func (r *Resolver) Query() genserver.QueryResolver { return r }

func (r *Resolver) Subscription() genserver.SubscriptionResolver { return r }

func (r *Resolver) Version(ctx context.Context) (*graph.Version, error) {
logging.Debug("Version resolver")
return &graph.Version{
Expand Down Expand Up @@ -129,3 +133,18 @@ func (r *Resolver) Projects(ctx context.Context) ([]*graph.Project, error) {

return projects, nil
}

func (r *Resolver) Quit(ctx context.Context) (<-chan bool, error) {
logging.Debug("Quit resolver")
done := make(chan bool)

// Each subscriber gets its own copy of a done channel. We wait on the main
// done channel and send a response once that channel is closed or a response
// is sent
go func() {
<-r.done
done <- true
}()

return done, nil
}
102 changes: 102 additions & 0 deletions cmd/state-svc/internal/server/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion cmd/state-svc/internal/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ func (s *Server) setupRouting() {
return nil
})

s.httpServer.GET("/subscriptions", func(c echo.Context) error {
s.graphServer.ServeHTTP(c.Response(), c.Request())
return nil
})

s.httpServer.GET(QuitRoute, func(c echo.Context) error {
s.shutdown()
s.quit()
return nil
})
}
31 changes: 24 additions & 7 deletions cmd/state-svc/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package server
import (
"context"
"net"
"net/http"
"strconv"
"time"

"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/lru"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/gorilla/websocket"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"

Expand All @@ -21,21 +23,23 @@ import (
)

type Server struct {
shutdown context.CancelFunc
cancel context.CancelFunc
done chan bool
graphServer *handler.Server
listener net.Listener
httpServer *echo.Echo
port int
}

func New(cfg *config.Instance, shutdown context.CancelFunc) (*Server, error) {
func New(cfg *config.Instance, cancel context.CancelFunc) (*Server, error) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, errs.Wrap(err, "Failed to listen")
}

s := &Server{shutdown: shutdown}
s.graphServer = newGraphServer(cfg)
s := &Server{cancel: cancel}
s.done = make(chan bool)
s.graphServer = newGraphServer(cfg, s.done)
s.listener = listener
s.httpServer = newHTTPServer(listener)

Expand All @@ -61,6 +65,11 @@ func (s *Server) Start() error {
return s.httpServer.Start(s.listener.Addr().String())
}

func (s *Server) quit() {
close(s.done)
s.cancel()
}

func (s *Server) Shutdown() error {
logging.Debug("shutting down server")
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
Expand All @@ -72,9 +81,17 @@ func (s *Server) Shutdown() error {
return nil
}

func newGraphServer(cfg *config.Instance) *handler.Server {
graphServer := handler.NewDefaultServer(genserver.NewExecutableSchema(genserver.Config{Resolvers: resolver.New(cfg)}))
graphServer.AddTransport(&transport.Websocket{})
func newGraphServer(cfg *config.Instance, done chan bool) *handler.Server {
graphServer := handler.NewDefaultServer(genserver.NewExecutableSchema(genserver.Config{Resolvers: resolver.New(cfg, done)}))
graphServer.AddTransport(&transport.Websocket{
Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
// For development. User proper CORS for prod
return true
},
},
KeepAlivePingInterval: 10 * time.Second,
})
graphServer.SetQueryCache(lru.New(1000))
graphServer.Use(extension.Introspection{})
graphServer.Use(extension.AutomaticPersistedQuery{
Expand Down
4 changes: 4 additions & 0 deletions cmd/state-svc/schema/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ type Query {
update(channel: String, version: String): DeferredUpdate
projects: [Project]!
}

type Subscription {
quit: Boolean!
}
33 changes: 31 additions & 2 deletions cmd/state-tray/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ func run() (rerr error) {

systray.AddSeparator()

mQuit := systray.AddMenuItem(locale.Tl("tray_exit", "Exit"), "")
quit, err := setupQuit(model)
if err != nil {
return errs.Wrap(err, "Could not setup quit channel")
}

for {
select {
Expand Down Expand Up @@ -242,13 +245,39 @@ func run() (rerr error) {
if err := execute(updlgInfo.Exec(), nil); err != nil {
return errs.New("Could not execute: %s", updlgInfo.Name())
}
case <-mQuit.ClickedCh:
case <-quit:
logging.Debug("Quit event")
model.CloseSubscriptions()
systray.Quit()
}
}
}

func setupQuit(model *model.SvcModel) (chan struct{}, error) {
mQuit := systray.AddMenuItem(locale.Tl("tray_exit", "Exit"), "")
quitSub, err := model.Quit(context.Background())
if err != nil {
return nil, errs.Wrap(err, "Could not subscribe so service quit event")
}

quit := make(chan struct{})

go func() {
for {
select {
case <-mQuit.ClickedCh:
logging.Debug("Quit clicked")
quit <- struct{}{}
case <-quitSub:
logging.Debug("Quit from subscription")
quit <- struct{}{}
}
}
}()

return quit, nil
}

func onExit() {
logging.Debug("systray.OnExit() was called.")
cfg, err := config.New()
Expand Down
2 changes: 1 addition & 1 deletion cmd/state-tray/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
)

func superviseUpdate(mdl *model.SvcModel, notice *updateNotice) func() {
var done chan struct{}
done := make(chan struct{})

go func() {
for {
Expand Down
Loading