comparison pkg/scheduler/boot.go @ 1557:62171cd9a42b

Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 11 Dec 2018 22:59:10 +0100
parents
children f39957ea08aa
comparison
equal deleted inserted replaced
1556:6869eb94ead2 1557:62171cd9a42b
1 // This is Free Software under GNU Affero General Public License v >= 3.0
2 // without warranty, see README.md and license for details.
3 //
4 // SPDX-License-Identifier: AGPL-3.0-or-later
5 // License-Filename: LICENSES/AGPL-3.0.txt
6 //
7 // Copyright (C) 2018 by via donau
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
9 // Software engineering by Intevation GmbH
10 //
11 // Author(s):
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
13
14 package scheduler
15
16 import (
17 "context"
18 "database/sql"
19 "log"
20
21 "gemma.intevation.de/gemma/pkg/auth"
22 "gemma.intevation.de/gemma/pkg/config"
23 )
24
25 const (
26 bootRole = "sys_admin"
27
28 selectImportConfSQL = `
29 SELECT id, username, kind, cron
30 FROM waterway.import_configuration
31 WHERE cron IS NOT NULL`
32 )
33
34 func init() { go boot() }
35
36 // boot starts the scheduler with the configurations from
37 // the database which have a schedule.
38 func boot() {
39 config.WaitReady()
40 log.Println("info: booting scheduler from database.")
41 ctx := context.Background()
42 err := auth.RunAs(
43 ctx, bootRole,
44 func(conn *sql.Conn) error {
45 rows, err := conn.QueryContext(ctx, selectImportConfSQL)
46 if err != nil {
47 return err
48 }
49 defer rows.Close()
50 err = BootActions(func(ba *BoundAction) (bool, error) {
51 if err != nil {
52 return false, err
53 }
54 if !rows.Next() {
55 return false, nil
56 }
57 var id int64
58 if err = rows.Scan(
59 &id,
60 &ba.User,
61 &ba.Name,
62 &ba.Spec,
63 ); err != nil {
64 return false, err
65 }
66 ba.CfgID = &id
67 return true, nil
68 })
69 if err != nil {
70 return err
71 }
72 return rows.Err()
73 })
74 if err != nil {
75 log.Printf("error: %v\n", err)
76 }
77 }