annotate pkg/geoserver/reconf.go @ 5490:5f47eeea988d logging

Use own logging package.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 20 Sep 2021 17:45:39 +0200
parents 2de644208706
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1017
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 890
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 890
diff changeset
2 // without warranty, see README.md and license for details.
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 890
diff changeset
3 //
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 890
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 890
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 890
diff changeset
6 //
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 890
diff changeset
7 // Copyright (C) 2018 by via donau
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 890
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 890
diff changeset
9 // Software engineering by Intevation GmbH
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 890
diff changeset
10 //
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 890
diff changeset
11 // Author(s):
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 890
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 890
diff changeset
13
890
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
14 package geoserver
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
15
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
16 import (
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
17 "container/list"
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
18 "net"
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
19 "net/url"
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
20 "sync"
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
21 "time"
4290
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
22
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4290
diff changeset
23 "gemma.intevation.de/gemma/pkg/log"
4290
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
24 "gemma.intevation.de/gemma/pkg/models"
890
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
25 )
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
26
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
27 var (
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
28 confQueue = list.New()
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
29 confQueueCond = sync.NewCond(new(sync.Mutex))
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
30 )
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
31
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
32 func init() {
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
33 go asyncConfigure()
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
34 }
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
35
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
36 func asyncConfigure() {
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
37 for {
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
38 var fn func() error
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
39 confQueueCond.L.Lock()
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
40 for confQueue.Len() == 0 {
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
41 confQueueCond.Wait()
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
42 }
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
43 fn = confQueue.Remove(confQueue.Front()).(func() error)
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
44 confQueueCond.L.Unlock()
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
45 if err := reconfigure(fn); err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4290
diff changeset
46 log.Warnf("configure GeoServer failed: %v\n", err)
890
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
47 }
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
48 }
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
49 }
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
50
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
51 func reconfigure(fn func() error) error {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4290
diff changeset
52 log.Infof("configure GeoServer...")
890
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
53 const (
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
54 maxTries = 10
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
55 sleep = time.Second * 5
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
56 )
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
57 var err error
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
58 for try := 1; try <= maxTries; try++ {
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
59 if err = fn(); err == nil {
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
60 break
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
61 }
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
62 if try < maxTries {
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
63 if uerr, ok := err.(*url.Error); ok {
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
64 if oerr, ok := uerr.Err.(*net.OpError); ok && oerr.Op == "dial" {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4290
diff changeset
65 log.Warnf("failed attempt %d of %d to configure GeoServer. "+
890
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
66 "Will try again in %s...\n", try, maxTries, sleep)
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
67 time.Sleep(sleep)
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
68 continue
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
69 }
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
70 }
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
71 }
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
72 break
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
73 }
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
74 return err
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
75 }
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
76
1466
d7152eb11d58 Added api doc strings for the geoserver package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1035
diff changeset
77 // Reconfigure adds a function to a queue of functions to update
d7152eb11d58 Added api doc strings for the geoserver package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1035
diff changeset
78 // the GeoServer async. The functions are run again a number
d7152eb11d58 Added api doc strings for the geoserver package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1035
diff changeset
79 // of times if network errors apppear indicating that the
d7152eb11d58 Added api doc strings for the geoserver package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1035
diff changeset
80 // GeoServer is down.
890
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
81 func Reconfigure(fn func() error) {
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
82 confQueueCond.L.Lock()
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
83 defer confQueueCond.L.Unlock()
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
84 confQueue.PushBack(fn)
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
85 confQueueCond.Signal()
0a563fef64a9 Reconfiguring GeoServer is now queued async.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
86 }
4290
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
87
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
88 // ReconfigureStyle returns a function to update a style
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
89 // in the GeoServer to be in sync with the database.
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
90 func ReconfigureStyle(name string) {
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
91 Reconfigure(func() error {
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
92 var stls styles
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
93 if err := stls.load(); err != nil {
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
94 return err
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
95 }
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
96
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
97 entries := models.InternalServices.Filter(
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
98 models.IntAnd(
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
99 models.IntWMS,
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
100 models.IntWithStyle,
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
101 models.IntByName(name)))
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
102
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
103 for i := range entries {
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
104 entry := &entries[i]
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
105 create := !stls.hasStyle(entry.Name)
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
106 if err := updateStyle(entry, create); err != nil {
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
107 return err
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
108 }
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
109 }
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
110
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
111 return nil
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
112 })
2de644208706 GeoServer config: Moved ReconfigureStyle function out of boot.go into reconf.go .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1466
diff changeset
113 }