From c69154b5c1e35ef038f00661e66fcb90113891bf Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 2 Apr 2016 15:39:54 +0200 Subject: improved handling of time.Ticker diff --git a/src/rhimportd/ctrlWatchDir.go b/src/rhimportd/ctrlWatchDir.go index 1b08e62..14d1c1f 100644 --- a/src/rhimportd/ctrlWatchDir.go +++ b/src/rhimportd/ctrlWatchDir.go @@ -165,6 +165,8 @@ func watchDirHandler(conf *rhimport.Config, db *rddb.DBChan, ctx *rhimport.Conte func watchDirRun(dir *os.File, conf *rhimport.Config, db *rddb.DBChan) { rhl.Printf("watch-dir-ctrl: watching for files in %s", dir.Name()) + t := time.NewTicker(1 * time.Second) + defer t.Stop() for { var err error if _, err = dir.Seek(0, 0); err != nil { @@ -199,8 +201,7 @@ func watchDirRun(dir *os.File, conf *rhimport.Config, db *rddb.DBChan) { } } } - - time.Sleep(1 * time.Second) + <-t.C } } diff --git a/src/rhimportd/uploadWeb.go b/src/rhimportd/uploadWeb.go index dc9b25c..1e3e7f0 100644 --- a/src/rhimportd/uploadWeb.go +++ b/src/rhimportd/uploadWeb.go @@ -119,7 +119,6 @@ func webUploadHandler(conf *rhimport.Config, db *rddb.DBChan, sessions *rhimport rhl.Printf("WebUploadHandler: got request from user '%s', filename='%s'", username, hdr.Filename) - // TODO: uploaded files should be deleted after some time... create directory with timestamp as part of the name? dstpath, err := ioutil.TempDir(conf.TempDir, "webupload-"+username+"-") if err != nil { rhl.Printf("WebUploadHandler: error creating temporary directory: %v", err) @@ -141,8 +140,9 @@ func webUploadHandler(conf *rhimport.Config, db *rddb.DBChan, sessions *rhimport } func webUploadCleanerRun(dir *os.File, conf *rhimport.Config) { - t := time.Tick(5 * time.Second) - for now := range t { + t := time.NewTicker(5 * time.Second) + defer t.Stop() + for now := range t.C { var err error if _, err = dir.Seek(0, 0); err != nil { rhl.Printf("webUploadCleaner: reading directory contents failed: %s", err) @@ -163,7 +163,13 @@ func webUploadCleanerRun(dir *os.File, conf *rhimport.Config) { } func webUploadCleaner(conf *rhimport.Config) { + first := true for { + if !first { + time.Sleep(5 * time.Second) + } + first = false + dir, err := os.Open(conf.TempDir) if err != nil { rhl.Printf("webUploadCleaner: %s", err) @@ -183,7 +189,5 @@ func webUploadCleaner(conf *rhimport.Config) { webUploadCleanerRun(dir, conf) rhdl.Printf("webUploadCleanerRun: returned - restring in 5 sec...") dir.Close() - - time.Sleep(5 * time.Second) } } -- cgit v0.10.2