refactor: Switching to a cron system for tasks, restructuring service and status models

Added response time field for Status model, set log level from ENV
This commit is contained in:
2025-10-28 00:51:29 +03:00
parent 1958d9b3d9
commit b33df27b31
11 changed files with 254 additions and 63 deletions

10
.env
View File

@@ -1,11 +1,19 @@
APP_HOST=http://localhost:8080
APP_TZ=Europe/Moscow
# panic
# fatal
# error
# warn
# info
# debug
LOG_LEVEL=debug
DATABASE_HOST=localhost
DATABASE_PORT=5444
DATABASE_USER=status
DATABASE_PASS=status
DATABASE_DB=status
DATABASE_TZ=Europe/Moscow
RABBIT_USER=user
RABBIT_PASSWORD=user

5
go.mod
View File

@@ -8,11 +8,14 @@ require (
github.com/go-chi/chi/v5 v5.2.2
github.com/go-chi/jwtauth/v5 v5.3.3
github.com/go-chi/render v1.0.3
github.com/go-co-op/gocron/v2 v2.17.0
github.com/golang-jwt/jwt/v5 v5.3.0
github.com/google/uuid v1.6.0
github.com/joho/godotenv v1.5.1
github.com/lestrrat-go/jwx/v2 v2.1.6
github.com/pressly/goose/v3 v3.24.3
github.com/rabbitmq/amqp091-go v1.10.0
github.com/samber/lo v1.52.0
github.com/sirupsen/logrus v1.9.3
go.uber.org/mock v0.6.0
golang.org/x/crypto v0.41.0
@@ -33,6 +36,7 @@ require (
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jonboulle/clockwork v0.5.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/lestrrat-go/blackmagic v1.0.4 // indirect
github.com/lestrrat-go/httpcc v1.0.1 // indirect
@@ -40,6 +44,7 @@ require (
github.com/lestrrat-go/iter v1.0.2 // indirect
github.com/lestrrat-go/option v1.0.1 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/sethvargo/go-retry v0.3.0 // indirect
go.uber.org/multierr v1.11.0 // indirect

32
go.sum
View File

@@ -8,8 +8,6 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 h1:rpfIENRNNilwHwZeG5+P150SMrnNEcHYvcCuK6dPZSg=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 h1:NMZiJj8QnKe1LgsbDayM4UoHwbvwDRwnI3hwNaAHRnc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0/go.mod h1:ZXNYxsqcloTdSy/rNShjYzMhyjf0LaoftYK0p+A3h40=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
@@ -26,8 +24,8 @@ github.com/go-chi/jwtauth/v5 v5.3.3 h1:50Uzmacu35/ZP9ER2Ht6SazwPsnLQ9LRJy6zTZJpH
github.com/go-chi/jwtauth/v5 v5.3.3/go.mod h1:O4QvPRuZLZghl9WvfVaON+ARfGzpD2PBX/QY5vUz7aQ=
github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4=
github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0=
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/go-co-op/gocron/v2 v2.17.0 h1:e/oj6fcAM8vOOKZxv2Cgfmjo+s8AXC46po5ZPtaSea4=
github.com/go-co-op/gocron/v2 v2.17.0/go.mod h1:Zii6he+Zfgy5W9B+JKk/KwejFOW0kZTFvHtwIpR4aBI=
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
@@ -48,12 +46,12 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/jonboulle/clockwork v0.5.0 h1:Hyh9A8u51kptdkR+cqRpT1EebBwTn1oK9YfGYbdFz6I=
github.com/jonboulle/clockwork v0.5.0/go.mod h1:3mZlmanh0g2NDKO5TWZVJAfofYk64M7XN3SzBPjZF60=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lestrrat-go/blackmagic v1.0.2 h1:Cg2gVSc9h7sz9NOByczrbUvLopQmXrfFx//N+AkAr5k=
github.com/lestrrat-go/blackmagic v1.0.2/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU=
github.com/lestrrat-go/blackmagic v1.0.4 h1:IwQibdnf8l2KoO+qC3uT4OaTWsW7tuRQXy9TRN9QanA=
github.com/lestrrat-go/blackmagic v1.0.4/go.mod h1:6AWFyKNNj0zEXQYfTMPfZrAXUWUfTIZ5ECEUEJaijtw=
github.com/lestrrat-go/httpcc v1.0.1 h1:ydWCStUeJLkpYyjLDHihupbn2tYmZ7m22BGkcvZZrIE=
@@ -62,8 +60,6 @@ github.com/lestrrat-go/httprc v1.0.6 h1:qgmgIRhpvBqexMJjA/PmwSvhNk679oqD1RbovdCG
github.com/lestrrat-go/httprc v1.0.6/go.mod h1:mwwz3JMTPBjHUkkDv/IGJ39aALInZLrhBp0X7KGUZlo=
github.com/lestrrat-go/iter v1.0.2 h1:gMXo1q4c2pHmC3dn8LzRhJfP1ceCbgSiT9lUydIzltI=
github.com/lestrrat-go/iter v1.0.2/go.mod h1:Momfcq3AnRlRjI5b5O8/G5/BvpzrhoFTZcn06fEOPt4=
github.com/lestrrat-go/jwx/v2 v2.1.3 h1:Ud4lb2QuxRClYAmRleF50KrbKIoM1TddXgBrneT5/Jo=
github.com/lestrrat-go/jwx/v2 v2.1.3/go.mod h1:q6uFgbgZfEmQrfJfrCo90QcQOcXFMfbI/fO0NqRtvZo=
github.com/lestrrat-go/jwx/v2 v2.1.6 h1:hxM1gfDILk/l5ylers6BX/Eq1m/pnxe9NBwW6lVfecA=
github.com/lestrrat-go/jwx/v2 v2.1.6/go.mod h1:Y722kU5r/8mV7fYDifjug0r8FK8mZdw0K0GpJw/l8pU=
github.com/lestrrat-go/option v1.0.1 h1:oAzP2fvZGQKWkvHa1/SAcFolBEca1oN+mQ7eooNBEYU=
@@ -83,9 +79,13 @@ github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzuk
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
github.com/samber/lo v1.52.0 h1:Rvi+3BFHES3A8meP33VPAxiBZX/Aws5RxrschYGjomw=
github.com/samber/lo v1.52.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0=
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
@@ -98,33 +98,23 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/mock v0.5.2 h1:LbtPTcP8A5k9WPXj54PPPbjcI4Y6lhyOZXn+VS7wNko=
go.uber.org/mock v0.5.2/go.mod h1:wLlUxC2vVTPTaE3UD51E0BGOAElKrILxhVSDYQLld5o=
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/crypto v0.40.0 h1:r4x+VvoG5Fm+eJcxMaY8CQM7Lb0l1lsmjGBQ6s8BfKM=
golang.org/x/crypto v0.40.0/go.mod h1:Qr1vMER5WyS2dfPHAlsOj01wgLbsyWtFn/aY+5+ZdxY=
golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4=
golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc=
golang.org/x/exp v0.0.0-20250718183923-645b1fa84792 h1:R9PFI6EUdfVKgwKjZef7QIwGcBKu86OEFpJ9nUEP2l4=
golang.org/x/exp v0.0.0-20250718183923-645b1fa84792/go.mod h1:A+z0yzpGtvnG90cToK5n2tu8UJVP2XUATh+r+sfOOOc=
golang.org/x/exp v0.0.0-20250819193227-8b4c13bb791b h1:DXr+pvt3nC887026GRP39Ej11UATqWDmWuS99x26cD0=
golang.org/x/exp v0.0.0-20250819193227-8b4c13bb791b/go.mod h1:4QTo5u+SEIbbKW1RacMZq1YEfOBqeXa19JeshGi+zc4=
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4=
golang.org/x/text v0.27.0/go.mod h1:1D28KMCvyooCX9hBiosv5Tz/+YLxj0j7XhWjpSUF7CU=
golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
@@ -135,8 +125,6 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/postgres v1.6.0 h1:2dxzU8xJ+ivvqTRph34QX+WrRaJlmfyPqXmoGVjMBa4=
gorm.io/driver/postgres v1.6.0/go.mod h1:vUw0mrGgrTK+uPHEhAdV4sfFELrByKVGnaVRkXDhtWo=
gorm.io/gorm v1.30.0 h1:qbT5aPv1UH8gI99OsRlvDToLxW5zR7FzS9acZDOZcgs=
gorm.io/gorm v1.30.0/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=
gorm.io/gorm v1.30.1 h1:lSHg33jJTBxs2mgJRfRZeLDG+WZaHYCk3Wtfl6Ngzo4=
gorm.io/gorm v1.30.1/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=
modernc.org/libc v1.65.0 h1:e183gLDnAp9VJh6gWKdTy0CThL9Pt7MfcR/0bgb7Y1Y=

View File

@@ -0,0 +1,8 @@
-- +goose Up
alter table statuses
add column response_time integer default null;
-- +goose Down
alter table statuses
drop column response_time;

View File

@@ -9,23 +9,29 @@ type HTTPConfig struct {
type ServiceTypeCheckConfig struct {
Version string `json:"version"`
HTTPConfig *HTTPConfig `json:"http_config"`
HTTPConfig *HTTPConfig `json:"httpConfig"`
// MaxFails - max "ping" fails, after with the service marked as unavailable
MaxFails uint8 `json:"maxFails"`
// Interval - interval between "ping" in seconds
Interval uint64 `json:"interval"`
// Timeout - interval after which the task will be canceled
Timeout uint64 `json:"timeout"`
}
type Service struct {
// Unique ID for entity
ID int `gorm:"primary_key;auto_increment" json:"id"`
ID uint64 `gorm:"primary_key;auto_increment" json:"id"`
// Human-readable service name
Name string `gorm:"size:255;not null" json:"name"`
// Human-readable service description
Description string `gorm:"size:255" json:"description"`
PublicDescription string `gorm:"size:255" json:"public_description"`
PublicDescription string `gorm:"size:255" json:"publicDescription"`
Public *bool `gorm:"default:false" json:"public"`
// Host to check, for example 192.168.1.44
// Host to check, for example 192.168.1.44 or https://google.com
Host string `gorm:"size:255;not null" json:"host"`
// Type for check, for now is TCP or HTTP
// Type for check, for now is TCP or HTTP or something else
Type string `gorm:"size:255;not null" json:"type"`
TypeConfig *ServiceTypeCheckConfig `gorm:"serializer:json" json:"type_config"`
Config *ServiceTypeCheckConfig `gorm:"serializer:json;column:type_config" json:"typeConfig"`
Statuses []Status `gorm:"foreignkey:ServiceID" json:"statuses"`
}

View File

@@ -15,11 +15,12 @@ const (
)
type Status struct {
ID int `gorm:"primary_key;auto_increment" json:"-"`
ServiceID int `gorm:"one" json:"-"`
ID uint64 `gorm:"primary_key;auto_increment" json:"-"`
ServiceID uint64 `json:"-"`
Status StatusCode `gorm:"size:255;not null" json:"status"`
Description *string `gorm:"size:255" json:"description"`
CreatedAt time.Time `json:"created_at"`
CreatedAt time.Time `json:"createdAt"`
ResponseTime uint64 `json:"responseTime"`
}
func (Status) TableName() string {

View File

@@ -26,7 +26,7 @@ func Connect() (*gorm.DB, error) {
os.Getenv("DATABASE_PASS"),
os.Getenv("DATABASE_DB"),
os.Getenv("DATABASE_PORT"),
os.Getenv("DATABASE_TZ"),
os.Getenv("APP_TZ"),
)
newLogger := logger.New(

View File

@@ -1,6 +1,8 @@
package log
import (
"os"
"strings"
"sync"
"github.com/sirupsen/logrus"
@@ -12,6 +14,8 @@ const (
DATABASE = "database"
CRON = "cron"
QUEUE = "queue"
Observer = "observer"
Scheduler = "scheduler"
RABBIT = "rabbit"
internal = "internal"
)
@@ -42,8 +46,14 @@ func (m *LoggerManager) Put(name string, logger *logrus.Logger) {
m.mu.Lock()
defer m.mu.Unlock()
l, err := logrus.ParseLevel(strings.ToLower(os.Getenv("LOG_LEVEL")))
if err != nil {
l = logrus.ErrorLevel
}
logger.Level = l
if _, ok := m.loggers[name]; ok {
m.loggers[internal].Errorf("Logger with name '%s' already exists. Use PutForce to replace it.", name)
m.loggers[internal].Warnf("Logger with name '%s' already exists. Use PutForce to replace it.", name)
return
}

View File

@@ -0,0 +1,66 @@
package scheduler
import (
"errors"
"os"
"time"
"git.ostiwe.com/ostiwe-com/status/modules/log"
"github.com/go-co-op/gocron/v2"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
var GlobalAppScheduler AppScheduler
type AppScheduler interface {
NewServiceJob(serviceID uint64, jd gocron.JobDefinition, t gocron.Task, opt ...gocron.JobOption) (gocron.Job, error)
StartJobs() error
}
type appScheduler struct {
jobServiceMap map[uint64]uuid.UUID
scheduler gocron.Scheduler
logger *logrus.Logger
}
func NewAppScheduler() (AppScheduler, error) {
log.Global.Put(log.Scheduler, logrus.New())
location, err := time.LoadLocation(os.Getenv("APP_TZ"))
if err != nil {
return nil, err
}
scheduler, err := gocron.NewScheduler(gocron.WithLocation(location))
if err != nil {
return nil, err
}
return &appScheduler{
scheduler: scheduler,
logger: log.Global.Get(log.Scheduler),
jobServiceMap: make(map[uint64]uuid.UUID),
}, nil
}
func (s *appScheduler) NewServiceJob(serviceID uint64, jd gocron.JobDefinition, t gocron.Task, opt ...gocron.JobOption) (gocron.Job, error) {
job, err := s.scheduler.NewJob(jd, t, opt...)
if err != nil {
return nil, err
}
s.jobServiceMap[serviceID] = job.ID()
return job, nil
}
func (s *appScheduler) StartJobs() error {
if s.scheduler == nil {
return errors.New("scheduler is nil")
}
s.scheduler.Start()
return nil
}

View File

@@ -9,6 +9,7 @@ import (
type Status interface {
Add(ctx context.Context, status model.Status) error
LastNStatuses(ctx context.Context, serviceID uint64, n uint) ([]model.Status, error)
}
type status struct {
@@ -24,3 +25,14 @@ func NewStatusRepository() Status {
func (s status) Add(ctx context.Context, status model.Status) error {
return s.db.WithContext(ctx).Create(&status).Error
}
func (s status) LastNStatuses(ctx context.Context, serviceID uint64, n uint) ([]model.Status, error) {
var items = make([]model.Status, 0, n)
return items, s.db.
WithContext(ctx).
Limit(int(n)).
Order("created_at DESC").
Find(&items, "service_id = ?", serviceID).
Error
}

View File

@@ -3,67 +3,154 @@ package service
import (
"context"
"errors"
"fmt"
"net/http"
"time"
"git.ostiwe.com/ostiwe-com/status/model"
"git.ostiwe.com/ostiwe-com/status/modules/log"
"git.ostiwe.com/ostiwe-com/status/modules/scheduler"
"git.ostiwe.com/ostiwe-com/status/repository"
"github.com/go-co-op/gocron/v2"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
)
var (
httpObserveFailed = errors.New("http observe fail")
httpObserveMaxTries = errors.New("http observe max tries")
GlobalCheckService Check
)
func init() {
log.Global.Put(log.CRON, logrus.New())
log.Global.Put(log.Observer, logrus.New())
}
type Check interface {
Observe(ctx context.Context, srv *model.Service) error
RegisterStatus(ctx context.Context, serviceID int, status model.StatusCode) error
RegisterStatus(ctx context.Context, serviceID uint64, status model.StatusCode, meta ResponseMeta) error
RegisterTasks(ctx context.Context) error
}
type check struct {
serviceRepository repository.Service
statusRepository repository.Status
appScheduler scheduler.AppScheduler
}
func NewCheck() Check {
func NewCheck(appScheduler scheduler.AppScheduler) Check {
return &check{
serviceRepository: repository.NewServiceRepository(),
statusRepository: repository.NewStatusRepository(),
appScheduler: appScheduler,
}
}
func (c check) Observe(ctx context.Context, srv *model.Service) error {
return c.observeHttp(srv)
func (c *check) RegisterTasks(ctx context.Context) error {
services, err := c.serviceRepository.All(ctx, -1, 0, false)
if err != nil {
return err
}
func (c check) RegisterStatus(ctx context.Context, serviceID int, status model.StatusCode) error {
for _, service := range services {
job, jobCreateErr := c.appScheduler.NewServiceJob(
service.ID,
gocron.DurationJob(time.Duration(service.Config.Interval)*time.Second),
gocron.NewTask(c.Observe, ctx, &service),
gocron.WithName(fmt.Sprintf("task-service-%d", service.ID)),
)
if jobCreateErr != nil {
return jobCreateErr
}
log.Global.Get(log.CRON).Infof(
"Registered service observe task for service with id - %d; task id - UUID: %s; Job interval: %d(s)",
service.ID,
job.ID().String(),
service.Config.Interval,
)
}
return nil
}
func (c *check) Observe(ctx context.Context, srv *model.Service) error {
// If the observation of an HTTP service failed, check the last N statuses.
// If the first one has a failure status, set it as failed.
// If all statuses in the last N are warning, set it as failed.
// Otherwise, set the status as warning.
if srv.Type == "http" {
meta, err := c.ObserveHttp(ctx, srv)
if err == nil {
return c.RegisterStatus(ctx, srv.ID, model.StatusOK, *meta)
}
lastNStatuses, err := c.statusRepository.LastNStatuses(ctx, srv.ID, uint(srv.Config.MaxFails))
if err != nil {
return err
}
if len(lastNStatuses) > 0 && lastNStatuses[0].Status == model.StatusFailed {
return c.RegisterStatus(ctx, srv.ID, model.StatusFailed, *meta)
}
everyWarn := lo.EveryBy(lastNStatuses, func(item model.Status) bool {
return item.Status == model.StatusWarn
})
if everyWarn {
return c.RegisterStatus(ctx, srv.ID, model.StatusFailed, *meta)
}
return c.RegisterStatus(ctx, srv.ID, model.StatusWarn, *meta)
}
// Todo: return err
return nil
}
func (c *check) RegisterStatus(ctx context.Context, serviceID uint64, status model.StatusCode, meta ResponseMeta) error {
return c.statusRepository.Add(ctx, model.Status{
ServiceID: serviceID,
Status: status,
ResponseTime: meta.ResponseTime,
CreatedAt: time.Now(),
})
}
func (c check) observeHttp(service *model.Service) error {
if service.TypeConfig == nil || service.TypeConfig.HTTPConfig == nil {
return errors.New("service has no config for http")
type ResponseMeta struct {
ResponseTime uint64
}
conf := service.TypeConfig.HTTPConfig
func (c *check) ObserveHttp(ctx context.Context, service *model.Service) (*ResponseMeta, error) {
var meta = &ResponseMeta{
ResponseTime: 0,
}
var startTime = time.Now()
defer func() {
meta.ResponseTime = uint64(time.Since(startTime).Milliseconds())
}()
if service.Config == nil || service.Config.HTTPConfig == nil {
return meta, errors.New("service has no config for http")
}
logger := log.Global.Get(log.Observer)
logger.Debugf("Start observe service - %d", service.ID)
conf := service.Config.HTTPConfig
client := &http.Client{
Timeout: time.Second * 5,
Timeout: time.Duration(service.Config.Timeout) * time.Second,
}
request, err := http.NewRequest(conf.Method, service.Host, nil)
request, err := http.NewRequestWithContext(ctx, conf.Method, service.Host, nil)
if err != nil {
return err
return meta, err
}
for s := range conf.Headers {
@@ -71,13 +158,13 @@ func (c check) observeHttp(service *model.Service) error {
}
response, err := client.Do(request)
if err != nil && !errors.Is(err, context.DeadlineExceeded) {
return err
if err != nil {
return meta, err
}
if response.StatusCode != http.StatusOK {
return httpObserveFailed
return meta, httpObserveFailed
}
return nil
return meta, nil
}