Rethink how metricator will work, golangci-lint and gitlab ci configs.
This commit is contained in:
63
internal/application/application.go
Normal file
63
internal/application/application.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"go.dev.pztrn.name/metricator/internal/storage"
|
||||
"go.dev.pztrn.name/metricator/internal/storage/memory"
|
||||
)
|
||||
|
||||
// Application is a thing that responsible for all application-related
|
||||
// actions like data fetching, storing, etc. on higher level.
|
||||
type Application struct {
|
||||
config *Config
|
||||
ctx context.Context
|
||||
doneChan chan struct{}
|
||||
name string
|
||||
|
||||
storage storage.Metrics
|
||||
storageDone chan struct{}
|
||||
}
|
||||
|
||||
// NewApplication creates new application.
|
||||
func NewApplication(ctx context.Context, name string, config *Config) *Application {
|
||||
a := &Application{
|
||||
config: config,
|
||||
ctx: ctx,
|
||||
doneChan: make(chan struct{}),
|
||||
name: name,
|
||||
}
|
||||
a.initialize()
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// GetDoneChan returns a channel which should be used to block execution until
|
||||
// application's routines are completed.
|
||||
func (a *Application) GetDoneChan() chan struct{} {
|
||||
return a.doneChan
|
||||
}
|
||||
|
||||
// Initializes internal things like storage, HTTP client, etc.
|
||||
func (a *Application) initialize() {
|
||||
a.storage, a.storageDone = memory.NewStorage(a.ctx, a.name+" storage")
|
||||
|
||||
log.Printf("Application '%s' initialized with configuration: %+v\n", a.name, a.config)
|
||||
}
|
||||
|
||||
// Start starts asynchronous things like data fetching, storage cleanup, etc.
|
||||
func (a *Application) Start() {
|
||||
a.storage.Start()
|
||||
|
||||
// The Context Listening Goroutine.
|
||||
go func() {
|
||||
<-a.ctx.Done()
|
||||
// We should wait until storage routines are also stopped.
|
||||
<-a.storage.GetDoneChan()
|
||||
|
||||
log.Println("Application", a.name, "stopped")
|
||||
|
||||
a.doneChan <- struct{}{}
|
||||
}()
|
||||
}
|
15
internal/application/config.go
Normal file
15
internal/application/config.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package application
|
||||
|
||||
import "time"
|
||||
|
||||
// Config is a generic application configuration.
|
||||
type Config struct {
|
||||
// Endpoint is a remote application endpoint which should give us metrics
|
||||
// in Prometheus format.
|
||||
Endpoint string
|
||||
// Headers is a list of headers that should be added to metrics request.
|
||||
Headers map[string]string
|
||||
// TimeBetweenRequests is a minimal amount of time which should pass
|
||||
// between requests.
|
||||
TimeBetweenRequests time.Duration
|
||||
}
|
4
internal/application/fetcher.go
Normal file
4
internal/application/fetcher.go
Normal file
@@ -0,0 +1,4 @@
|
||||
package application
|
||||
|
||||
// Configures and starts Prometheus data fetching goroutine.
|
||||
func (a *Application) startFetcher() {}
|
Reference in New Issue
Block a user