From 21223628913845536c7f9196c7e5830592da3427 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Tue, 20 Jun 2023 16:09:17 -0600 Subject: [PATCH] Resources initialize on creation instead of explicitly --- gql.go | 89 ++++++++++++++++++++++++++--------------------------- resource.go | 5 --- 2 files changed, 43 insertions(+), 51 deletions(-) diff --git a/gql.go b/gql.go index eb60213..fe1e79e 100644 --- a/gql.go +++ b/gql.go @@ -870,6 +870,49 @@ func NewGQLServer(listen string, extended_types map[reflect.Type]*graphql.Object extended_subscriptions: extended_subscriptions, } + go func() { + log.Logf("gql", "GOROUTINE_START for %s", server.ID()) + + mux := http.NewServeMux() + http_handler, ws_handler := MakeGQLHandlers(server) + mux.HandleFunc("/gql", http_handler) + mux.HandleFunc("/gqlws", ws_handler) + mux.HandleFunc("/graphiql", GraphiQLHandler()) + fs := http.FileServer(http.Dir("./site")) + mux.Handle("/site/", http.StripPrefix("/site", fs)) + + srv := &http.Server{ + Addr: server.listen, + Handler: mux, + } + + http_done := &sync.WaitGroup{} + http_done.Add(1) + go func(srv *http.Server, http_done *sync.WaitGroup) { + defer http_done.Done() + err := srv.ListenAndServe() + if err != http.ErrServerClosed { + panic(fmt.Sprintf("Failed to start gql server: %s", err)) + } + }(srv, http_done) + + for true { + select { + case signal:=<-server.signal: + if signal.Type() == "abort" || signal.Type() == "cancel" { + err := srv.Shutdown(context.Background()) + if err != nil{ + panic(fmt.Sprintf("Failed to shutdown gql server: %s", err)) + } + http_done.Wait() + break + } + log.Logf("gql", "GOROUTINE_SIGNAL for %s: %+v", server.ID(), signal) + // Take signals to resource and send to GQL subscriptions + } + } + }() + return server } @@ -1014,49 +1057,3 @@ func GQLQueryOwner() *graphql.Field { return gql_query_owner } - -func (server * GQLServer) Init(abort chan error) bool { - go func(abort chan error) { - log.Logf("gql", "GOROUTINE_START for %s", server.ID()) - - mux := http.NewServeMux() - http_handler, ws_handler := MakeGQLHandlers(server) - mux.HandleFunc("/gql", http_handler) - mux.HandleFunc("/gqlws", ws_handler) - mux.HandleFunc("/graphiql", GraphiQLHandler()) - fs := http.FileServer(http.Dir("./site")) - mux.Handle("/site/", http.StripPrefix("/site", fs)) - - srv := &http.Server{ - Addr: server.listen, - Handler: mux, - } - - http_done := &sync.WaitGroup{} - http_done.Add(1) - go func(srv *http.Server, http_done *sync.WaitGroup) { - defer http_done.Done() - err := srv.ListenAndServe() - if err != http.ErrServerClosed { - panic(fmt.Sprintf("Failed to start gql server: %s", err)) - } - }(srv, http_done) - - for true { - select { - case <-abort: - log.Logf("gql", "GOROUTINE_ABORT for %s", server.ID()) - err := srv.Shutdown(context.Background()) - if err != nil{ - panic(fmt.Sprintf("Failed to shutdown gql server: %s", err)) - } - http_done.Wait() - break - case signal:=<-server.signal: - log.Logf("gql", "GOROUTINE_SIGNAL for %s: %+v", server.ID(), signal) - // Take signals to resource and send to GQL subscriptions - } - } - }(abort) - return true -} diff --git a/resource.go b/resource.go index 2b612c5..9191121 100644 --- a/resource.go +++ b/resource.go @@ -52,7 +52,6 @@ type Resource interface { LockState() UnlockState() - Init(abort chan error) bool lock(node GraphNode) error unlock(node GraphNode) error } @@ -193,10 +192,6 @@ func (resource * BaseResource) UnlockState() { resource.state_lock.Unlock() } -func (resource * BaseResource) Init(abort chan error) bool { - return false -} - func (resource * BaseResource) Owner() GraphNode { return resource.lock_holder }