2020-11-29 03:22:39 +05:00
|
|
|
package application
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-29 05:44:21 +05:00
|
|
|
"net/http"
|
|
|
|
"sync"
|
2020-11-29 03:22:39 +05:00
|
|
|
|
2020-11-29 06:09:35 +05:00
|
|
|
"go.dev.pztrn.name/metricator/internal/common"
|
2020-12-23 16:28:57 +05:00
|
|
|
"go.dev.pztrn.name/metricator/internal/logger"
|
2020-11-29 03:22:39 +05:00
|
|
|
"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 {
|
2020-12-23 19:31:09 +05:00
|
|
|
ctx context.Context
|
|
|
|
storage storage.Metrics
|
|
|
|
config *Config
|
|
|
|
doneChan chan struct{}
|
|
|
|
logger *logger.Logger
|
|
|
|
storageDone chan struct{}
|
|
|
|
httpClient *http.Client
|
|
|
|
name string
|
2020-11-29 05:44:21 +05:00
|
|
|
fetchIsRunningMutex sync.RWMutex
|
2020-12-23 19:31:09 +05:00
|
|
|
fetchIsRunning bool
|
2020-11-29 03:22:39 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewApplication creates new application.
|
2020-12-23 16:28:57 +05:00
|
|
|
func NewApplication(ctx context.Context, name string, config *Config, logger *logger.Logger) *Application {
|
2021-11-21 14:45:09 +05:00
|
|
|
// Some variables are initialized in initialize() function.
|
|
|
|
// nolint:exhaustivestruct
|
|
|
|
app := &Application{
|
2020-11-29 03:22:39 +05:00
|
|
|
config: config,
|
|
|
|
ctx: ctx,
|
|
|
|
doneChan: make(chan struct{}),
|
2020-12-23 16:28:57 +05:00
|
|
|
logger: logger,
|
2020-11-29 03:22:39 +05:00
|
|
|
name: name,
|
|
|
|
}
|
2021-11-21 14:45:09 +05:00
|
|
|
app.initialize()
|
2020-11-29 03:22:39 +05:00
|
|
|
|
2021-11-21 14:45:09 +05:00
|
|
|
return app
|
2020-11-29 03:22:39 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-11-29 06:09:35 +05:00
|
|
|
// GetHandler returns HTTP requests handling function.
|
|
|
|
func (a *Application) GetHandler() common.HTTPHandlerFunc {
|
|
|
|
return a.respond
|
|
|
|
}
|
|
|
|
|
2020-11-29 03:22:39 +05:00
|
|
|
// Initializes internal things like storage, HTTP client, etc.
|
|
|
|
func (a *Application) initialize() {
|
2020-12-23 16:28:57 +05:00
|
|
|
a.storage, a.storageDone = memory.NewStorage(a.ctx, a.name+" storage", a.logger)
|
2020-11-29 03:22:39 +05:00
|
|
|
|
2020-12-23 16:28:57 +05:00
|
|
|
a.logger.Debugf("Application '%s' initialized with configuration: %+v\n", a.name, a.config)
|
2020-11-29 03:22:39 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts asynchronous things like data fetching, storage cleanup, etc.
|
|
|
|
func (a *Application) Start() {
|
|
|
|
a.storage.Start()
|
|
|
|
|
2020-11-29 05:44:21 +05:00
|
|
|
go a.startFetcher()
|
|
|
|
|
2020-11-29 03:22:39 +05:00
|
|
|
// The Context Listening Goroutine.
|
|
|
|
go func() {
|
|
|
|
<-a.ctx.Done()
|
|
|
|
// We should wait until storage routines are also stopped.
|
|
|
|
<-a.storage.GetDoneChan()
|
|
|
|
|
2020-12-23 16:28:57 +05:00
|
|
|
a.logger.Infoln("Application", a.name, "stopped")
|
2020-11-29 03:22:39 +05:00
|
|
|
|
|
|
|
a.doneChan <- struct{}{}
|
|
|
|
}()
|
|
|
|
}
|