refactor: add ctx to all services

This commit is contained in:
Derrick Hammer 2024-02-27 03:28:25 -05:00
parent 3a7bf94a08
commit ddde672b3c
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
7 changed files with 36 additions and 30 deletions

View File

@ -1,6 +1,7 @@
package node package node
import ( import (
"context"
"git.lumeweb.com/LumeWeb/libs5-go/config" "git.lumeweb.com/LumeWeb/libs5-go/config"
"git.lumeweb.com/LumeWeb/libs5-go/encoding" "git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/protocol" "git.lumeweb.com/LumeWeb/libs5-go/protocol"
@ -48,19 +49,19 @@ func (n *Node) Db() *bolt.DB {
return nil return nil
} }
func (n *Node) Start() error { func (n *Node) Start(ctx context.Context) error {
protocol.RegisterProtocols() protocol.RegisterProtocols()
protocol.RegisterSignedProtocols() protocol.RegisterSignedProtocols()
return n.services.Start() return n.services.Start(ctx)
} }
func (n *Node) Init() error { func (n *Node) Init(ctx context.Context) error {
return n.services.Init() return n.services.Init(ctx)
} }
func (n *Node) Stop() error { func (n *Node) Stop(ctx context.Context) error {
return n.services.Stop() return n.services.Stop(ctx)
} }
func (n *Node) WaitOnConnectedPeers() { func (n *Node) WaitOnConnectedPeers() {

View File

@ -1,6 +1,7 @@
package node package node
import ( import (
"context"
"git.lumeweb.com/LumeWeb/libs5-go/service" "git.lumeweb.com/LumeWeb/libs5-go/service"
) )
@ -69,9 +70,9 @@ func (s *ServicesImpl) IsStarted() bool {
return s.started return s.started
} }
func (s *ServicesImpl) Init() error { func (s *ServicesImpl) Init(ctx context.Context) error {
for _, svc := range s.All() { for _, svc := range s.All() {
err := svc.Init() err := svc.Init(ctx)
if err != nil { if err != nil {
return err return err
} }
@ -80,9 +81,9 @@ func (s *ServicesImpl) Init() error {
return nil return nil
} }
func (s *ServicesImpl) Start() error { func (s *ServicesImpl) Start(ctx context.Context) error {
for _, svc := range s.All() { for _, svc := range s.All() {
err := svc.Start() err := svc.Start(ctx)
if err != nil { if err != nil {
return err return err
} }
@ -92,9 +93,9 @@ func (s *ServicesImpl) Start() error {
return nil return nil
} }
func (s *ServicesImpl) Stop() error { func (s *ServicesImpl) Stop(ctx context.Context) error {
for _, svc := range s.All() { for _, svc := range s.All() {
err := svc.Stop() err := svc.Stop(ctx)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,6 +1,7 @@
package _default package _default
import ( import (
"context"
"git.lumeweb.com/LumeWeb/libs5-go/build" "git.lumeweb.com/LumeWeb/libs5-go/build"
"git.lumeweb.com/LumeWeb/libs5-go/net" "git.lumeweb.com/LumeWeb/libs5-go/net"
"git.lumeweb.com/LumeWeb/libs5-go/service" "git.lumeweb.com/LumeWeb/libs5-go/service"
@ -46,15 +47,15 @@ func (h *HTTPServiceDefault) GetHttpRouter(inject map[string]jape.Handler) *http
return jape.Mux(routes) return jape.Mux(routes)
} }
func (h *HTTPServiceDefault) Start() error { func (h *HTTPServiceDefault) Start(ctx context.Context) error {
return nil return nil
} }
func (h *HTTPServiceDefault) Stop() error { func (h *HTTPServiceDefault) Stop(ctx context.Context) error {
return nil return nil
} }
func (h *HTTPServiceDefault) Init() error { func (h *HTTPServiceDefault) Init(ctx context.Context) error {
return nil return nil
} }

View File

@ -85,7 +85,7 @@ func (p *P2PServiceDefault) Peers() structs.Map {
return p.peers return p.peers
} }
func (p *P2PServiceDefault) Start() error { func (p *P2PServiceDefault) Start(ctx context.Context) error {
config := p.Config() config := p.Config()
if len(config.P2P.Peers.Initial) > 0 { if len(config.P2P.Peers.Initial) > 0 {
initialPeers := config.P2P.Peers.Initial initialPeers := config.P2P.Peers.Initial
@ -109,11 +109,11 @@ func (p *P2PServiceDefault) Start() error {
return nil return nil
} }
func (p *P2PServiceDefault) Stop() error { func (p *P2PServiceDefault) Stop(ctx context.Context) error {
return nil return nil
} }
func (p *P2PServiceDefault) Init() error { func (p *P2PServiceDefault) Init(ctx context.Context) error {
if p.inited { if p.inited {
return nil return nil
} }

View File

@ -1,6 +1,7 @@
package _default package _default
import ( import (
"context"
"errors" "errors"
"git.lumeweb.com/LumeWeb/libs5-go/encoding" "git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/net" "git.lumeweb.com/LumeWeb/libs5-go/net"
@ -29,15 +30,15 @@ type RegistryServiceDefault struct {
service.ServiceBase service.ServiceBase
} }
func (r *RegistryServiceDefault) Start() error { func (r *RegistryServiceDefault) Start(ctx context.Context) error {
return nil return nil
} }
func (r *RegistryServiceDefault) Stop() error { func (r *RegistryServiceDefault) Stop(ctx context.Context) error {
return nil return nil
} }
func (r *RegistryServiceDefault) Init() error { func (r *RegistryServiceDefault) Init(ctx context.Context) error {
return utils.CreateBucket(registryBucketName, r.Db()) return utils.CreateBucket(registryBucketName, r.Db())
} }

View File

@ -1,6 +1,7 @@
package _default package _default
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"git.lumeweb.com/LumeWeb/libs5-go/encoding" "git.lumeweb.com/LumeWeb/libs5-go/encoding"
@ -40,7 +41,7 @@ func NewStorage(params service.ServiceParams) *StorageService {
} }
} }
func (s *StorageService) Start() error { func (s *StorageService) Start(ctx context.Context) error {
err := err :=
utils.CreateBucket(cacheBucketName, s.Db()) utils.CreateBucket(cacheBucketName, s.Db())
@ -51,11 +52,11 @@ func (s *StorageService) Start() error {
return nil return nil
} }
func (s *StorageService) Stop() error { func (s *StorageService) Stop(ctx context.Context) error {
return nil return nil
} }
func (s *StorageService) Init() error { func (s *StorageService) Init(ctx context.Context) error {
return nil return nil
} }

View File

@ -1,6 +1,7 @@
package service package service
import ( import (
"context"
"git.lumeweb.com/LumeWeb/libs5-go/config" "git.lumeweb.com/LumeWeb/libs5-go/config"
"go.etcd.io/bbolt" "go.etcd.io/bbolt"
"go.uber.org/zap" "go.uber.org/zap"
@ -11,9 +12,9 @@ type ServicesSetter interface {
} }
type Service interface { type Service interface {
Start() error Start(ctx context.Context) error
Stop() error Stop(ctx context.Context) error
Init() error Init(ctx context.Context) error
Logger() *zap.Logger Logger() *zap.Logger
Config() *config.NodeConfig Config() *config.NodeConfig
Db() *bbolt.DB Db() *bbolt.DB
@ -25,10 +26,10 @@ type Services interface {
HTTP() HTTPService HTTP() HTTPService
Storage() StorageService Storage() StorageService
All() []Service All() []Service
Init() error Init(ctx context.Context) error
IsStarted() bool IsStarted() bool
Start() error Start(ctx context.Context) error
Stop() error Stop(ctx context.Context) error
} }
type ServiceParams struct { type ServiceParams struct {