Skip to content

Commit

Permalink
Merge pull request #78 from jszafran/add_s3_fallback_on_app_startup
Browse files Browse the repository at this point in the history
Use S3 as fallback source upon application startup.
  • Loading branch information
jszafran committed Jul 2, 2023
2 parents cc96c70 + 6418d99 commit 0135b39
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
13 changes: 12 additions & 1 deletion backend/cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,18 @@ func initializeDataSnapshot() (eurostat.DataSnapshot, error) {
case "production":
log.Println("DEPLOY_ENV=production; reading live snapshot from Eurostat.")
snapshot, err = eurostat.DataSnapshotFromEurostat()
if err != nil {
if err != nil && os.Getenv("USE_S3_AS_FALLBACK") == "true" {
log.Printf("Reading live snapshot from Eurostat failed because of: %s\n", err)
sm, err := eurostat.NewSnapshotManager(os.Getenv("S3_BUCKET"))
if err != nil {
return snapshot, err
}

snapshot, err = sm.LatestSnapshotFromS3()
if err != nil {
return snapshot, err
}
} else {
return snapshot, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion backend/cmd/api/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type WeeklyDeathsResponse struct {
WeeklyDeaths []eurostat.WeekYearDeaths `json:"weekly_deaths"`
}

// MetadataLabel is a represenation of label data
// MetadataLabel is a representation of label data
// that is returned by /api/labels endpoint.
type MetadataLabel struct {
Value string `json:"value"`
Expand Down
1 change: 1 addition & 0 deletions backend/fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ primary_region = "cdg"
PORT = "8080"
PRIMARY_REGION = "cdg"
S3_BUCKET = "eurostat-weekly-deaths-snapshots"
USE_S3_AS_FALLBACK = "true"

[[services]]
protocol = "tcp"
Expand Down
4 changes: 3 additions & 1 deletion backend/internal/eurostat/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ func latestKey(keys []string) (string, error) {

func (sm *SnapshotManager) LatestSnapshotFromS3() (DataSnapshot, error) {
var ds DataSnapshot

log.Println("Attempting to fetch latest snapshot from S3.")
obj := make([]string, 0)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
Expand Down Expand Up @@ -169,6 +171,6 @@ func (sm *SnapshotManager) LatestSnapshotFromS3() (DataSnapshot, error) {
if err != nil {
return ds, err
}

log.Printf("Successfully fetched S3 snapshot (timestamp: %s)\n", ds.Timestamp)
return ds, nil
}

0 comments on commit 0135b39

Please sign in to comment.