From b917755aab166ebb59ebdb5356114d373ce5cb91 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Mon, 21 Dec 2015 22:41:22 +0100
Subject: improved error handling and reporting, canceling running requests
 when telnet client leaves


diff --git a/src/helsinki.at/rhimport/fetcher.go b/src/helsinki.at/rhimport/fetcher.go
index 24635d1..1f0b364 100644
--- a/src/helsinki.at/rhimport/fetcher.go
+++ b/src/helsinki.at/rhimport/fetcher.go
@@ -116,6 +116,7 @@ func FetchFileCurl(ctx *ImportContext, res *FetchResult, uri *url.URL) (err erro
 		easy.Setopt(curl.OPT_NOPROGRESS, false)
 		easy.Setopt(curl.OPT_PROGRESSFUNCTION, func(dltotal, dlnow, ultotal, ulnow float64, userdata interface{}) bool {
 			if ctx.Cancel != nil && len(ctx.Cancel) > 0 {
+				rhl.Printf("downloading '%s' got canceled", ctx.SourceUri)
 				res.ResponseCode = http.StatusNoContent
 				res.ErrorString = "canceled"
 				return false
@@ -129,6 +130,11 @@ func FetchFileCurl(ctx *ImportContext, res *FetchResult, uri *url.URL) (err erro
 		easy.Setopt(curl.OPT_PROGRESSDATA, ctx)
 
 		if err = easy.Perform(); err != nil {
+			if cbdata.File != nil {
+				rhdl.Printf("Removing stale file: %s", cbdata.filename)
+				os.Remove(cbdata.filename)
+				os.Remove(path.Dir(cbdata.filename))
+			}
 			if res.ResponseCode == http.StatusNoContent {
 				err = nil
 			} else {
@@ -176,6 +182,7 @@ func FetchFileFake(ctx *ImportContext, res *FetchResult, uri *url.URL) error {
 	} else {
 		for i := uint(0); i < uint(duration); i++ {
 			if ctx.Cancel != nil && len(ctx.Cancel) > 0 {
+				rhl.Printf("faking got canceled")
 				res.ResponseCode = http.StatusNoContent
 				res.ErrorString = "canceled"
 				return nil
diff --git a/src/helsinki.at/rhimport/importer.go b/src/helsinki.at/rhimport/importer.go
index 512daf6..1bfdf0c 100644
--- a/src/helsinki.at/rhimport/importer.go
+++ b/src/helsinki.at/rhimport/importer.go
@@ -516,6 +516,7 @@ func import_audio(ctx *ImportContext, res *ImportResult) (err error) {
 
 		if err = easy.Perform(); err != nil {
 			if res.ResponseCode == http.StatusNoContent {
+				rhl.Printf("import to cart/cat %d/%d got canceled", ctx.Cart, ctx.Cut)
 				res.Cart = ctx.Cart
 				res.Cut = ctx.Cut
 				err = nil
@@ -626,7 +627,7 @@ func cleanup_files(ctx *ImportContext) {
 func ImportFile(ctx *ImportContext) (res *ImportResult, err error) {
 	defer cleanup_files(ctx)
 
-	rhdl.Printf("importer: ImportFile called with: show-id: %d, pool-name: '%s', cart/cut: %d/%d", ctx.ShowId, ctx.GroupName, ctx.Cart, ctx.Cut)
+	rhl.Printf("importer: ImportFile called with: show-id: %d, pool-name: '%s', cart/cut: %d/%d", ctx.ShowId, ctx.GroupName, ctx.Cart, ctx.Cut)
 
 	if ctx.ProgressCallBack != nil {
 		ctx.ProgressCallBack(2, "importing", 0.0, ctx.ProgressCallBackData)
@@ -696,6 +697,7 @@ func ImportFile(ctx *ImportContext) (res *ImportResult, err error) {
 			return
 		}
 		if res.ResponseCode != http.StatusOK {
+			rhl.Printf("Fileimport has failed (Cart/Cut %d/%d): %s", res.Cart, res.Cut, res.ErrorString)
 			rmres := ImportResult{ResponseCode: http.StatusOK}
 			if rmCartOnErr {
 				if err = remove_cart(ctx, &rmres); err != nil {
@@ -706,10 +708,12 @@ func ImportFile(ctx *ImportContext) (res *ImportResult, err error) {
 					return
 				}
 			}
+		} else {
+			rhl.Printf("File got succesfully imported into Cart/Cut %d/%d", res.Cart, res.Cut)
 		}
 	} else {
 		res.ResponseCode = http.StatusBadRequest
-		res.ErrorString = "The request doesn't contain enough information to be processed"
+		res.ErrorString = "importer: The request doesn't contain enough information to be processed"
 	}
 
 	return
diff --git a/src/helsinki.at/rhimportd/ctrlTelnet.go b/src/helsinki.at/rhimportd/ctrlTelnet.go
index 41ac7f2..b47d2a1 100644
--- a/src/helsinki.at/rhimportd/ctrlTelnet.go
+++ b/src/helsinki.at/rhimportd/ctrlTelnet.go
@@ -283,15 +283,13 @@ func telnet_cmd_run(ctx rhimport.ImportContext, out chan<- string) {
 
 	out <- fmt.Sprintf("\nimporting file '%s'\n", ctx.SourceFile)
 	if res, err := rhimport.ImportFile(&ctx); err != nil {
-		out <- fmt.Sprintf("import file error: %s\n", err)
+		out <- fmt.Sprintf("\nimport file error: %s\n", err)
 		return
 	} else {
 		if res.ResponseCode == http.StatusOK {
 			out <- fmt.Sprintf("\nFile got succesfully imported into Cart/Cut %d/%d\n", res.Cart, res.Cut)
-			rhl.Printf("File got succesfully imported into Cart/Cut %d/%d", res.Cart, res.Cut)
 		} else {
-			out <- fmt.Sprintf("Fileimport has failed (Cart/Cut %d/%d): %s\n", res.Cart, res.Cut, res.ErrorString)
-			rhl.Printf("Fileimport has failed (Cart/Cut %d/%d): %s", res.Cart, res.Cut, res.ErrorString)
+			out <- fmt.Sprintf("\nFileimport has failed (Cart/Cut %d/%d): %s\n", res.Cart, res.Cut, res.ErrorString)
 		}
 	}
 }
@@ -362,7 +360,7 @@ func (c *TelnetClient) handle_iac(iac []byte, cancel chan<- bool) bool {
 	case IP:
 		select {
 		case cancel <- true:
-		default:  // process got canceled already
+		default: // process got canceled already
 		}
 		return false
 	default:
@@ -454,6 +452,13 @@ func (c *TelnetClient) handle() {
 
 	done := make(chan bool)
 	cancel := make(chan bool, 1)
+	defer func() { // make sure to cancel possible running job when closing connection
+		select {
+		case cancel <- true:
+		default:
+		}
+	}()
+
 	c.write_string(prompt)
 	for {
 		select {
-- 
cgit v0.10.2