diff options
author | Christian Pointner <equinox@helsinki.at> | 2016-08-04 00:41:21 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2016-08-04 00:41:21 (GMT) |
commit | 8ec032e992e517f453161c92ce927bf2927cae9c (patch) | |
tree | e6d56a259aa4e0c14d470a13e48dd0a0e978e22e /src/pool-import | |
parent | b9be20bf86684a2aedc654588ef2767923b00b4c (diff) |
use cancel to abort on signal
Diffstat (limited to 'src/pool-import')
-rw-r--r-- | src/pool-import/main.go | 43 |
1 files changed, 33 insertions, 10 deletions
diff --git a/src/pool-import/main.go b/src/pool-import/main.go index 4670815..36300bf 100644 --- a/src/pool-import/main.go +++ b/src/pool-import/main.go @@ -26,7 +26,9 @@ import ( "log" "net/http" "os" + "os/signal" "sort" + "syscall" "time" "code.helsinki.at/rhrd-go/rddb" @@ -44,7 +46,7 @@ func Done(res rhimport.Result, userdata interface{}) bool { return true } -func HandleCart(cart uint, cut uint, artist, album, title, dstgroup string, sessions *rhimport.SessionStore, conf *rhimport.Config, stdlog, dbglog *log.Logger) bool { +func HandleCart(cart uint, cut uint, artist, album, title, dstgroup string, sessions *rhimport.SessionStore, conf *rhimport.Config, stdlog, dbglog *log.Logger, c <-chan os.Signal) int { filename := fmt.Sprintf("%06d_%03d.wav", cart, cut) stdlog.Printf("") @@ -64,24 +66,37 @@ func HandleCart(cart uint, cut uint, artist, album, title, dstgroup string, sess _, s, code, errstring := sessions.New(ctx, "") if code != http.StatusOK { stdlog.Printf(">>>>>>>> ERROR: creating session: %s", errstring) - return false + return 1 } donechan := make(chan rhimport.Result, 1) if err := s.AddDoneHandler((chan<- rhimport.Result)(donechan), Done); err != nil { stdlog.Printf(">>>>>>> ERROR: adding done handler: %s", err.Error()) - return false + return 1 } s.Run(10 * time.Minute) - res := <-donechan + var res rhimport.Result +Loop: + for { + select { + case res = <-donechan: + break Loop + case <-c: + s.Cancel() + } + } + if res.ResponseCode == http.StatusNoContent { + stdlog.Printf(">>>>>>> CANCELED!!") + return -1 + } if res.ResponseCode != http.StatusOK { stdlog.Printf(">>>>>>> ERROR: import failed: %s", res.ErrorString) - return false + return 1 } stdlog.Printf(">>>>>>> SUCCESS: imported into: cart/cut %d/%d", res.Cart, res.Cut) - return true + return 0 } func main() { @@ -91,7 +106,7 @@ func main() { oldGroup := os.Args[1] newGroup := os.Args[2] - conf, err := rhimport.NewConfig(NEW_RD_CONF, "http://localhost/rd-bin/rdxport.cgi", "/tmp", "snd/") + conf, err := rhimport.NewConfig(NEW_RD_CONF, "http://localhost/rd-bin/rdxport.cgi", "tmp/", "snd/") if err != nil { log.Fatal("Error parsing configuration file:", err) } @@ -103,7 +118,7 @@ func main() { defer newdb.Cleanup() stdlog := log.New(os.Stderr, "[std] ", log.LstdFlags) - // dbglog := log.New(os.Stderr, "[dbg] ", log.LstdFlags) + //dbglog := log.New(os.Stderr, "[dbg] ", log.LstdFlags) var dbglog *log.Logger sessions, err := rhimport.NewSessionStore(conf, newdb.GetInterface(), stdlog, dbglog) if err != nil { @@ -124,6 +139,9 @@ func main() { log.Fatal("Error fetching Pool cart list:", err) } + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM) + stdlog.Println("***************************************************************") stdlog.Printf("*** will import: %d carts from old:%s -> new:%s", len(carts), oldGroup, newGroup) stdlog.Println("***************************************************************") @@ -137,6 +155,7 @@ func main() { start := time.Now() successCnt := 0 errCnt := 0 +Loop: for _, k := range keys { cart := carts[uint(k)] if len(cart.Cuts) == 0 { @@ -146,9 +165,12 @@ func main() { stdlog.Printf("Warning: Cart %d has multiple cuts - will only use the first", cart.Number) } cut := cart.Cuts[0].Number - if HandleCart(cart.Number, cut, cart.Artist, cart.Album, cart.Title, newGroup, sessions.GetInterface(), conf, stdlog, dbglog) { + switch HandleCart(cart.Number, cut, cart.Artist, cart.Album, cart.Title, newGroup, sessions.GetInterface(), conf, stdlog, dbglog, c) { + case 0: successCnt++ - } else { + case -1: + break Loop + default: errCnt++ } } @@ -157,4 +179,5 @@ func main() { stdlog.Printf("*** %d files imported successfully, %d errors", successCnt, errCnt) stdlog.Printf("*** process took %v", time.Since(start)) stdlog.Println("***************************************************************") + time.Sleep(time.Second) // give session some time to cleanup } |