From d22aca00b5c2db5835f2457bebb5e28ca756991b Mon Sep 17 00:00:00 2001 From: ostiwe Date: Sun, 10 Aug 2025 22:16:18 +0300 Subject: [PATCH] refactor(observer): Refactor observer service --- service/check.go | 124 ++++++----------------------------------------- 1 file changed, 16 insertions(+), 108 deletions(-) diff --git a/service/check.go b/service/check.go index 58e6e1f..5447dd5 100644 --- a/service/check.go +++ b/service/check.go @@ -3,11 +3,7 @@ package service import ( "context" "errors" - "net" "net/http" - "net/url" - "slices" - "sync" "time" "git.ostiwe.com/ostiwe-com/status/model" @@ -26,109 +22,35 @@ func init() { } type Check interface { - Start(interval time.Duration) + Observe(ctx context.Context, srv *model.Service) error + RegisterStatus(ctx context.Context, serviceID int, status model.StatusCode) error } type check struct { - serviceRepository repository.Service - maxServicesPerRoutine int - statusRepository repository.Status + serviceRepository repository.Service + statusRepository repository.Status } -func NewCheck(maxServicesPerRoutine int) Check { - if maxServicesPerRoutine <= 0 { - maxServicesPerRoutine = 15 - } - +func NewCheck() Check { return &check{ - maxServicesPerRoutine: maxServicesPerRoutine, - serviceRepository: repository.NewServiceRepository(), - statusRepository: repository.NewStatusRepository(), + serviceRepository: repository.NewServiceRepository(), + statusRepository: repository.NewStatusRepository(), } } -func (c check) Start(interval time.Duration) { - for { - time.Sleep(interval) - - services, err := c.serviceRepository.All(context.Background(), -1, 0, false) - if err != nil { - log.Global.Get(log.CRON).Error(err) - - continue - } - - wg := new(sync.WaitGroup) - - chunked := slices.Chunk(services, c.maxServicesPerRoutine) - - for i := range chunked { - wg.Add(1) - - go c.observeChunk(wg, i) - } - - wg.Wait() - - } +func (c check) Observe(ctx context.Context, srv *model.Service) error { + return c.observeHttp(srv) } -func (c check) observeChunk(wg *sync.WaitGroup, chunk []model.Service) { - defer wg.Done() - - for _, item := range chunk { - switch item.Type { - case "http": - err := c.observeHttp(item, 0) - if err == nil { - addErr := c.statusRepository.Add(model.Status{ - ServiceID: item.ID, - Status: model.StatusOK, - }) - if addErr != nil { - log.Global.Get(log.CRON).Error("Add status to status repository fail", item.ID, addErr) - } - - continue - } - - if errors.Is(err, httpObserveFailed) { - addErr := c.statusRepository.Add(model.Status{ - ServiceID: item.ID, - Status: model.StatusFailed, - }) - if addErr != nil { - log.Global.Get(log.CRON).Error("Add status to status repository fail", item.ID, addErr) - } - - continue - } - - log.Global.Get(log.CRON).Error("Unexpected observe fail", item.ID, err) - - break - case "tcp": - log.Global.Get(log.CRON).Warn("Cannot handle service with id: ", item.ID, ", because a tcp observer not implemented.") - continue - } - } +func (c check) RegisterStatus(ctx context.Context, serviceID int, status model.StatusCode) error { + return c.statusRepository.Add(ctx, model.Status{ + ServiceID: serviceID, + Status: status, + CreatedAt: time.Now(), + }) } -func (c check) observeHttp(service model.Service, attempt int) error { - if attempt >= 20 { - return httpObserveMaxTries - } - - if attempt > 5 { - err := c.statusRepository.Add(model.Status{ - ServiceID: service.ID, - Status: model.StatusWarn, - }) - if err != nil { - return err - } - } - +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") } @@ -150,23 +72,9 @@ func (c check) observeHttp(service model.Service, attempt int) error { response, err := client.Do(request) if err != nil && !errors.Is(err, context.DeadlineExceeded) { - var ( - e *url.Error - opErr *net.OpError - ) - if errors.As(err, &e) && errors.As(e.Err, &opErr) { - time.Sleep(2 * time.Second) - - return c.observeHttp(service, attempt+1) - } - return err } - if errors.Is(err, context.DeadlineExceeded) { - return c.observeHttp(service, attempt+1) - } - if response.StatusCode != http.StatusOK { return httpObserveFailed }