1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package output
- import (
- "fmt"
- "context"
- "go-common/app/service/ops/log-agent/event"
- "go-common/library/log"
- "github.com/BurntSushi/toml"
- )
- type configDecodeFunc = func(md toml.MetaData, primValue toml.Primitive) (c interface{}, err error)
- type Output interface {
- Run() (err error)
- Stop()
- InputChan() (chan *event.ProcessorEvent)
- }
- // Factory is used to register functions creating new Input instances.
- type Factory = func(ctx context.Context, config interface{}) (Output, error)
- var registry = make(map[string]Factory)
- var runningOutput = make(map[string]Output)
- func Register(name string, factory Factory) error {
- log.Info("Registering output factory")
- if name == "" {
- return fmt.Errorf("Error registering output: name cannot be empty")
- }
- if factory == nil {
- return fmt.Errorf("Error registering output '%v': factory cannot be empty", name)
- }
- if _, exists := registry[name]; exists {
- return fmt.Errorf("Error registering output '%v': already registered", name)
- }
- registry[name] = factory
- log.Info("Successfully registered output")
- return nil
- }
- func OutputExists(name string) bool {
- _, exists := registry[name]
- return exists
- }
- func GetFactory(name string) (Factory, error) {
- if _, exists := registry[name]; !exists {
- return nil, fmt.Errorf("Error creating output. No such output type exist: '%v'", name)
- }
- return registry[name], nil
- }
- func GetOutputChan(name string) (chan *event.ProcessorEvent, error) {
- if name == "" {
- name = "lancer-ops-log"
- }
- if _, exists := runningOutput[name]; !exists {
- return nil, fmt.Errorf("Error getting output chan. No such output chan exist: '%v'", name)
- }
- return runningOutput[name].InputChan(), nil
- }
- func OutputRunning(name string) bool {
- _, exists := runningOutput[name]
- return exists
- }
- func RegisterOutput(name string, o Output) (error) {
- if name == "" {
- return nil
- }
- if _, exists := runningOutput[name]; exists {
- return fmt.Errorf("output %s already running", name)
- }
- runningOutput[name] = o
- return nil
- }
- func ChanConnect(ctx context.Context, from <-chan *event.ProcessorEvent, to chan<- *event.ProcessorEvent) {
- go func() {
- for {
- select {
- case <-ctx.Done():
- return
- case e := <-from:
- to <- e
- }
- }
- }()
- return
- }
|