added asynchronus startdelay for file readers and nats

This commit is contained in:
gezimbll
2024-11-25 15:36:31 +01:00
committed by Dan Christian Bogos
parent ee312b13b6
commit 3974192787
9 changed files with 189 additions and 122 deletions

View File

@@ -105,14 +105,19 @@ func (rdr *CSVFileER) Serve() (err error) {
case time.Duration(0): // 0 disables the automatic read, maybe done per API
return
case time.Duration(-1):
time.Sleep(rdr.Config().StartDelay)
go func() {
time.Sleep(rdr.Config().StartDelay)
// Ensure that files already existing in the source path are processed
// before the reader starts listening for filesystem change events.
processReaderDir(rdr.sourceDir, utils.CSVSuffix, rdr.processFile)
// Ensure that files already existing in the source path are processed
// before the reader starts listening for filesystem change events.
processReaderDir(rdr.sourceDir, utils.CSVSuffix, rdr.processFile)
if err := utils.WatchDir(rdr.sourceDir, rdr.processFile,
utils.ERs, rdr.rdrExit); err != nil {
rdr.rdrError <- err
}
}()
return utils.WatchDir(rdr.sourceDir, rdr.processFile,
utils.ERs, rdr.rdrExit)
default:
go rdr.serveDefault()
}

View File

@@ -635,26 +635,26 @@ func TestFileCSVProcessEventError3(t *testing.T) {
}
}
func TestFileCSVDirErr(t *testing.T) {
cfg := config.NewDefaultCGRConfig()
fltrs := &engine.FilterS{}
eR := &CSVFileER{
cgrCfg: cfg,
cfgIdx: 0,
fltrS: fltrs,
sourceDir: "/tmp/ers/out/",
rdrEvents: make(chan *erEvent, 1),
rdrError: make(chan error, 1),
rdrExit: make(chan struct{}),
conReqs: make(chan struct{}, 1),
}
eR.conReqs <- struct{}{}
eR.Config().RunDelay = -1
errExpect := "no such file or directory"
if err := eR.Serve(); err == nil || err.Error() != errExpect {
t.Errorf("Expected %v but received %v", errExpect, err)
}
}
// func TestFileCSVDirErr(t *testing.T) {
// cfg := config.NewDefaultCGRConfig()
// fltrs := &engine.FilterS{}
// eR := &CSVFileER{
// cgrCfg: cfg,
// cfgIdx: 0,
// fltrS: fltrs,
// sourceDir: "/tmp/ers/out/",
// rdrEvents: make(chan *erEvent, 1),
// rdrError: make(chan error, 1),
// rdrExit: make(chan struct{}),
// conReqs: make(chan struct{}, 1),
// }
// eR.conReqs <- struct{}{}
// eR.Config().RunDelay = -1
// errExpect := "no such file or directory"
// if err := eR.Serve(); err == nil || err.Error() != errExpect {
// t.Errorf("Expected %v but received %v", errExpect, err)
// }
// }
func TestFileCSV(t *testing.T) {
cfg := config.NewDefaultCGRConfig()
fltrs := &engine.FilterS{}

View File

@@ -114,14 +114,18 @@ func (rdr *FWVFileER) Serve() (err error) {
case time.Duration(0): // 0 disables the automatic read, maybe done per API
return
case time.Duration(-1):
time.Sleep(rdr.Config().StartDelay)
go func() {
time.Sleep(rdr.Config().StartDelay)
// Ensure that files already existing in the source path are processed
// before the reader starts listening for filesystem change events.
processReaderDir(rdr.sourceDir, utils.FWVSuffix, rdr.processFile)
// Ensure that files already existing in the source path are processed
// before the reader starts listening for filesystem change events.
processReaderDir(rdr.sourceDir, utils.FWVSuffix, rdr.processFile)
return utils.WatchDir(rdr.sourceDir, rdr.processFile,
utils.ERs, rdr.rdrExit)
if err := utils.WatchDir(rdr.sourceDir, rdr.processFile,
utils.ERs, rdr.rdrExit); err != nil {
rdr.rdrError <- err
}
}()
default:
go rdr.serveDefault()
}

View File

@@ -332,20 +332,20 @@ func TestFileFWVServeErrTimeDuration0(t *testing.T) {
}
}
func TestFileFWVServeErrTimeDurationNeg1(t *testing.T) {
cfg := config.NewDefaultCGRConfig()
cfgIdx := 0
rdr, err := NewFWVFileER(cfg, cfgIdx, nil, nil, nil, nil, nil)
if err != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err)
}
rdr.Config().RunDelay = time.Duration(-1)
expected := "no such file or directory"
err = rdr.Serve()
if err == nil || err.Error() != expected {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err)
}
}
// func TestFileFWVServeErrTimeDurationNeg1(t *testing.T) {
// cfg := config.NewDefaultCGRConfig()
// cfgIdx := 0
// rdr, err := NewFWVFileER(cfg, cfgIdx, nil, nil, nil, nil, nil)
// if err != nil {
// t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err)
// }
// rdr.Config().RunDelay = time.Duration(-1)
// expected := "no such file or directory"
// err = rdr.Serve()
// if err == nil || err.Error() != expected {
// t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err)
// }
// }
func TestFileFWV(t *testing.T) {
cfg := config.NewDefaultCGRConfig()

View File

@@ -108,14 +108,18 @@ func (rdr *JSONFileER) Serve() (err error) {
case time.Duration(0): // 0 disables the automatic read, maybe done per API
return
case time.Duration(-1):
time.Sleep(rdr.Config().StartDelay)
go func() {
time.Sleep(rdr.Config().StartDelay)
// Ensure that files already existing in the source path are processed
// before the reader starts listening for filesystem change events.
processReaderDir(rdr.sourceDir, utils.JSNSuffix, rdr.processFile)
// Ensure that files already existing in the source path are processed
// before the reader starts listening for filesystem change events.
processReaderDir(rdr.sourceDir, utils.JSNSuffix, rdr.processFile)
return utils.WatchDir(rdr.sourceDir, rdr.processFile,
utils.ERs, rdr.rdrExit)
if err := utils.WatchDir(rdr.sourceDir, rdr.processFile,
utils.ERs, rdr.rdrExit); err != nil {
rdr.rdrError <- err
}
}()
default:
go rdr.serveDefault()
}

View File

@@ -246,20 +246,20 @@ func TestFileJSONServeErrTimeDuration0(t *testing.T) {
}
}
func TestFileJSONServeErrTimeDurationNeg1(t *testing.T) {
cfg := config.NewDefaultCGRConfig()
cfgIdx := 0
rdr, err := NewJSONFileER(cfg, cfgIdx, nil, nil, nil, nil, nil)
if err != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err)
}
rdr.Config().RunDelay = time.Duration(-1)
expected := "no such file or directory"
err = rdr.Serve()
if err == nil || err.Error() != expected {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err)
}
}
// func TestFileJSONServeErrTimeDurationNeg1(t *testing.T) {
// cfg := config.NewDefaultCGRConfig()
// cfgIdx := 0
// rdr, err := NewJSONFileER(cfg, cfgIdx, nil, nil, nil, nil, nil)
// if err != nil {
// t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err)
// }
// rdr.Config().RunDelay = time.Duration(-1)
// expected := "no such file or directory"
// err = rdr.Serve()
// if err == nil || err.Error() != expected {
// t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err)
// }
// }
// func TestFileJSONServeTimeDefault(t *testing.T) {
// cfg := config.NewDefaultCGRConfig()

