Skip to content

Commit

Permalink
chore: improve log
Browse files Browse the repository at this point in the history
  • Loading branch information
GalvinGao committed Nov 21, 2023
1 parent d3c339d commit bb76d1e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
6 changes: 6 additions & 0 deletions internal/controller/meta/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"exusiai.dev/backend-next/internal/model/cache"
"exusiai.dev/backend-next/internal/model/gamedata"
"exusiai.dev/backend-next/internal/model/types"
"exusiai.dev/backend-next/internal/pkg/flog"
"exusiai.dev/backend-next/internal/pkg/pgerr"
"exusiai.dev/backend-next/internal/repo"
"exusiai.dev/backend-next/internal/server/svr"
Expand Down Expand Up @@ -721,6 +722,11 @@ func (c *AdminController) ArchiveDropReports(ctx *fiber.Ctx) error {

err = c.DropReportArchiveService.ArchiveByDate(ctx.UserContext(), date)
if err != nil {
flog.ErrorFrom(ctx, "archive.drop_report").
Err(err).
Time("targetDay", date).
Msg("failed to archive drop report")

return err
}
return ctx.SendStatus(fiber.StatusOK)
Expand Down
36 changes: 28 additions & 8 deletions internal/pkg/archiver/archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,29 @@ func (a *Archiver) canonicalFilePath() string {
func (a *Archiver) Prepare(ctx context.Context, date time.Time) error {
a.initLogger()

a.logger.Info().Str("date", date.Format("2006-01-02")).Msg("preparing archiver")
a.logger.Info().
Str("evt.name", "archiver.prepare").
Str("date", date.Format("2006-01-02")).
Msg("preparing archiver")

a.date = date
a.writerCh = make(chan interface{}, ArchiverChanBufferSize)

if err := a.assertS3FileNonExistence(ctx); err != nil {
return errors.Wrap(err, "failed to assertFileNonExistence")
}
a.logger.Trace().Msg("asserted S3 file non-existence")
a.logger.Debug().
Str("evt.name", "archiver.prepare.assertFileNonExistence").
Str("canonicalFilePath", a.canonicalFilePath()).
Msg("asserted S3 file non-existence")

if err := a.createLocalTempDir(); err != nil {
return errors.Wrap(err, "failed to createLocalTempDir")
}
a.logger.Trace().Str("localTempDir", a.localTempDir).Msg("created local temp dir")
a.logger.Debug().
Str("evt.name", "archiver.prepare.createLocalTempDir").
Str("localTempDir", a.localTempDir).
Msg("created local temp dir")

return nil
}
Expand Down Expand Up @@ -132,12 +142,16 @@ func (a *Archiver) Collect(ctx context.Context) error {
if err := a.archiveToLocalFile(ctx); err != nil {
return errors.Wrap(err, "failed to archiveToLocalFile")
}
a.logger.Trace().Msg("archived to local file")
a.logger.Debug().
Str("evt.name", "archiver.collect.archiveToLocalFile").
Msg("archived to local file")

if err := a.uploadToS3(ctx); err != nil {
return errors.Wrap(err, "failed to uploadToS3")
}
a.logger.Trace().Msg("uploaded to S3")
a.logger.Debug().
Str("evt.name", "archiver.collect.uploadToS3").
Msg("uploaded to S3")

if err := a.Cleanup(); err != nil {
return errors.Wrap(err, "failed to Cleanup")
Expand All @@ -150,14 +164,18 @@ func (a *Archiver) archiveToLocalFile(ctx context.Context) error {
if err := a.ensureFileBaseDir(localTempFilePath); err != nil {
return errors.Wrap(err, "failed to ensureFileBaseDir")
}
a.logger.Trace().Str("localTempFilePath", localTempFilePath).Msg("ensured file base dir")
a.logger.Debug().
Str("evt.name", "archiver.collect.archiveToLocalFile.ensureFileBaseDir").
Str("localTempFilePath", localTempFilePath).Msg("ensured file base dir")

file, err := os.OpenFile(localTempFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
return errors.Wrap(err, "failed to open file")
}
defer file.Close()
a.logger.Trace().Str("localTempFilePath", localTempFilePath).Msg("opened file, ready to write gzip stream")
a.logger.Debug().
Str("evt.name", "archiver.collect.archiveToLocalFile.openFile").
Str("localTempFilePath", localTempFilePath).Msg("opened file, ready to write gzip stream")

gzipWriter := gzip.NewWriter(file)
defer gzipWriter.Close()
Expand All @@ -170,7 +188,9 @@ func (a *Archiver) archiveToLocalFile(ctx context.Context) error {
return nil
case item, ok := <-a.writerCh:
if !ok {
a.logger.Trace().Msg("writerCh closed, exiting archiveToLocalFile (closing gzipWriter and file)")
a.logger.Debug().
Str("evt.name", "archiver.collect.archiveToLocalFile.writerChClosed").
Msg("writerCh closed, exiting archiveToLocalFile (closing gzipWriter and file)")
return nil
}
if err := jsonEncoder.Encode(item); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/repo/drop_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func (s *DropReport) GetDropReports(ctx context.Context, queryCtx *model.DropRep
func (s *DropReport) GetDropReportsForArchive(ctx context.Context, cursor *model.Cursor, date time.Time, limit int) ([]*model.DropReport, model.Cursor, error) {
start := time.UnixMilli(util.GetDayStartTime(&date, "CN")) // we use CN server's day start time across all servers for archive
end := start.Add(time.Hour * 24)
results := make([]*model.DropReport, 0)
results := make([]*model.DropReport, 0, limit)
query := s.DB.NewSelect().
Model(&results).
Where("created_at >= to_timestamp(?)", start.Unix()).
Expand Down
2 changes: 2 additions & 0 deletions internal/service/drop_report_archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func (s *Archive) populateDropReportsToArchiver(ctx context.Context, date time.T
break
}
log.Info().
Str("evt.name", "archive.populate.drop_reports").
Int("page", page).
Int("cursor_start", cursor.Start).
Int("cursor_end", cursor.End).
Expand Down Expand Up @@ -166,6 +167,7 @@ func (s *Archive) populateDropReportExtrasToArchiver(ctx context.Context, idIncl
break
}
log.Info().
Str("evt.name", "archive.populate.drop_report_extras").
Int("page", page).
Int("cursor_start", cursor.Start).
Int("cursor_end", cursor.End).
Expand Down

0 comments on commit bb76d1e

Please sign in to comment.