Resources initialize on creation instead of explicitly

graph-rework
noah metz 2023-06-20 16:09:17 -06:00
parent b00a6b7bd4
commit 2122362891
2 changed files with 43 additions and 51 deletions

@ -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
}

@ -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
}