Skip to content

Commit 3cc6b12

Browse files
committed
stop process after file size has not changed for --stop-timeout seconds
1 parent 4be8497 commit 3cc6b12

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

gatlingparser/gatlingparser.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -591,44 +591,43 @@ func processRemainingRecords(
591591
ctx context.Context,
592592
wg *sync.WaitGroup,
593593
reader *bufio.Reader,
594+
file *os.File,
594595
runMessage RunMessage,
595596
scenarios []string,
596597
records chan<- interface{},
597598
) {
598-
defer func() {
599-
close(records)
600-
wg.Done()
601-
}()
599+
defer close(records)
600+
defer wg.Done()
602601

603602
latestReadTime := time.Now()
603+
lastSize := int64(0)
604604

605605
for {
606606
select {
607607
case <-ctx.Done():
608608
l.Infoln("Parser received closing signal. Processing stopped")
609609
return
610610
default:
611+
// Check file size
612+
if stat, _ := file.Stat(); stat.Size() == lastSize {
613+
if time.Now().After(latestReadTime.Add(time.Duration(waitTime) * time.Second)) {
614+
l.Infof("File size unchanged for %d seconds. Stopping application...", waitTime)
615+
return
616+
}
617+
} else {
618+
lastSize = stat.Size()
619+
latestReadTime = time.Now()
620+
}
621+
611622
record, err := ReadNotHeaderRecord(reader, runMessage.Start, scenarios)
612623
if err != nil {
613-
if err == io.EOF {
614-
// If no new data read for more than value provided by 'stop-timeout' key then processing is stopped
615-
if time.Now().After(latestReadTime.Add(time.Duration(waitTime) * time.Second)) {
616-
l.Infof("No new entries found for %d seconds. Stopping application...", waitTime)
617-
return
618-
}
619-
time.Sleep(time.Second) // Wait if end of file
620-
continue
621-
}
622-
l.Errorf("Reading error: %v", err)
624+
time.Sleep(100 * time.Millisecond)
623625
continue
624626
}
625-
626627
records <- record
627-
latestReadTime = time.Now()
628628
}
629629
}
630630
}
631-
632631
type RecordsWriter interface {
633632
writeAll(wg *sync.WaitGroup, records <-chan interface{})
634633
}
@@ -718,9 +717,10 @@ func (w *SumRecordsWriter) writeAll(wg *sync.WaitGroup, records <-chan interface
718717
l.Debugf("msg = %d, users = %d, reqs = %d, groups = %d, errors = %d\n", rumMessages, users, reqs, groups, errors)
719718
}
720719

721-
func fileProcessorBinary(ctx context.Context, file *os.File, recordsWriter RecordsWriter) {
720+
func fileProcessorBinary(ctx context.Context, file *os.File, recordsWriter RecordsWriter) {
722721
defer func() { parserStopped <- struct{}{} }()
723722
reader := bufio.NewReader(file)
723+
724724
runMessage, scenarios, err := processLogHeader(reader)
725725
if err != nil {
726726
l.Errorf("Log file %s reading error: %v", file.Name(), err)
@@ -732,7 +732,7 @@ func fileProcessorBinary(ctx context.Context, file *os.File, recordsWriter Reco
732732
records <- *runMessage
733733

734734
wg.Add(2)
735-
go processRemainingRecords(ctx, wg, reader, *runMessage, scenarios, records)
735+
go processRemainingRecords(ctx, wg, reader, file, *runMessage, scenarios, records)
736736
go recordsWriter.writeAll(wg, records)
737737
wg.Wait()
738738
}

0 commit comments

Comments
 (0)