Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fafba0f01b | |||
| 3c95fa97cd | |||
| dbca0548b1 |
@@ -180,9 +180,9 @@ Registry-based construction allows daemons to opt into any sink drivers.
|
|||||||
|
|
||||||
Sink Status
|
Sink Status
|
||||||
stdout 🟢 Implemented
|
stdout 🟢 Implemented
|
||||||
|
nats 🟢 Implemented
|
||||||
file 🔴 Stub
|
file 🔴 Stub
|
||||||
postgres 🔴 Stub
|
postgres 🔴 Stub
|
||||||
rabbitmq 🔴 Stub
|
|
||||||
|
|
||||||
All sinks are required to respect context cancellation.
|
All sinks are required to respect context cancellation.
|
||||||
|
|
||||||
|
|||||||
@@ -83,9 +83,9 @@ func (c *Config) Validate() error {
|
|||||||
m.Add(fieldErr(path+".driver", "is required (e.g. openmeteo_observation, rss_feed, ...)"))
|
m.Add(fieldErr(path+".driver", "is required (e.g. openmeteo_observation, rss_feed, ...)"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Every
|
// Every (optional but if present must be >=0)
|
||||||
if s.Every.Duration <= 0 {
|
if s.Every.Duration < 0 {
|
||||||
m.Add(fieldErr(path+".every", "must be a positive duration (e.g. 15m, 1m, 30s)"))
|
m.Add(fieldErr(path+".every", "is optional, but must be a positive duration (e.g. 15m, 1m, 30s) if provided"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Kind (optional but if present must be non-empty after trimming)
|
// Kind (optional but if present must be non-empty after trimming)
|
||||||
|
|||||||
13
go.mod
13
go.mod
@@ -2,4 +2,15 @@ module gitea.maximumdirect.net/ejr/feedkit
|
|||||||
|
|
||||||
go 1.22
|
go 1.22
|
||||||
|
|
||||||
require gopkg.in/yaml.v3 v3.0.1
|
require (
|
||||||
|
github.com/nats-io/nats.go v1.34.0
|
||||||
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/klauspost/compress v1.17.2 // indirect
|
||||||
|
github.com/nats-io/nkeys v0.4.7 // indirect
|
||||||
|
github.com/nats-io/nuid v1.0.1 // indirect
|
||||||
|
golang.org/x/crypto v0.18.0 // indirect
|
||||||
|
golang.org/x/sys v0.16.0 // indirect
|
||||||
|
)
|
||||||
|
|||||||
13
go.sum
13
go.sum
@@ -1,3 +1,16 @@
|
|||||||
|
github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/ixi4=
|
||||||
|
github.com/klauspost/compress v1.17.2/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
|
||||||
|
github.com/nats-io/nats.go v1.34.0 h1:fnxnPCNiwIG5w08rlMcEKTUw4AV/nKyGCOJE8TdhSPk=
|
||||||
|
github.com/nats-io/nats.go v1.34.0/go.mod h1:Ubdu4Nh9exXdSz0RVWRFBbRfrbSxOYd26oF0wkWclB8=
|
||||||
|
github.com/nats-io/nkeys v0.4.7 h1:RwNJbbIdYCoClSDNY7QVKZlyb/wfT6ugvFCiKy6vDvI=
|
||||||
|
github.com/nats-io/nkeys v0.4.7/go.mod h1:kqXRgRDPlGy7nGaEDMuYzmiJCIAAWDK0IMBtDmGD0nc=
|
||||||
|
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
|
||||||
|
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
|
||||||
|
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
|
||||||
|
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
|
||||||
|
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
|
||||||
|
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
@@ -17,15 +17,27 @@ import (
|
|||||||
// one function everywhere without type mismatch friction.
|
// one function everywhere without type mismatch friction.
|
||||||
type Logger = logging.Logf
|
type Logger = logging.Logf
|
||||||
|
|
||||||
|
// Job describes one scheduler task.
|
||||||
|
//
|
||||||
|
// A Job may be backed by either:
|
||||||
|
// - a polling source (sources.Source): uses Every + jitter and calls Poll()
|
||||||
|
// - a stream source (sources.StreamSource): ignores Every and calls Run()
|
||||||
|
//
|
||||||
|
// Jitter behavior:
|
||||||
|
// - For polling sources: Jitter is applied at startup and before each poll tick.
|
||||||
|
// - For stream sources: Jitter is applied once at startup only (optional; useful to avoid
|
||||||
|
// reconnect storms when many instances start together).
|
||||||
type Job struct {
|
type Job struct {
|
||||||
Source sources.Source
|
Source sources.Input
|
||||||
Every time.Duration
|
Every time.Duration
|
||||||
|
|
||||||
// Jitter is the maximum additional delay added before each poll.
|
// Jitter is the maximum additional delay added before each poll.
|
||||||
// Example: if Every=15m and Jitter=30s, each poll will occur at:
|
// Example: if Every=15m and Jitter=30s, each poll will occur at:
|
||||||
// tick time + random(0..30s)
|
// tick time + random(0..30s)
|
||||||
//
|
//
|
||||||
// If Jitter == 0, we compute a default jitter based on Every.
|
// If Jitter == 0 for polling sources, we compute a default jitter based on Every.
|
||||||
|
//
|
||||||
|
// For stream sources, Jitter is treated as *startup jitter only*.
|
||||||
Jitter time.Duration
|
Jitter time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,8 +47,9 @@ type Scheduler struct {
|
|||||||
Logf Logger
|
Logf Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run starts one polling goroutine per job.
|
// Run starts one goroutine per job.
|
||||||
// Each job runs on its own interval and emits 0..N events per poll.
|
// Poll jobs run on their own interval and emit 0..N events per poll.
|
||||||
|
// Stream jobs run continuously and emit events as they arrive.
|
||||||
func (s *Scheduler) Run(ctx context.Context) error {
|
func (s *Scheduler) Run(ctx context.Context) error {
|
||||||
if s.Out == nil {
|
if s.Out == nil {
|
||||||
return fmt.Errorf("scheduler.Run: Out channel is nil")
|
return fmt.Errorf("scheduler.Run: Out channel is nil")
|
||||||
@@ -59,17 +72,48 @@ func (s *Scheduler) runJob(ctx context.Context, job Job) {
|
|||||||
s.logf("scheduler: job has nil source")
|
s.logf("scheduler: job has nil source")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if job.Every <= 0 {
|
|
||||||
s.logf("scheduler: job %s has invalid interval", job.Source.Name())
|
// Stream sources: event-driven.
|
||||||
|
if ss, ok := job.Source.(sources.StreamSource); ok {
|
||||||
|
s.runStream(ctx, job, ss)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Poll sources: time-based.
|
||||||
|
ps, ok := job.Source.(sources.Source)
|
||||||
|
if !ok {
|
||||||
|
s.logf("scheduler: source %T (%s) implements neither Poll() nor Run()", job.Source, job.Source.Name())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if job.Every <= 0 {
|
||||||
|
s.logf("scheduler: polling job %q missing/invalid interval (sources[].every)", ps.Name())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.runPoller(ctx, job, ps)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Scheduler) runStream(ctx context.Context, job Job, src sources.StreamSource) {
|
||||||
|
// Optional startup jitter: helps avoid reconnect storms if many daemons start at once.
|
||||||
|
if job.Jitter > 0 {
|
||||||
|
rng := seededRNG(src.Name())
|
||||||
|
if !sleepJitter(ctx, rng, job.Jitter) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stream sources should block until ctx cancel or fatal error.
|
||||||
|
if err := src.Run(ctx, s.Out); err != nil && ctx.Err() == nil {
|
||||||
|
s.logf("scheduler: stream source %q exited with error: %v", src.Name(), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Scheduler) runPoller(ctx context.Context, job Job, src sources.Source) {
|
||||||
// Compute jitter: either configured per job, or a sensible default.
|
// Compute jitter: either configured per job, or a sensible default.
|
||||||
jitter := effectiveJitter(job.Every, job.Jitter)
|
jitter := effectiveJitter(job.Every, job.Jitter)
|
||||||
|
|
||||||
// Each worker gets its own RNG (safe + no lock contention).
|
// Each worker gets its own RNG (safe + no lock contention).
|
||||||
seed := time.Now().UnixNano() ^ int64(hashStringFNV32a(job.Source.Name()))
|
rng := seededRNG(src.Name())
|
||||||
rng := rand.New(rand.NewSource(seed))
|
|
||||||
|
|
||||||
// Optional startup jitter: avoids all jobs firing at the exact moment the daemon starts.
|
// Optional startup jitter: avoids all jobs firing at the exact moment the daemon starts.
|
||||||
if !sleepJitter(ctx, rng, jitter) {
|
if !sleepJitter(ctx, rng, jitter) {
|
||||||
@@ -77,7 +121,7 @@ func (s *Scheduler) runJob(ctx context.Context, job Job) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Immediate poll at startup (after startup jitter).
|
// Immediate poll at startup (after startup jitter).
|
||||||
s.pollOnce(ctx, job)
|
s.pollOnce(ctx, src)
|
||||||
|
|
||||||
t := time.NewTicker(job.Every)
|
t := time.NewTicker(job.Every)
|
||||||
defer t.Stop()
|
defer t.Stop()
|
||||||
@@ -89,7 +133,7 @@ func (s *Scheduler) runJob(ctx context.Context, job Job) {
|
|||||||
if !sleepJitter(ctx, rng, jitter) {
|
if !sleepJitter(ctx, rng, jitter) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.pollOnce(ctx, job)
|
s.pollOnce(ctx, src)
|
||||||
|
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
@@ -97,10 +141,10 @@ func (s *Scheduler) runJob(ctx context.Context, job Job) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scheduler) pollOnce(ctx context.Context, job Job) {
|
func (s *Scheduler) pollOnce(ctx context.Context, src sources.Source) {
|
||||||
events, err := job.Source.Poll(ctx)
|
events, err := src.Poll(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logf("scheduler: poll failed (%s): %v", job.Source.Name(), err)
|
s.logf("scheduler: poll failed (%s): %v", src.Name(), err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,6 +164,13 @@ func (s *Scheduler) logf(format string, args ...any) {
|
|||||||
s.Logf(format, args...)
|
s.Logf(format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- helpers ----
|
||||||
|
|
||||||
|
func seededRNG(name string) *rand.Rand {
|
||||||
|
seed := time.Now().UnixNano() ^ int64(hashStringFNV32a(name))
|
||||||
|
return rand.New(rand.NewSource(seed))
|
||||||
|
}
|
||||||
|
|
||||||
// effectiveJitter chooses a jitter value.
|
// effectiveJitter chooses a jitter value.
|
||||||
// - If configuredMax > 0, use it (but clamp).
|
// - If configuredMax > 0, use it (but clamp).
|
||||||
// - Else default to min(every/10, 30s).
|
// - Else default to min(every/10, 30s).
|
||||||
|
|||||||
@@ -27,9 +27,9 @@ func RegisterBuiltins(r *Registry) {
|
|||||||
return NewPostgresSinkFromConfig(cfg)
|
return NewPostgresSinkFromConfig(cfg)
|
||||||
})
|
})
|
||||||
|
|
||||||
// RabbitMQ sink: publishes events to a broker for downstream consumers.
|
// NATS sink: publishes events to a broker for downstream consumers.
|
||||||
r.Register("rabbitmq", func(cfg config.SinkConfig) (Sink, error) {
|
r.Register("nats", func(cfg config.SinkConfig) (Sink, error) {
|
||||||
return NewRabbitMQSinkFromConfig(cfg)
|
return NewNATSSinkFromConfig(cfg)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
97
sinks/nats.go
Normal file
97
sinks/nats.go
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
package sinks
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||||
|
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||||
|
"github.com/nats-io/nats.go"
|
||||||
|
)
|
||||||
|
|
||||||
|
type NATSSink struct {
|
||||||
|
name string
|
||||||
|
url string
|
||||||
|
exchange string
|
||||||
|
|
||||||
|
mu sync.Mutex
|
||||||
|
conn *nats.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNATSSinkFromConfig(cfg config.SinkConfig) (Sink, error) {
|
||||||
|
url, err := requireStringParam(cfg, "url")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ex, err := requireStringParam(cfg, "exchange")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &NATSSink{name: cfg.Name, url: url, exchange: ex}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *NATSSink) Name() string { return r.name }
|
||||||
|
|
||||||
|
func (r *NATSSink) Consume(ctx context.Context, e event.Event) error {
|
||||||
|
// Boundary validation: if something upstream violated invariants,
|
||||||
|
// surface it loudly rather than printing partial nonsense.
|
||||||
|
if err := e.Validate(); err != nil {
|
||||||
|
return fmt.Errorf("NATS sink: invalid event: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := r.connect(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("NATS sink: connect: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := json.Marshal(e)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("NATS sink: marshal event: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := conn.Publish(r.exchange, b); err != nil {
|
||||||
|
return fmt.Errorf("NATS sink: publish: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *NATSSink) connect(ctx context.Context) (*nats.Conn, error) {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
r.mu.Lock()
|
||||||
|
defer r.mu.Unlock()
|
||||||
|
|
||||||
|
if r.conn != nil && r.conn.Status() != nats.CLOSED {
|
||||||
|
return r.conn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := []nats.Option{
|
||||||
|
nats.Name(fmt.Sprintf("feedkit sink %s", r.name)),
|
||||||
|
}
|
||||||
|
if deadline, ok := ctx.Deadline(); ok {
|
||||||
|
timeout := time.Until(deadline)
|
||||||
|
if timeout <= 0 {
|
||||||
|
return nil, ctx.Err()
|
||||||
|
}
|
||||||
|
opts = append(opts, nats.Timeout(timeout))
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := nats.Connect(r.url, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
r.conn = conn
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
package sinks
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"gitea.maximumdirect.net/ejr/feedkit/config"
|
|
||||||
"gitea.maximumdirect.net/ejr/feedkit/event"
|
|
||||||
)
|
|
||||||
|
|
||||||
type RabbitMQSink struct {
|
|
||||||
name string
|
|
||||||
url string
|
|
||||||
exchange string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRabbitMQSinkFromConfig(cfg config.SinkConfig) (Sink, error) {
|
|
||||||
url, err := requireStringParam(cfg, "url")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ex, err := requireStringParam(cfg, "exchange")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &RabbitMQSink{name: cfg.Name, url: url, exchange: ex}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RabbitMQSink) Name() string { return r.name }
|
|
||||||
|
|
||||||
func (r *RabbitMQSink) Consume(ctx context.Context, e event.Event) error {
|
|
||||||
_ = ctx
|
|
||||||
|
|
||||||
// Boundary validation: if something upstream violated invariants,
|
|
||||||
// surface it loudly rather than printing partial nonsense.
|
|
||||||
if err := e.Validate(); err != nil {
|
|
||||||
return fmt.Errorf("rabbitmq sink: invalid event: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO implement RabbitMQ publishing
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -13,13 +13,18 @@ import (
|
|||||||
// domain-specific source drivers (Open-Meteo, NWS, RSS, etc.) while feedkit
|
// domain-specific source drivers (Open-Meteo, NWS, RSS, etc.) while feedkit
|
||||||
// remains domain-agnostic.
|
// remains domain-agnostic.
|
||||||
type Factory func(cfg config.SourceConfig) (Source, error)
|
type Factory func(cfg config.SourceConfig) (Source, error)
|
||||||
|
type StreamFactory func(cfg config.SourceConfig) (StreamSource, error)
|
||||||
|
|
||||||
type Registry struct {
|
type Registry struct {
|
||||||
byDriver map[string]Factory
|
byDriver map[string]Factory
|
||||||
|
byStreamDriver map[string]StreamFactory
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRegistry() *Registry {
|
func NewRegistry() *Registry {
|
||||||
return &Registry{byDriver: map[string]Factory{}}
|
return &Registry{
|
||||||
|
byDriver: map[string]Factory{},
|
||||||
|
byStreamDriver: map[string]StreamFactory{},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register associates a driver name (e.g. "openmeteo_observation") with a factory.
|
// Register associates a driver name (e.g. "openmeteo_observation") with a factory.
|
||||||
@@ -35,10 +40,27 @@ func (r *Registry) Register(driver string, f Factory) {
|
|||||||
if f == nil {
|
if f == nil {
|
||||||
panic(fmt.Sprintf("sources.Registry.Register: factory cannot be nil (driver=%q)", driver))
|
panic(fmt.Sprintf("sources.Registry.Register: factory cannot be nil (driver=%q)", driver))
|
||||||
}
|
}
|
||||||
|
if _, exists := r.byStreamDriver[driver]; exists {
|
||||||
|
panic(fmt.Sprintf("sources.Registry.Register: driver %q already registered as a stream source", driver))
|
||||||
|
}
|
||||||
r.byDriver[driver] = f
|
r.byDriver[driver] = f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RegisterStream is the StreamSource equivalent of Register.
|
||||||
|
func (r *Registry) RegisterStream(driver string, f StreamFactory) {
|
||||||
|
driver = strings.TrimSpace(driver)
|
||||||
|
if driver == "" {
|
||||||
|
panic("sources.Registry.RegisterStream: driver cannot be empty")
|
||||||
|
}
|
||||||
|
if f == nil {
|
||||||
|
panic(fmt.Sprintf("sources.Registry.RegisterStream: factory cannot be nil (driver=%q)", driver))
|
||||||
|
}
|
||||||
|
if _, exists := r.byDriver[driver]; exists {
|
||||||
|
panic(fmt.Sprintf("sources.Registry.RegisterStream: driver %q already registered as a polling source", driver))
|
||||||
|
}
|
||||||
|
r.byStreamDriver[driver] = f
|
||||||
|
}
|
||||||
|
|
||||||
// Build constructs a Source from a SourceConfig by looking up cfg.Driver.
|
// Build constructs a Source from a SourceConfig by looking up cfg.Driver.
|
||||||
func (r *Registry) Build(cfg config.SourceConfig) (Source, error) {
|
func (r *Registry) Build(cfg config.SourceConfig) (Source, error) {
|
||||||
f, ok := r.byDriver[cfg.Driver]
|
f, ok := r.byDriver[cfg.Driver]
|
||||||
@@ -47,3 +69,14 @@ func (r *Registry) Build(cfg config.SourceConfig) (Source, error) {
|
|||||||
}
|
}
|
||||||
return f(cfg)
|
return f(cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BuildInput can return either a polling Source or a StreamSource.
|
||||||
|
func (r *Registry) BuildInput(cfg config.SourceConfig) (Input, error) {
|
||||||
|
if f, ok := r.byStreamDriver[cfg.Driver]; ok {
|
||||||
|
return f(cfg)
|
||||||
|
}
|
||||||
|
if f, ok := r.byDriver[cfg.Driver]; ok {
|
||||||
|
return f(cfg)
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("unknown source driver: %q", cfg.Driver)
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +6,12 @@ import (
|
|||||||
"gitea.maximumdirect.net/ejr/feedkit/event"
|
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Input is the common surface shared by all source types.
|
||||||
|
type Input interface {
|
||||||
|
Name() string
|
||||||
|
Kind() event.Kind
|
||||||
|
}
|
||||||
|
|
||||||
// Source is a configured polling job that emits 0..N events per poll.
|
// Source is a configured polling job that emits 0..N events per poll.
|
||||||
//
|
//
|
||||||
// Source implementations live in domain modules (weatherfeeder/newsfeeder/...)
|
// Source implementations live in domain modules (weatherfeeder/newsfeeder/...)
|
||||||
@@ -28,3 +34,12 @@ type Source interface {
|
|||||||
// Implementations should honor ctx.Done() for network calls and other I/O.
|
// Implementations should honor ctx.Done() for network calls and other I/O.
|
||||||
Poll(ctx context.Context) ([]event.Event, error)
|
Poll(ctx context.Context) ([]event.Event, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StreamSource is an event-driven source (NATS/RabbitMQ/MQTT/etc).
|
||||||
|
//
|
||||||
|
// Run should block, producing events into `out` until ctx is cancelled or a fatal error occurs.
|
||||||
|
// It MUST NOT close out (the scheduler/daemon owns the bus).
|
||||||
|
type StreamSource interface {
|
||||||
|
Input
|
||||||
|
Run(ctx context.Context, out chan<- event.Event) error
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user