cleaner now

This commit is contained in:
Jeffrey Paul 2019-11-05 17:15:31 -08:00
parent a25851c25c
commit 99f44c5f38
4 changed files with 69 additions and 91 deletions

View File

@ -8,10 +8,10 @@ import "github.com/gin-gonic/gin"
type hash map[string]interface{}
func (a *TootArchiverAPIServer) instances() []hash {
func (a *FetaAPIServer) instances() []hash {
resp := make([]hash, 0)
now := time.Now()
for _, v := range a.archiver.manager.listInstances() {
for _, v := range a.feta.manager.listInstances() {
i := make(hash)
// FIXME figure out why a very short lock here deadlocks
v.Lock()
@ -34,7 +34,7 @@ func (a *TootArchiverAPIServer) instances() []hash {
return resp
}
func (a *TootArchiverAPIServer) getIndexHandler() http.HandlerFunc {
func (a *FetaAPIServer) getIndexHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
index := &gin.H{
@ -42,7 +42,7 @@ func (a *TootArchiverAPIServer) getIndexHandler() http.HandlerFunc {
"instances": a.instances(),
"status": "ok",
"now": time.Now().UTC().Format(time.RFC3339),
"uptime": a.archiver.Uptime().String(),
"uptime": a.feta.Uptime().String(),
}
json, err := json.Marshal(index)
@ -57,12 +57,12 @@ func (a *TootArchiverAPIServer) getIndexHandler() http.HandlerFunc {
}
}
func (a *TootArchiverAPIServer) getHealthCheckHandler() http.HandlerFunc {
func (a *FetaAPIServer) getHealthCheckHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
resp := &gin.H{
"status": "ok",
"now": time.Now().UTC().Format(time.RFC3339),
"uptime": a.archiver.Uptime().String(),
"uptime": a.feta.Uptime().String(),
}
json, err := json.Marshal(resp)

View File

@ -9,17 +9,17 @@ import "github.com/rs/zerolog/log"
import "github.com/gin-gonic/gin"
import "github.com/dn365/gin-zerolog"
type TootArchiverAPIServer struct {
archiver *TootArchiver
type FetaAPIServer struct {
feta *FetaProcess
}
func (self *TootArchiverAPIServer) SetArchiver(archiver *TootArchiver) {
self.archiver = archiver
func (self *FetaAPIServer) SetFeta(feta *FetaProcess) {
self.feta = feta
}
func (a *TootArchiverAPIServer) Serve() {
if a.archiver == nil {
panic("must have archiver from which to serve stats")
func (a *FetaAPIServer) Serve() {
if a.feta == nil {
panic("must have feta app from which to serve stats")
}
s := a.getServer()
err := s.ListenAndServe()
@ -29,7 +29,7 @@ func (a *TootArchiverAPIServer) Serve() {
}
}
func (a *TootArchiverAPIServer) getRouter() *gin.Engine {
func (a *FetaAPIServer) getRouter() *gin.Engine {
if os.Getenv("DEBUG") == "" {
gin.SetMode(gin.ReleaseMode)
}
@ -49,7 +49,7 @@ func (a *TootArchiverAPIServer) getRouter() *gin.Engine {
return r
}
func (a *TootArchiverAPIServer) getServer() *http.Server {
func (a *FetaAPIServer) getServer() *http.Server {
r := a.getRouter()
port := "8080"

View File

@ -1,45 +0,0 @@
package feta
import "time"
type InstanceHostname string
type TootArchiver struct {
locator *InstanceLocator
manager *InstanceManager
startup *time.Time
}
func NewTootArchiver() *TootArchiver {
a := new(TootArchiver)
return a
}
func (a *TootArchiver) Uptime() time.Duration {
return time.Since(*a.startup)
}
func (a *TootArchiver) RunForever() {
t := time.Now()
a.startup = &t
newInstanceHostnameNotifications := make(chan InstanceHostname)
a.locator = NewInstanceLocator()
a.manager = NewInstanceManager()
a.locator.AddInstanceNotificationChannel(newInstanceHostnameNotifications)
a.manager.AddInstanceNotificationChannel(newInstanceHostnameNotifications)
// locator goroutine:
go a.locator.Locate()
// manager goroutine:
go a.manager.Manage()
// this goroutine (main) does nothing until we handle signals
// FIXME(sneak)
for {
time.Sleep(1 * time.Second)
}
}

85
feta.go
View File

@ -1,17 +1,17 @@
package feta
import "os"
//import "os/signal"
import "sync"
//import "syscall"
import "time"
import "github.com/jinzhu/gorm"
import _ "github.com/jinzhu/gorm/dialects/sqlite"
import "github.com/rs/zerolog"
import "github.com/rs/zerolog/log"
import "golang.org/x/crypto/ssh/terminal"
type InstanceHostname string
func CLIEntry(version string, buildtime string, buildarch string, builduser string) int {
f := new(FetaProcess)
f.version = version
@ -19,7 +19,7 @@ func CLIEntry(version string, buildtime string, buildarch string, builduser stri
f.buildarch = buildarch
f.builduser = builduser
f.setupLogging()
return f.runForever()
return f.RunForever()
}
// FetaProcess is the main structure/process of this app
@ -28,10 +28,11 @@ type FetaProcess struct {
buildtime string
buildarch string
builduser string
archiver *TootArchiver
api *TootArchiverAPIServer
wg *sync.WaitGroup
//quit chan os.Signal
locator *InstanceLocator
manager *InstanceManager
api *FetaAPIServer
db *gorm.DB
startup *time.Time
}
func (f *FetaProcess) identify() {
@ -44,9 +45,17 @@ func (f *FetaProcess) identify() {
}
func (f *FetaProcess) setupLogging() {
log.Logger = log.With().Caller().Logger()
if terminal.IsTerminal(int(os.Stdout.Fd())) {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
out := zerolog.NewConsoleWriter(
func(w *zerolog.ConsoleWriter) {
// Customize time format
w.TimeFormat = time.RFC3339
},
)
log.Logger = log.Output(out)
}
// always log in UTC
@ -62,30 +71,44 @@ func (f *FetaProcess) setupLogging() {
f.identify()
}
func (f *FetaProcess) runForever() int {
f.archiver = NewTootArchiver()
func (f *FetaProcess) Uptime() time.Duration {
return time.Since(*f.startup)
}
f.api = new(TootArchiverAPIServer)
f.api.SetArchiver(f.archiver)
func (f *FetaProcess) RunForever() int {
t := time.Now()
f.startup = &t
//FIXME(sneak) get this channel into places that need to be shut down
//f.quit = make(chan os.Signal)
//signal.Notify(f.quit, syscall.SIGINT, syscall.SIGTERM)
var err error
f.db, err = gorm.Open("sqlite3", "/feta.sqlite")
defer f.db.Close()
f.wg = new(sync.WaitGroup)
// start api webserver goroutine
f.wg.Add(1)
go func() {
f.api.Serve()
f.wg.Done()
}()
if err != nil {
log.Panic().Err(err)
}
newInstanceHostnameNotifications := make(chan InstanceHostname)
f.wg.Add(1)
go func() {
f.archiver.RunForever()
f.wg.Done()
}()
f.locator = NewInstanceLocator()
f.manager = NewInstanceManager()
f.api = new(FetaAPIServer)
f.api.feta = f // api needs to get to us
f.locator.AddInstanceNotificationChannel(newInstanceHostnameNotifications)
f.manager.AddInstanceNotificationChannel(newInstanceHostnameNotifications)
// locator goroutine:
go f.locator.Locate()
// manager goroutine:
go f.manager.Manage()
go f.api.Serve()
// this goroutine (main) does nothing until we handle signals
// FIXME(sneak)
for {
time.Sleep(1 * time.Second)
}
f.wg.Wait()
return 0
}