cleaner now
This commit is contained in:
parent
a25851c25c
commit
99f44c5f38
@ -8,10 +8,10 @@ import "github.com/gin-gonic/gin"
|
|||||||
|
|
||||||
type hash map[string]interface{}
|
type hash map[string]interface{}
|
||||||
|
|
||||||
func (a *TootArchiverAPIServer) instances() []hash {
|
func (a *FetaAPIServer) instances() []hash {
|
||||||
resp := make([]hash, 0)
|
resp := make([]hash, 0)
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
for _, v := range a.archiver.manager.listInstances() {
|
for _, v := range a.feta.manager.listInstances() {
|
||||||
i := make(hash)
|
i := make(hash)
|
||||||
// FIXME figure out why a very short lock here deadlocks
|
// FIXME figure out why a very short lock here deadlocks
|
||||||
v.Lock()
|
v.Lock()
|
||||||
@ -34,7 +34,7 @@ func (a *TootArchiverAPIServer) instances() []hash {
|
|||||||
return resp
|
return resp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *TootArchiverAPIServer) getIndexHandler() http.HandlerFunc {
|
func (a *FetaAPIServer) getIndexHandler() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
index := &gin.H{
|
index := &gin.H{
|
||||||
@ -42,7 +42,7 @@ func (a *TootArchiverAPIServer) getIndexHandler() http.HandlerFunc {
|
|||||||
"instances": a.instances(),
|
"instances": a.instances(),
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"now": time.Now().UTC().Format(time.RFC3339),
|
"now": time.Now().UTC().Format(time.RFC3339),
|
||||||
"uptime": a.archiver.Uptime().String(),
|
"uptime": a.feta.Uptime().String(),
|
||||||
}
|
}
|
||||||
|
|
||||||
json, err := json.Marshal(index)
|
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) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
resp := &gin.H{
|
resp := &gin.H{
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"now": time.Now().UTC().Format(time.RFC3339),
|
"now": time.Now().UTC().Format(time.RFC3339),
|
||||||
"uptime": a.archiver.Uptime().String(),
|
"uptime": a.feta.Uptime().String(),
|
||||||
}
|
}
|
||||||
|
|
||||||
json, err := json.Marshal(resp)
|
json, err := json.Marshal(resp)
|
||||||
|
18
apiserver.go
18
apiserver.go
@ -9,17 +9,17 @@ import "github.com/rs/zerolog/log"
|
|||||||
import "github.com/gin-gonic/gin"
|
import "github.com/gin-gonic/gin"
|
||||||
import "github.com/dn365/gin-zerolog"
|
import "github.com/dn365/gin-zerolog"
|
||||||
|
|
||||||
type TootArchiverAPIServer struct {
|
type FetaAPIServer struct {
|
||||||
archiver *TootArchiver
|
feta *FetaProcess
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *TootArchiverAPIServer) SetArchiver(archiver *TootArchiver) {
|
func (self *FetaAPIServer) SetFeta(feta *FetaProcess) {
|
||||||
self.archiver = archiver
|
self.feta = feta
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *TootArchiverAPIServer) Serve() {
|
func (a *FetaAPIServer) Serve() {
|
||||||
if a.archiver == nil {
|
if a.feta == nil {
|
||||||
panic("must have archiver from which to serve stats")
|
panic("must have feta app from which to serve stats")
|
||||||
}
|
}
|
||||||
s := a.getServer()
|
s := a.getServer()
|
||||||
err := s.ListenAndServe()
|
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") == "" {
|
if os.Getenv("DEBUG") == "" {
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
}
|
}
|
||||||
@ -49,7 +49,7 @@ func (a *TootArchiverAPIServer) getRouter() *gin.Engine {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *TootArchiverAPIServer) getServer() *http.Server {
|
func (a *FetaAPIServer) getServer() *http.Server {
|
||||||
r := a.getRouter()
|
r := a.getRouter()
|
||||||
|
|
||||||
port := "8080"
|
port := "8080"
|
||||||
|
45
archiver.go
45
archiver.go
@ -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
85
feta.go
@ -1,17 +1,17 @@
|
|||||||
package feta
|
package feta
|
||||||
|
|
||||||
import "os"
|
import "os"
|
||||||
|
|
||||||
//import "os/signal"
|
|
||||||
import "sync"
|
|
||||||
|
|
||||||
//import "syscall"
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
|
import "github.com/jinzhu/gorm"
|
||||||
|
import _ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||||
|
|
||||||
import "github.com/rs/zerolog"
|
import "github.com/rs/zerolog"
|
||||||
import "github.com/rs/zerolog/log"
|
import "github.com/rs/zerolog/log"
|
||||||
import "golang.org/x/crypto/ssh/terminal"
|
import "golang.org/x/crypto/ssh/terminal"
|
||||||
|
|
||||||
|
type InstanceHostname string
|
||||||
|
|
||||||
func CLIEntry(version string, buildtime string, buildarch string, builduser string) int {
|
func CLIEntry(version string, buildtime string, buildarch string, builduser string) int {
|
||||||
f := new(FetaProcess)
|
f := new(FetaProcess)
|
||||||
f.version = version
|
f.version = version
|
||||||
@ -19,7 +19,7 @@ func CLIEntry(version string, buildtime string, buildarch string, builduser stri
|
|||||||
f.buildarch = buildarch
|
f.buildarch = buildarch
|
||||||
f.builduser = builduser
|
f.builduser = builduser
|
||||||
f.setupLogging()
|
f.setupLogging()
|
||||||
return f.runForever()
|
return f.RunForever()
|
||||||
}
|
}
|
||||||
|
|
||||||
// FetaProcess is the main structure/process of this app
|
// FetaProcess is the main structure/process of this app
|
||||||
@ -28,10 +28,11 @@ type FetaProcess struct {
|
|||||||
buildtime string
|
buildtime string
|
||||||
buildarch string
|
buildarch string
|
||||||
builduser string
|
builduser string
|
||||||
archiver *TootArchiver
|
locator *InstanceLocator
|
||||||
api *TootArchiverAPIServer
|
manager *InstanceManager
|
||||||
wg *sync.WaitGroup
|
api *FetaAPIServer
|
||||||
//quit chan os.Signal
|
db *gorm.DB
|
||||||
|
startup *time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FetaProcess) identify() {
|
func (f *FetaProcess) identify() {
|
||||||
@ -44,9 +45,17 @@ func (f *FetaProcess) identify() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *FetaProcess) setupLogging() {
|
func (f *FetaProcess) setupLogging() {
|
||||||
|
|
||||||
log.Logger = log.With().Caller().Logger()
|
log.Logger = log.With().Caller().Logger()
|
||||||
|
|
||||||
if terminal.IsTerminal(int(os.Stdout.Fd())) {
|
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
|
// always log in UTC
|
||||||
@ -62,30 +71,44 @@ func (f *FetaProcess) setupLogging() {
|
|||||||
f.identify()
|
f.identify()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FetaProcess) runForever() int {
|
func (f *FetaProcess) Uptime() time.Duration {
|
||||||
f.archiver = NewTootArchiver()
|
return time.Since(*f.startup)
|
||||||
|
}
|
||||||
|
|
||||||
f.api = new(TootArchiverAPIServer)
|
func (f *FetaProcess) RunForever() int {
|
||||||
f.api.SetArchiver(f.archiver)
|
t := time.Now()
|
||||||
|
f.startup = &t
|
||||||
|
|
||||||
//FIXME(sneak) get this channel into places that need to be shut down
|
var err error
|
||||||
//f.quit = make(chan os.Signal)
|
f.db, err = gorm.Open("sqlite3", "/feta.sqlite")
|
||||||
//signal.Notify(f.quit, syscall.SIGINT, syscall.SIGTERM)
|
defer f.db.Close()
|
||||||
|
|
||||||
f.wg = new(sync.WaitGroup)
|
if err != nil {
|
||||||
// start api webserver goroutine
|
log.Panic().Err(err)
|
||||||
f.wg.Add(1)
|
}
|
||||||
go func() {
|
newInstanceHostnameNotifications := make(chan InstanceHostname)
|
||||||
f.api.Serve()
|
|
||||||
f.wg.Done()
|
|
||||||
}()
|
|
||||||
|
|
||||||
f.wg.Add(1)
|
f.locator = NewInstanceLocator()
|
||||||
go func() {
|
f.manager = NewInstanceManager()
|
||||||
f.archiver.RunForever()
|
f.api = new(FetaAPIServer)
|
||||||
f.wg.Done()
|
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
|
return 0
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user