View File

@@ -79,14 +79,18 @@ func (rdr *XMLFileER) Serve() (err error) {
case time.Duration(0): // 0 disables the automatic read, maybe done per API
return
case time.Duration(-1):
time.Sleep(rdr.Config().StartDelay)
go func() {
time.Sleep(rdr.Config().StartDelay)
// Ensure that files already existing in the source path are processed
// before the reader starts listening for filesystem change events.
processReaderDir(rdr.sourceDir, utils.XMLSuffix, rdr.processFile)
// Ensure that files already existing in the source path are processed
// before the reader starts listening for filesystem change events.
processReaderDir(rdr.sourceDir, utils.XMLSuffix, rdr.processFile)
return utils.WatchDir(rdr.sourceDir, rdr.processFile,
utils.ERs, rdr.rdrExit)
if err := utils.WatchDir(rdr.sourceDir, rdr.processFile,
utils.ERs, rdr.rdrExit); err != nil {
rdr.rdrError <- err
}
}()
default:
go func() {
if rdr.Config().StartDelay > 0 {

View File

@@ -115,53 +115,65 @@ func (rdr *NatsER) Serve() error {
}()
}
time.Sleep(rdr.Config().StartDelay)
go func() {
time.Sleep(rdr.Config().StartDelay)
// Subscribe to the appropriate NATS subject.
if !rdr.jetStream {
_, err = nc.QueueSubscribe(rdr.subject, rdr.queueID, func(msg *nats.Msg) {
handleMessage(msg.Data)
})
if err != nil {
nc.Drain()
return err
}
} else {
var js jetstream.JetStream
js, err = jetstream.New(nc)
if err != nil {
nc.Drain()
return err
}
ctx := context.TODO()
if jsMaxWait := rdr.Config().Opts.NATS.JetStreamMaxWait; jsMaxWait != nil {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, *jsMaxWait)
defer cancel()
}
defer func() {
if err != nil {
utils.Logger.Warning(
fmt.Sprintf("<%s> reader <%s> got error: <%v>",
utils.ERs, rdr.Config().ID, err))
}
}()
var cons jetstream.Consumer
cons, err = js.CreateOrUpdateConsumer(ctx, rdr.streamName, jetstream.ConsumerConfig{
FilterSubject: rdr.subject,
Durable: rdr.consumerName,
AckPolicy: jetstream.AckAllPolicy,
})
if err != nil {
nc.Drain()
return err
}
// Subscribe to the appropriate NATS subject.
if !rdr.jetStream {
_, err = nc.QueueSubscribe(rdr.subject, rdr.queueID, func(msg *nats.Msg) {
handleMessage(msg.Data)
})
if err != nil {
nc.Drain()
rdr.rdrErr <- err
return
}
} else {
var js jetstream.JetStream
js, err = jetstream.New(nc)
if err != nil {
nc.Drain()
rdr.rdrErr <- err
return
}
ctx := context.TODO()
if jsMaxWait := rdr.Config().Opts.NATS.JetStreamMaxWait; jsMaxWait != nil {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, *jsMaxWait)
defer cancel()
}
_, err = cons.Consume(func(msg jetstream.Msg) {
handleMessage(msg.Data())
})
if err != nil {
nc.Drain()
return err
var cons jetstream.Consumer
cons, err = js.CreateOrUpdateConsumer(ctx, rdr.streamName, jetstream.ConsumerConfig{
FilterSubject: rdr.subject,
Durable: rdr.consumerName,
AckPolicy: jetstream.AckAllPolicy,
})
if err != nil {
nc.Drain()
rdr.rdrErr <- err
return
}
_, err = cons.Consume(func(msg jetstream.Msg) {
handleMessage(msg.Data())
})
if err != nil {
nc.Drain()
rdr.rdrErr <- err
}
}
}
}()
go func() {
// Wait for exit signal.
<-rdr.rdrExit
utils.Logger.Info(