Skip to content

Commit

Permalink
nsq_to_file: un-indent FileLogger.Close()
Browse files Browse the repository at this point in the history
  • Loading branch information
mreiferson committed Jan 3, 2019
1 parent 70416b1 commit c594894
Showing 1 changed file with 41 additions and 39 deletions.
80 changes: 41 additions & 39 deletions apps/nsq_to_file/file_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,53 +154,55 @@ func (f *FileLogger) router() {
}

func (f *FileLogger) Close() {
if f.out != nil {
f.out.Sync()
if f.gzipWriter != nil {
f.gzipWriter.Close()
if f.out == nil {
return
}

f.out.Sync()
if f.gzipWriter != nil {
f.gzipWriter.Close()
}
f.out.Close()

// Move file from work dir to output dir if necessary, taking care not
// to overwrite existing files
if f.opts.WorkDir != f.opts.OutputDir {
src := f.out.Name()
dst := makeOutputPath(src, f.opts.WorkDir, f.opts.OutputDir)

// Optimistic rename
log.Printf("INFO: moving finished file %s to %s", src, dst)
err := atomicRename(src, dst)
if err == nil {
return
} else if !os.IsExist(err) {
log.Fatalf("ERROR: unable to move file from %s to %s: %s", src, dst, err)
return
}
f.out.Close()

// Move file from work dir to output dir if necessary, taking care not
// to overwrite existing files
if f.opts.WorkDir != f.opts.OutputDir {
src := f.out.Name()
dst := makeOutputPath(src, f.opts.WorkDir, f.opts.OutputDir)
// Optimistic rename failed, so we need to generate a new
// destination file name by bumping the revision number.
_, filenameTmpl := filepath.Split(f.lastFilename)
dstDir, _ := filepath.Split(dst)
dstTmpl := filepath.Join(dstDir, filenameTmpl)

// Optimistic rename
log.Printf("INFO: moving finished file %s to %s", src, dst)
for i := f.rev + 1; ; i++ {
log.Printf("INFO: destination file already exists: %s", dst)
dst := strings.Replace(dstTmpl, "<REV>", fmt.Sprintf("-%06d", i), -1)
err := atomicRename(src, dst)
if err == nil {
return
} else if !os.IsExist(err) {
log.Fatalf("ERROR: unable to move file from %s to %s: %s", src, dst, err)
return
}

// Optimistic rename failed, so we need to generate a new
// destination file name by bumping the revision number.
_, filenameTmpl := filepath.Split(f.lastFilename)
dstDir, _ := filepath.Split(dst)
dstTmpl := filepath.Join(dstDir, filenameTmpl)

for i := f.rev + 1; ; i++ {
log.Printf("INFO: destination file already exists: %s", dst)
dst := strings.Replace(dstTmpl, "<REV>", fmt.Sprintf("-%06d", i), -1)
err := atomicRename(src, dst)
if err != nil {
if os.IsExist(err) {
continue // next rev
}
log.Fatalf("ERROR: unable to rename file from %s to %s: %s", src, dst, err)
return
if err != nil {
if os.IsExist(err) {
continue // next rev
}
log.Printf("INFO: renamed finished file %s to %s to avoid overwrite", src, dst)
break
log.Fatalf("ERROR: unable to rename file from %s to %s: %s", src, dst, err)
return
}
log.Printf("INFO: renamed finished file %s to %s to avoid overwrite", src, dst)
break
}

f.out = nil
}

f.out = nil
}

func (f *FileLogger) Write(p []byte) (n int, err error) {
Expand Down

0 comments on commit c594894

Please sign in to comment.