add StartService to plugis
This commit is contained in:
parent
0ec05352e9
commit
50b4fce8a3
6 changed files with 35 additions and 77 deletions
|
|
@ -2,9 +2,7 @@ package echoservice
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/nats-io/nats.go/micro"
|
||||
"github.com/telemac/plugisservice"
|
||||
nats_service "github.com/telemac/plugisservice/pkg/nats-service"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
|
@ -21,7 +19,7 @@ func NewEchoService() *EchoService {
|
|||
}
|
||||
|
||||
// ExecuteCommand sends a command
|
||||
func (svc *EchoService) ExecuteCommand(ctx context.Context, command string) error {
|
||||
func (svc *EchoService) ExecuteCommand(ctx context.Context, command string) ([]byte, error) {
|
||||
subject := "ism.homelab.service.plugis.command"
|
||||
|
||||
svc.Logger().Info("sending command",
|
||||
|
|
@ -35,7 +33,7 @@ func (svc *EchoService) ExecuteCommand(ctx context.Context, command string) erro
|
|||
"error", err,
|
||||
"command", command,
|
||||
)
|
||||
return err
|
||||
return nil, err
|
||||
} else {
|
||||
svc.Logger().Info("command executed successfully",
|
||||
"command", command,
|
||||
|
|
@ -43,7 +41,7 @@ func (svc *EchoService) ExecuteCommand(ctx context.Context, command string) erro
|
|||
)
|
||||
}
|
||||
|
||||
return nil
|
||||
return msg.Data, nil
|
||||
}
|
||||
|
||||
// Run is the main function of the service.
|
||||
|
|
@ -59,37 +57,14 @@ func (svc *EchoService) Run(ctx context.Context) error {
|
|||
|
||||
svc.ExecuteCommand(ctx, "sleep 3")
|
||||
|
||||
service, err := nats_service.NewNatsService(svc.Nats(), svc.Prefix(), micro.Config{
|
||||
Name: svc.Name(),
|
||||
Endpoint: nil,
|
||||
Version: svc.Version(),
|
||||
Description: svc.Description(),
|
||||
Metadata: svc.Metadata(),
|
||||
})
|
||||
service, err := svc.StartService(svc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
service.Stop()
|
||||
}()
|
||||
/*
|
||||
pingEndpoint := nats_service.EndpointConfig{
|
||||
Name: "ping",
|
||||
Handler: func(ctx context.Context, request micro.Request) (any, error) {
|
||||
data := request.Data()
|
||||
_ = data
|
||||
return "ping: " + string(data), err
|
||||
},
|
||||
MaxConcurrency: 10,
|
||||
RequestTimeout: 2 * time.Second,
|
||||
Metadata: map[string]string{
|
||||
"description": "ping",
|
||||
"version": "0.0.1",
|
||||
},
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
pingEndpoint.UserData = svc
|
||||
err = service.AddEndpoint(ctx, pingEndpoint)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -9,9 +9,17 @@ import (
|
|||
|
||||
var pingEndpoint = nats_service.EndpointConfig{
|
||||
Name: "ping",
|
||||
Handler: func(ctx context.Context, request micro.Request) (any, error) {
|
||||
Handler: func(ctx context.Context, request micro.Request, ec nats_service.EndpointConfig) (any, error) {
|
||||
data := request.Data()
|
||||
_ = data
|
||||
// get plugis from EndpointConfigt
|
||||
echoService, ok := ec.UserData.(*EchoService)
|
||||
if ok {
|
||||
echoService.Logger().Info("plugis ping received")
|
||||
res, err := echoService.ExecuteCommand(ctx, "hostnamectl")
|
||||
return string(res), err
|
||||
}
|
||||
return ec, nil
|
||||
return "ping: " + string(data), nil
|
||||
},
|
||||
MaxConcurrency: 10,
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ type EndpointConfig struct {
|
|||
Metadata map[string]string `json:"metadata,omitempty"`
|
||||
QueueGroup string `json:"queue_group,omitempty"`
|
||||
Subject string `json:"subject,omitempty"`
|
||||
UserData any `json:"-"`
|
||||
}
|
||||
|
||||
// setDefaults applies default values to endpoint configuration
|
||||
|
|
@ -90,7 +91,7 @@ type PlugisServiceReply struct {
|
|||
}
|
||||
|
||||
// PlugisServiceHandler is the function signature for endpoint handlers
|
||||
type PlugisServiceHandler func(ctx context.Context, request micro.Request) (any, error)
|
||||
type PlugisServiceHandler func(ctx context.Context, request micro.Request, endpoint EndpointConfig) (any, error)
|
||||
|
||||
// PlugisHandler manages concurrency and timeouts for a single endpoint
|
||||
type PlugisHandler struct {
|
||||
|
|
@ -154,7 +155,7 @@ func (ph PlugisHandler) handleWithTimeout(req micro.Request) {
|
|||
}
|
||||
}()
|
||||
|
||||
data, err := ph.plugisServiceHandler(ctx, req)
|
||||
data, err := ph.plugisServiceHandler(ctx, req, ph.config)
|
||||
resultChan <- result{data: data, err: err}
|
||||
}()
|
||||
|
||||
|
|
|
|||
|
|
@ -1,44 +0,0 @@
|
|||
package systeminfo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/telemac/goutils/net"
|
||||
"os"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
type SystemInfo struct {
|
||||
Platform string `json:"platform"` // ex: linux/amd64
|
||||
Docker bool `json:"docker"` // true if runnint in docker container
|
||||
Hostname string `json:"hostname"`
|
||||
MacAddress string `json:"mac_address"`
|
||||
}
|
||||
|
||||
func NewSystemInfo() (*SystemInfo, error) {
|
||||
var systemInfo SystemInfo
|
||||
err := systemInfo.Fill()
|
||||
return &systemInfo, err
|
||||
}
|
||||
|
||||
func (si *SystemInfo) Fill() error {
|
||||
var err error
|
||||
si.Platform = runtime.GOOS + "/" + runtime.GOARCH
|
||||
si.Docker = isRunningInDockerContainer()
|
||||
si.Hostname, err = os.Hostname()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to get hostname: %v", err)
|
||||
}
|
||||
si.MacAddress, err = net.GetMACAddress()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to get MAC address: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// isRunningInDockerContainer
|
||||
func isRunningInDockerContainer() bool {
|
||||
if _, err := os.Stat("/.dockerenv"); err == nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
16
plugis.go
16
plugis.go
|
|
@ -4,6 +4,8 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/nats-io/nats.go/micro"
|
||||
nats_service "github.com/telemac/plugisservice/pkg/nats-service"
|
||||
"iter"
|
||||
"log/slog"
|
||||
"os"
|
||||
|
|
@ -218,6 +220,7 @@ func NewServiceMetadata(prefix string, startedAt time.Time) (*ServiceMetadata, e
|
|||
// return meta
|
||||
//}
|
||||
|
||||
// Meta returns ServiceMetaData as map[string]string
|
||||
func (smd *ServiceMetadata) Meta() Metadata {
|
||||
data, err := json.Marshal(smd)
|
||||
if err != nil {
|
||||
|
|
@ -229,3 +232,16 @@ func (smd *ServiceMetadata) Meta() Metadata {
|
|||
}
|
||||
return meta
|
||||
}
|
||||
|
||||
// StartService initializes and starts a NATS service for the given PlugisServiceIntf implementation.
|
||||
// It returns the created NatsService and any error encountered during the creation process.
|
||||
func (plugis *Plugis) StartService(svc PlugisServiceIntf) (*nats_service.NatsService, error) {
|
||||
service, err := nats_service.NewNatsService(plugis.Nats(), plugis.Prefix(), micro.Config{
|
||||
Name: svc.Name(),
|
||||
Endpoint: nil,
|
||||
Version: svc.Version(),
|
||||
Description: svc.Description(),
|
||||
Metadata: svc.Metadata(),
|
||||
})
|
||||
return service, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package plugisservice
|
|||
|
||||
import (
|
||||
"context"
|
||||
nats_service "github.com/telemac/plugisservice/pkg/nats-service"
|
||||
"iter"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
|
@ -35,6 +36,7 @@ type PlugisIntf interface {
|
|||
Request(subj string, data []byte, timeout time.Duration) (*nats.Msg, error)
|
||||
RequestMany(ctx context.Context, subject string, data []byte, opts ...natsext.RequestManyOpt) (iter.Seq2[*nats.Msg, error], error)
|
||||
GetServices(ctx context.Context) ([]ServiceInfo, error)
|
||||
StartService(svc PlugisServiceIntf) (*nats_service.NatsService, error)
|
||||
}
|
||||
|
||||
// ServiceInfo is the information about a service.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue