func NewStore(ctx context.Context, db *sql.DB, keys keyring.Keys, fallbackURL string) (*Store, error) {
	p := util.Prepare{Ctx: ctx, DB: db}
	s := &Store{
		db:           db,
		fallbackURL:  fallbackURL,
		latestConfig: p.P(`select id, data, schema from config where schema <= $1 order by id desc limit 1`),
		setConfig:    p.P(`insert into config (id, schema, data) values (DEFAULT, $1, $2) returning (id)`),
		lock:         p.P(`lock config in exclusive mode`),
		keys:         keys,
		closeCh:      make(chan struct{}),
	}
	if p.Err != nil {
		return nil, p.Err
	}
	var err error
	permission.SudoContext(ctx, func(ctx context.Context) {
		err = s.Reload(ctx)
	})
	if err != nil {
		return nil, err
	}
	var seed int64
	err = binary.Read(cryptoRand.Reader, binary.BigEndian, &seed)
	if err != nil {
		return nil, err
	}
	src := rand.New(rand.NewSource(
		seed,
	))
	go func() {
		randDelay := func() time.Duration {
			return 30*time.Second + time.Duration(src.Int63n(int64(30*time.Second)))
		}
		t := time.NewTimer(randDelay())
		for {
			select {
			case <-t.C:
				t.Reset(randDelay())
				permission.SudoContext(context.Background(), func(ctx context.Context) {
					err := s.Reload(ctx)
					if err != nil {
						log.Log(ctx, errors.Wrap(err, "config auto-reload"))
					}
				})
			case s.closeCh <- struct{}{}:
				close(s.closeCh)
				return
			}
		}
	}()
	return s, nil
}
NewStore will create a new Store with the given parameters. It will automatically detect
new configuration changes.
package config
import (
	"context"
	cryptoRand "crypto/rand"
	"database/sql"
	"encoding/binary"
	"encoding/json"
	"fmt"
	"io"
	"math/rand"
	"net/http"
	"sync"
	"time"
	"github.com/target/goalert/keyring"
	"github.com/target/goalert/permission"
	"github.com/target/goalert/util"
	"github.com/target/goalert/util/errutil"
	"github.com/target/goalert/util/jsonutil"
	"github.com/target/goalert/util/log"
	"github.com/pkg/errors"
)
type Store struct {
	rawCfg       Config
	cfgVers      int
	fallbackURL  string
	mx           sync.RWMutex
	db           *sql.DB
	keys         keyring.Keys
	latestConfig *sql.Stmt
	setConfig    *sql.Stmt
	lock         *sql.Stmt
	closeCh chan struct{}
}