@@ -24,6 +24,7 @@ import (
24
24
"github.com/pkg/errors"
25
25
26
26
"gitlab.com/postgres-ai/database-lab/v3/internal/provision/databases/postgres"
27
+ "gitlab.com/postgres-ai/database-lab/v3/internal/provision/databases/postgres/pgconfig"
27
28
"gitlab.com/postgres-ai/database-lab/v3/internal/provision/docker"
28
29
"gitlab.com/postgres-ai/database-lab/v3/internal/provision/pool"
29
30
"gitlab.com/postgres-ai/database-lab/v3/internal/provision/resources"
@@ -620,12 +621,15 @@ func (p *Provisioner) LastSessionActivity(session *resources.Session, minimumTim
620
621
ctx , cancel := context .WithCancel (p .ctx )
621
622
defer cancel ()
622
623
623
- fileSelector := pglog .NewSelector (fsm .Pool ().ClonePath (session .Port ))
624
+ clonePath := fsm .Pool ().ClonePath (session .Port )
625
+ fileSelector := pglog .NewSelector (clonePath )
624
626
625
627
if err := fileSelector .DiscoverLogDir (); err != nil {
626
628
return nil , errors .Wrap (err , "failed to init file selector" )
627
629
}
628
630
631
+ location := detectLogsTimeZone (clonePath )
632
+
629
633
fileSelector .SetMinimumTime (minimumTime )
630
634
fileSelector .FilterOldFilesInList ()
631
635
@@ -639,7 +643,7 @@ func (p *Provisioner) LastSessionActivity(session *resources.Session, minimumTim
639
643
return nil , errors .Wrap (err , "failed get CSV log filenames" )
640
644
}
641
645
642
- activity , err := p .scanCSVLogFile (ctx , filename , minimumTime )
646
+ activity , err := p .scanCSVLogFile (ctx , filename , minimumTime , location )
643
647
if err == io .EOF {
644
648
continue
645
649
}
@@ -650,12 +654,39 @@ func (p *Provisioner) LastSessionActivity(session *resources.Session, minimumTim
650
654
return nil , pglog .ErrNotFound
651
655
}
652
656
653
- const csvMessageLogFieldsLength = 14
657
+ const (
658
+ csvMessageLogFieldsLength = 14
659
+ logTZ = "log_timezone"
660
+ )
661
+
662
+ func detectLogsTimeZone (dataDir string ) * time.Location {
663
+ userCfg , err := pgconfig .ReadUserConfig (dataDir )
664
+ if err != nil {
665
+ log .Msg ("unable to read user-defined config of clone:" , err .Error ())
666
+
667
+ return time .UTC
668
+ }
669
+
670
+ if tz , ok := userCfg [logTZ ]; ok {
671
+ location , err := time .LoadLocation (tz )
672
+
673
+ if err != nil {
674
+ log .Msg (fmt .Sprintf ("unable to load location (%q) defined in config: %s" , tz , err .Error ()))
675
+
676
+ return time .UTC
677
+ }
678
+
679
+ return location
680
+ }
681
+
682
+ return time .UTC
683
+ }
654
684
655
- func (p * Provisioner ) scanCSVLogFile (ctx context.Context , filename string , availableTime time.Time ) (* time.Time , error ) {
685
+ func (p * Provisioner ) scanCSVLogFile (ctx context.Context , filename string , availableTime time.Time ,
686
+ location * time.Location ) (* time.Time , error ) {
656
687
csvFile , err := os .Open (filename )
657
688
if err != nil {
658
- return nil , errors .Wrap (err , "failed to open a CSV log file" )
689
+ return nil , errors .Wrap (err , "failed to open CSV log file" )
659
690
}
660
691
661
692
defer func () {
@@ -683,13 +714,13 @@ func (p *Provisioner) scanCSVLogFile(ctx context.Context, filename string, avail
683
714
logTime := entry [0 ]
684
715
logMessage := entry [13 ]
685
716
686
- lastActivity , err := pglog .ParsePostgresLastActivity (logTime , logMessage )
717
+ lastActivity , err := pglog .ParsePostgresLastActivity (logTime , logMessage , location )
687
718
if err != nil {
688
- return nil , errors .Wrapf (err , "failed to get the time of last activity" )
719
+ return nil , errors .Wrapf (err , "failed to determine last activity timestamp " )
689
720
}
690
721
691
722
// Filter invalid and non-recent activity.
692
- if lastActivity == nil || lastActivity .Before (availableTime ) {
723
+ if lastActivity == nil || lastActivity .In ( time . UTC ). Before (availableTime ) {
693
724
continue
694
725
}
695
726
0 commit comments