changeset 1660:f32e8a973b48

Scheduler: Moved code to figure out the scheduled jobs per user from the database to a more suited place.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 21 Dec 2018 13:04:46 +0100
parents 897e7cad7e77
children 51a0ba4ede41
files pkg/controllers/user.go pkg/scheduler/boot.go
diffstat 2 files changed, 33 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/user.go	Fri Dec 21 12:19:29 2018 +0100
+++ b/pkg/controllers/user.go	Fri Dec 21 13:04:46 2018 +0100
@@ -17,7 +17,6 @@
 
 import (
 	"bytes"
-	"context"
 	"database/sql"
 	"fmt"
 	"log"
@@ -72,10 +71,6 @@
   ST_XMax(map_extent), ST_YMax(map_extent)
 FROM users.list_users
 WHERE username = $1`
-
-	scheduledIDsSQL = `
-SELECT id from waterway.import_configuration
-WHERE username = $1`
 )
 
 var (
@@ -101,30 +96,6 @@
 (inkluding import errors) and details on the concerned import.`))
 )
 
-func scheduledIDs(
-	ctx context.Context,
-	conn *sql.Conn,
-	user string,
-) (map[int64]struct{}, error) {
-	ids := map[int64]struct{}{}
-	rows, err := conn.QueryContext(ctx, scheduledIDsSQL, user)
-	if err != nil {
-		return nil, nil
-	}
-	defer rows.Close()
-	for rows.Next() {
-		var id int64
-		if err := rows.Scan(&id); err != nil {
-			return nil, err
-		}
-		ids[id] = struct{}{}
-	}
-	if err := rows.Err(); err != nil {
-		return nil, err
-	}
-	return ids, nil
-}
-
 func deleteUser(
 	_ interface{}, req *http.Request,
 	db *sql.Conn,
@@ -142,8 +113,10 @@
 		return
 	}
 
+	ctx := req.Context()
+
 	// Remove scheduled tasks.
-	ids, err2 := scheduledIDs(req.Context(), db, user)
+	ids, err2 := scheduler.ScheduledUserIDs(ctx, db, user)
 	if err2 == nil {
 		if len(ids) > 0 {
 			go func() { scheduler.UnbindByIDs(ids) }()
@@ -154,7 +127,7 @@
 
 	var res sql.Result
 
-	if res, err = db.ExecContext(req.Context(), deleteUserSQL, user); err != nil {
+	if res, err = db.ExecContext(ctx, deleteUserSQL, user); err != nil {
 		return
 	}
 
--- a/pkg/scheduler/boot.go	Fri Dec 21 12:19:29 2018 +0100
+++ b/pkg/scheduler/boot.go	Fri Dec 21 13:04:46 2018 +0100
@@ -29,6 +29,10 @@
 SELECT id, username, kind, cron
 FROM waterway.import_configuration
 WHERE cron IS NOT NULL`
+
+	scheduledIDsSQL = `
+SELECT id from waterway.import_configuration
+WHERE username = $1 AND cron IS NOT NULL`
 )
 
 func init() { go boot() }
@@ -74,3 +78,28 @@
 		log.Printf("error: %v\n", err)
 	}
 }
+
+// ScheduledUserIDs returns the IDs with a schedule for a given user.
+func ScheduledUserIDs(
+	ctx context.Context,
+	conn *sql.Conn,
+	user string,
+) (map[int64]struct{}, error) {
+	ids := map[int64]struct{}{}
+	rows, err := conn.QueryContext(ctx, scheduledIDsSQL, user)
+	if err != nil {
+		return nil, nil
+	}
+	defer rows.Close()
+	for rows.Next() {
+		var id int64
+		if err := rows.Scan(&id); err != nil {
+			return nil, err
+		}
+		ids[id] = struct{}{}
+	}
+	if err := rows.Err(); err != nil {
+		return nil, err
+	}
+	return ids, nil
+}