drone/events/system.go
Johannes Batzill 7abcacdd2f feat: Add Events Framework (#120)
This Commit adds:
- stream package (provides different implementation of stream producers and consumers)
  + Redis -> will be used for any non-local deployments
  + InMemory -> a VERY BASIC implementation that is used for local execution
- events package
  + GenericReporter -> responsible for reporting events, can be used to send any type of event and payload
  + GenericReader -> responsible for reading events from a stream, can be used to register handlers for any type of event and payload
  + ReaderFactory -> responsible for launching readers for any type of consumer group&name.
- webhook package
  + The wire frame of the webhook package.
- gitrpc/events package
  + defines event Reader/Reporter for events of category git
2022-12-16 13:37:08 -08:00

67 lines
1.8 KiB
Go

// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package events
import "errors"
// System represents a single contained event system that is used
// to setup event Reporters and ReaderFactories.
type System struct {
streamConsumerFactoryFn StreamConsumerFactoryFunc
streamProducer StreamProducer
}
func NewSystem(streamConsumerFactoryFunc StreamConsumerFactoryFunc, streamProducer StreamProducer) (*System, error) {
if streamConsumerFactoryFunc == nil {
return nil, errors.New("streamConsumerFactoryFunc can't be empty")
}
if streamProducer == nil {
return nil, errors.New("streamProducer can't be empty")
}
return &System{
streamConsumerFactoryFn: streamConsumerFactoryFunc,
streamProducer: streamProducer,
}, nil
}
func NewReaderFactory[R Reader](system *System, category string, fn ReaderFactoryFunc[R]) (*ReaderFactory[R], error) {
if system == nil {
return nil, errors.New("system can't be empty")
}
if category == "" {
return nil, errors.New("category can't be empty")
}
if fn == nil {
return nil, errors.New("fn can't be empty")
}
return &ReaderFactory[R]{
// values coming from system
streamConsumerFactoryFn: system.streamConsumerFactoryFn,
// values coming from input parameters
category: category,
readerFactoryFn: fn,
}, nil
}
func NewReporter(system *System, category string) (*GenericReporter, error) {
if system == nil {
return nil, errors.New("system can't be empty")
}
if category == "" {
return nil, errors.New("category can't be empty")
}
return &GenericReporter{
// values coming from system
producer: system.streamProducer,
// values coming from input parameters
category: category,
}, nil
}