fix: worker retry and fly new database

This commit is contained in:
Tine 2024-02-21 10:41:20 +01:00
parent c862660e7d
commit f8c8e381c1
Signed by: mentos1386
SSH key fingerprint: SHA256:MNtTsLbihYaWF8j1fkOHfkKNlnN1JQfxEU/rBU8nCGw
6 changed files with 48 additions and 39 deletions

View file

@ -16,6 +16,9 @@ primary_region = 'waw'
ROOT_URL = 'https://zdravko.mnts.dev'
TEMPORAL_SERVER_HOST = 'server.process.zdravko.internal:7233'
TEMPORAL_DATABASE_PATH = '/data/temporal-1.db'
DATABASE_PATH = '/data/zdravko-1.db'
[processes]
server = '--temporal --server'
worker = '--worker'

View file

@ -16,13 +16,14 @@ import (
)
type Server struct {
server *http.Server
cfg *config.ServerConfig
echo *echo.Echo
cfg *config.ServerConfig
}
func NewServer(cfg *config.ServerConfig) (*Server, error) {
return &Server{
cfg: cfg,
cfg: cfg,
echo: echo.New(),
}, nil
}
@ -31,10 +32,9 @@ func (s *Server) Name() string {
}
func (s *Server) Start() error {
e := echo.New()
e.Renderer = templates.NewTemplates()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
s.echo.Renderer = templates.NewTemplates()
s.echo.Use(middleware.Logger())
s.echo.Use(middleware.Recover())
db, query, err := internal.ConnectToDatabase(s.cfg.DatabasePath)
if err != nil {
@ -51,7 +51,7 @@ func (s *Server) Start() error {
h := handlers.NewBaseHandler(db, query, temporalClient, s.cfg)
// Health
e.GET("/health", func(c echo.Context) error {
s.echo.GET("/health", func(c echo.Context) error {
d, err := db.DB()
if err != nil {
return err
@ -65,16 +65,16 @@ func (s *Server) Start() error {
})
// Server static files
stat := e.Group("/static")
stat := s.echo.Group("/static")
stat.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Filesystem: http.FS(static.Static),
}))
// Public
e.GET("", h.Index)
s.echo.GET("", h.Index)
// Settings
settings := e.Group("/settings")
settings := s.echo.Group("/settings")
settings.Use(h.Authenticated)
settings.GET("", h.SettingsOverviewGET)
settings.GET("/healthchecks", h.SettingsHealthchecksGET)
@ -89,19 +89,19 @@ func (s *Server) Start() error {
settings.Match([]string{"GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"}, "/temporal*", h.Temporal)
// OAuth2
oauth2 := e.Group("/oauth2")
oauth2 := s.echo.Group("/oauth2")
oauth2.GET("/login", h.OAuth2LoginGET)
oauth2.GET("/callback", h.OAuth2CallbackGET)
oauth2.GET("/logout", h.OAuth2LogoutGET, h.Authenticated)
// API
apiv1 := e.Group("/api/v1")
apiv1 := s.echo.Group("/api/v1")
apiv1.Use(h.Authenticated)
apiv1.GET("/workers/connect", h.ApiV1WorkersConnectGET)
apiv1.POST("/healthcheck/:slug/history", h.ApiV1HealthchecksHistoryPOST)
// Error handler
e.HTTPErrorHandler = func(err error, c echo.Context) {
s.echo.HTTPErrorHandler = func(err error, c echo.Context) {
code := http.StatusInternalServerError
if he, ok := err.(*echo.HTTPError); ok {
code = he.Code
@ -114,10 +114,10 @@ func (s *Server) Start() error {
_ = c.String(code, err.Error())
}
return e.Start(":" + s.cfg.Port)
return s.echo.Start(":" + s.cfg.Port)
}
func (s *Server) Stop() error {
ctx := context.Background()
return s.server.Shutdown(ctx)
return s.echo.Shutdown(ctx)
}

View file

@ -5,11 +5,13 @@ import (
"io"
"log"
"net/http"
"time"
"code.tjo.space/mentos1386/zdravko/internal/activities"
"code.tjo.space/mentos1386/zdravko/internal/config"
"code.tjo.space/mentos1386/zdravko/internal/temporal"
"code.tjo.space/mentos1386/zdravko/internal/workflows"
"code.tjo.space/mentos1386/zdravko/pkg/retry"
"github.com/pkg/errors"
"go.temporal.io/sdk/worker"
)
@ -29,23 +31,25 @@ func getConnectionConfig(token string, apiUrl string) (*ConnectionConfig, error)
}
req.Header.Add("Authorization", "Bearer "+token)
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to connect to API")
}
return retry.Retry(10, 3*time.Second, func() (*ConnectionConfig, error) {
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to connect to API")
}
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read response body")
}
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read response body")
}
config := ConnectionConfig{}
err = json.Unmarshal(body, &config)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal connection config")
}
config := ConnectionConfig{}
err = json.Unmarshal(body, &config)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal connection config")
}
return &config, nil
return &config, nil
})
}
type Worker struct {

View file

@ -748,10 +748,18 @@ video {
justify-content: center;
}
.justify-between {
justify-content: space-between;
}
.gap-2 {
gap: 0.5rem;
}
.gap-4 {
gap: 1rem;
}
.gap-8 {
gap: 2rem;
}

View file

@ -1,6 +1,6 @@
{{define "settings"}}
{{ $description := "Healthchecks represent periodic checks of some HTTP or TCP service, to see if it's responding correctly to deterime if it's healthy or not." }}
{{ $description := "Healthchecks represent periodicly the k6 script, to see if the monitored service is healthy." }}
{{ if eq .HealthchecksLength 0 }}
<section>
@ -24,7 +24,7 @@
<table class="w-full text-sm text-left rtl:text-right text-gray-500">
<caption class="p-5 text-lg font-semibold text-left rtl:text-right text-gray-900 bg-white">
List of Healthchecks
<div class="mt-1 flex">
<div class="mt-1 gap-4 flex justify-between">
<p class="mt-1 text-sm font-normal text-gray-500">
{{ $description }}
</p>
@ -42,9 +42,6 @@
<th scope="col" class="px-6 py-3">
Worker Groups
</th>
<th scope="col" class="px-6 py-3">
Type
</th>
<th scope="col" class="px-6 py-3">
Status
</th>
@ -66,9 +63,6 @@
{{range .WorkerGroups}}
{{ . }}
{{end}}
</td>
<td class="px-6 py-4">
HTTP
</td>
<td class="px-6 py-4">
OK

View file

@ -24,7 +24,7 @@
<table class="w-full text-sm text-left rtl:text-right text-gray-500">
<caption class="p-5 text-lg font-semibold text-left rtl:text-right text-gray-900 bg-white">
List of Workers
<div class="mt-1 flex">
<div class="mt-1 gap-4 flex justify-between">
<p class="mt-1 text-sm font-normal text-gray-500">
{{ $description }}
</p>