Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup archive file if there is a problem while uploading to s3 or writing to DB #47

Merged
merged 2 commits into from
Nov 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 50 additions & 26 deletions archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,17 @@ func CreateArchiveFile(ctx context.Context, db *sqlx.DB, archive *Archive, archi
if err != nil {
return errors.Wrapf(err, "error creating temp file: %s", file.Name())
}

defer func() {
// we only set the archive filename when we succeed
if archive.ArchiveFile == "" {
err = os.Remove(file.Name())
if err != nil {
log.WithError(err).WithField("filename", file.Name()).Error("error cleaning up archive file")
}
}
}()

hash := md5.New()
gzWriter := gzip.NewWriter(io.MultiWriter(file, hash))
writer := bufio.NewWriter(gzWriter)
Expand All @@ -679,7 +690,6 @@ func CreateArchiveFile(ctx context.Context, db *sqlx.DB, archive *Archive, archi
return errors.Wrapf(err, "error writing archive")
}

archive.ArchiveFile = file.Name()
err = writer.Flush()
if err != nil {
return errors.Wrapf(err, "error flushing archive file")
Expand All @@ -701,6 +711,7 @@ func CreateArchiveFile(ctx context.Context, db *sqlx.DB, archive *Archive, archi
return fmt.Errorf("archive too large, must be smaller than 5 gigs, build dailies if possible")
}

archive.ArchiveFile = file.Name()
archive.Size = stat.Size()
archive.RecordCount = recordCount
archive.BuildTime = int(time.Since(start) / time.Millisecond)
Expand All @@ -712,6 +723,7 @@ func CreateArchiveFile(ctx context.Context, db *sqlx.DB, archive *Archive, archi
"file_hash": archive.Hash,
"elapsed": time.Since(start),
}).Debug("completed writing archive file")

return nil
}

Expand Down Expand Up @@ -828,6 +840,10 @@ func WriteArchiveToDB(ctx context.Context, db *sqlx.DB, archive *Archive) error

// DeleteArchiveFile removes our own disk archive file
func DeleteArchiveFile(archive *Archive) error {
if archive.ArchiveFile == "" {
return nil
}

err := os.Remove(archive.ArchiveFile)

if err != nil {
Expand Down Expand Up @@ -907,14 +923,44 @@ func CreateOrgArchives(ctx context.Context, now time.Time, config *Config, db *s
return archives, nil
}

func createArchive(ctx context.Context, db *sqlx.DB, config *Config, s3Client s3iface.S3API, archive *Archive) error {
err := CreateArchiveFile(ctx, db, archive, config.TempDir)
if err != nil {
return errors.Wrap(err, "error writing archive file")
}

defer func() {
if !config.KeepFiles {
err := DeleteArchiveFile(archive)
if err != nil {
logrus.WithError(err).Error("error deleting temporary archive file")
}
}
}()

if config.UploadToS3 {
err = UploadArchive(ctx, s3Client, config.S3Bucket, archive)
if err != nil {
return errors.Wrap(err, "error writing archive to s3")
}
}

err = WriteArchiveToDB(ctx, db, archive)
if err != nil {
return errors.Wrap(err, "error writing record to db")
}

return nil
}

func createArchives(ctx context.Context, db *sqlx.DB, config *Config, s3Client s3iface.S3API, org Org, archives []*Archive) error {
log := logrus.WithFields(logrus.Fields{
"org": org.Name,
"org_id": org.ID,
})

for _, archive := range archives {
log = log.WithFields(logrus.Fields{
log = logrus.WithFields(logrus.Fields{
"start_date": archive.StartDate,
"end_date": archive.endDate(),
"period": archive.Period,
Expand All @@ -923,34 +969,12 @@ func createArchives(ctx context.Context, db *sqlx.DB, config *Config, s3Client s
log.Info("starting archive")
start := time.Now()

err := CreateArchiveFile(ctx, db, archive, config.TempDir)
err := createArchive(ctx, db, config, s3Client, archive)
if err != nil {
log.WithError(err).Error("error writing archive file")
log.WithError(err).Error("error creating archive")
continue
}

if config.UploadToS3 {
err = UploadArchive(ctx, s3Client, config.S3Bucket, archive)
if err != nil {
log.WithError(err).Error("error writing archive to s3")
continue
}
}

err = WriteArchiveToDB(ctx, db, archive)
if err != nil {
log.WithError(err).Error("error writing record to db")
continue
}

if !config.KeepFiles {
err := DeleteArchiveFile(archive)
if err != nil {
log.WithError(err).Error("error deleting temporary file")
continue
}
}

elapsed := time.Since(start)
log.WithFields(logrus.Fields{
"id": archive.ID,
Expand Down