view pkg/geoserver/reconf.go @ 2549:9bf6b767a56a

client: refactored and improved splitscreen for diagrams To make different diagrams possible, the splitscreen view needed to be decoupled from the cross profiles. Also the style has changed to make it more consistent with the rest of the app. The standard box header is now used and there are collapse and expand animations.
author Markus Kottlaender <markus@intevation.de>
date Fri, 08 Mar 2019 08:50:47 +0100
parents d7152eb11d58
children 2de644208706
line wrap: on
line source

// This is Free Software under GNU Affero General Public License v >= 3.0
// without warranty, see README.md and license for details.
//
// SPDX-License-Identifier: AGPL-3.0-or-later
// License-Filename: LICENSES/AGPL-3.0.txt
//
// Copyright (C) 2018 by via donau
//   – Österreichische Wasserstraßen-Gesellschaft mbH
// Software engineering by Intevation GmbH
//
// Author(s):
//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>

package geoserver

import (
	"container/list"
	"log"
	"net"
	"net/url"
	"sync"
	"time"
)

var (
	confQueue     = list.New()
	confQueueCond = sync.NewCond(new(sync.Mutex))
)

func init() {
	go asyncConfigure()
}

func asyncConfigure() {
	for {
		var fn func() error
		confQueueCond.L.Lock()
		for confQueue.Len() == 0 {
			confQueueCond.Wait()
		}
		fn = confQueue.Remove(confQueue.Front()).(func() error)
		confQueueCond.L.Unlock()
		if err := reconfigure(fn); err != nil {
			log.Printf("warn: configure GeoServer failed: %v\n", err)
		}
	}
}

func reconfigure(fn func() error) error {
	log.Println("info: configure GeoServer...")
	const (
		maxTries = 10
		sleep    = time.Second * 5
	)
	var err error
	for try := 1; try <= maxTries; try++ {
		if err = fn(); err == nil {
			break
		}
		if try < maxTries {
			if uerr, ok := err.(*url.Error); ok {
				if oerr, ok := uerr.Err.(*net.OpError); ok && oerr.Op == "dial" {
					log.Printf("warn: failed attempt %d of %d to configure GeoServer. "+
						"Will try again in %s...\n", try, maxTries, sleep)
					time.Sleep(sleep)
					continue
				}
			}
		}
		break
	}
	return err
}

// Reconfigure adds a function to a queue of functions to update
// the GeoServer async. The functions are run again a number
// of times if network errors apppear indicating that the
// GeoServer is down.
func Reconfigure(fn func() error) {
	confQueueCond.L.Lock()
	defer confQueueCond.L.Unlock()
	confQueue.PushBack(fn)
	confQueueCond.Signal()
}