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

View File

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

View File

@ -1,6 +1,7 @@
package _default
import (
"context"
"git.lumeweb.com/LumeWeb/libs5-go/build"
"git.lumeweb.com/LumeWeb/libs5-go/net"
"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)
}
func (h *HTTPServiceDefault) Start() error {
func (h *HTTPServiceDefault) Start(ctx context.Context) error {
return nil
}
func (h *HTTPServiceDefault) Stop() error {
func (h *HTTPServiceDefault) Stop(ctx context.Context) error {
return nil
}
func (h *HTTPServiceDefault) Init() error {
func (h *HTTPServiceDefault) Init(ctx context.Context) error {
return nil
}

View File

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

View File

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

View File

@ -1,6 +1,7 @@
package _default
import (
"context"
"errors"
"fmt"
"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 :=
utils.CreateBucket(cacheBucketName, s.Db())
@ -51,11 +52,11 @@ func (s *StorageService) Start() error {
return nil
}
func (s *StorageService) Stop() error {
func (s *StorageService) Stop(ctx context.Context) error {
return nil
}
func (s *StorageService) Init() error {
func (s *StorageService) Init(ctx context.Context) error {
return nil
}

View File

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