2017-06-23 11:12:24 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ecix/alice-lg/backend/api"
|
|
|
|
"github.com/ecix/alice-lg/backend/sources"
|
|
|
|
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-06-23 12:36:32 +02:00
|
|
|
const (
|
|
|
|
STATE_INIT = iota
|
|
|
|
STATE_READY
|
|
|
|
STATE_UPDATING
|
|
|
|
STATE_ERROR
|
|
|
|
)
|
|
|
|
|
|
|
|
type StoreStatus struct {
|
|
|
|
LastRefresh time.Time
|
|
|
|
LastError error
|
|
|
|
State int
|
|
|
|
}
|
|
|
|
|
2017-06-23 11:12:24 +02:00
|
|
|
type RoutesStore struct {
|
|
|
|
routesMap map[sources.Source]api.RoutesResponse
|
2017-06-23 12:36:32 +02:00
|
|
|
statusMap map[sources.Source]StoreStatus
|
2017-06-23 11:12:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRoutesStore(config *Config) *RoutesStore {
|
|
|
|
|
|
|
|
// Build mapping based on source instances
|
|
|
|
routesMap := make(map[sources.Source]api.RoutesResponse)
|
2017-06-23 12:36:32 +02:00
|
|
|
statusMap := make(map[sources.Source]StoreStatus)
|
2017-06-23 11:12:24 +02:00
|
|
|
for _, source := range config.Sources {
|
2017-06-23 12:36:32 +02:00
|
|
|
instance := source.getInstance()
|
|
|
|
routesMap[instance] = api.RoutesResponse{}
|
|
|
|
statusMap[instance] = StoreStatus{
|
|
|
|
State: STATE_INIT,
|
|
|
|
}
|
2017-06-23 11:12:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
store := &RoutesStore{
|
|
|
|
routesMap: routesMap,
|
2017-06-23 12:36:32 +02:00
|
|
|
statusMap: statusMap,
|
2017-06-23 11:12:24 +02:00
|
|
|
}
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RoutesStore) Start() {
|
|
|
|
log.Println("Starting local routes store")
|
|
|
|
|
2017-06-23 12:36:32 +02:00
|
|
|
// Initial refresh
|
|
|
|
go self.update()
|
2017-06-23 11:12:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update all routes
|
|
|
|
func (self *RoutesStore) update() {
|
|
|
|
for source, _ := range self.routesMap {
|
2017-06-23 12:36:32 +02:00
|
|
|
// Get current update state
|
|
|
|
if self.statusMap[source].State == STATE_UPDATING {
|
|
|
|
continue // nothing to do here
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set update state
|
|
|
|
self.statusMap[source] = StoreStatus{
|
|
|
|
State: STATE_UPDATING,
|
|
|
|
}
|
|
|
|
|
2017-06-23 11:12:24 +02:00
|
|
|
routes, err := source.AllRoutes()
|
|
|
|
if err != nil {
|
2017-06-23 12:36:32 +02:00
|
|
|
self.statusMap[source] = StoreStatus{
|
|
|
|
State: STATE_ERROR,
|
|
|
|
LastError: err,
|
|
|
|
LastRefresh: time.Now(),
|
|
|
|
}
|
|
|
|
|
2017-06-23 11:12:24 +02:00
|
|
|
log.Println("Error while updating routes cache:", err)
|
|
|
|
continue
|
|
|
|
}
|
2017-06-23 12:36:32 +02:00
|
|
|
|
|
|
|
// Update data
|
2017-06-23 11:12:24 +02:00
|
|
|
self.routesMap[source] = routes
|
2017-06-23 12:36:32 +02:00
|
|
|
// Update state
|
|
|
|
self.statusMap[source] = StoreStatus{
|
|
|
|
LastRefresh: time.Now(),
|
|
|
|
State: STATE_READY,
|
|
|
|
}
|
2017-06-23 11:12:24 +02:00
|
|
|
}
|
2017-06-23 12:36:32 +02:00
|
|
|
|
|
|
|
log.Println("All caches refreshed")
|
2017-06-23 11:12:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RoutesStore) Lookup(prefix string) []api.LookupRoute {
|
|
|
|
result := []api.LookupRoute{}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|