Skip to content
Snippets Groups Projects
xbus.go 1.99 KiB
Newer Older
package cmd

import (
	"fmt"

	"orus.io/xbus/xbus-stdlib/service"
	"xbus.io/go-xbus/v4"
	"xbus.io/go-xbus/v4/api"
)

type XbusOptions struct {
	Config string `long:"config-file" ini-name:"config-file" description:"xbus client configuration file"`
}

func SetupXbusCmd[E any](program *Program[E]) *XbusCmd {
	cmd := XbusCmd{}
	c, err := program.Parser.AddCommand("xbus", "Standalone xbus client", "", &cmd)
	if err != nil {
		panic(err)
	}

	SetupXbusServeCmd(program, c)
	SetupXbusActorListCmd(program, c)

	return &cmd
}

type XbusCmd struct{}

func (p *Program[E]) xbusClientOptions() (xbus.COptions, error) {
	if p.XbusOptions.Config == "" {
		return xbus.COptions{}, fmt.Errorf("%w: missing xbus.config-file", ErrInvalidConfiguration)
	}
	coptions, err := xbus.COptionsFromFile(p.XbusOptions.Config, api.Account_ACTOR, p.Logger)
	// TODO allow the program user to make verifications on the configuration
	return coptions, err
}

func (p *Program[E]) xbusRegisterActors() error {
	for _, ff := range p.setupXbusActors {
		for _, factory := range ff(p) {
			switch f := factory.Factory.(type) {
			case service.ConsumerFactory:
				if err := service.RegisterConsumer(factory.Name, f); err != nil {
					return err
				}
			case service.ConsumerFunc:
				if err := service.RegisterConsumerFunc(factory.Name, f); err != nil {
					return err
				}
			case service.WorkerFactory:
				if err := service.RegisterWorker(factory.Name, f); err != nil {
					return err
				}
			case service.WorkerFunc:
				if err := service.RegisterWorkerFunc(factory.Name, f); err != nil {
					return err
				}
			case xbus.NewActorServiceFunc:
				xbus.RegisterActorService(factory.Name, f)
			default:
				return fmt.Errorf(
					"%s type is not a recognised as a factory: %t",
					factory.Name, factory.Factory,
				)
			p.xbusActorNames = append(p.xbusActorNames, factory.Name)
func (p *Program[E]) xbusUnregisterActors() {
	for _, n := range p.xbusActorNames {
		xbus.UnregisterActorService(n)
	}
	p.xbusActorNames = nil
}