mirror of
https://github.com/tiennm99/keepalive.git
synced 2026-10-11 03:13:31 +00:00
feat(config): support multi-service yaml config
This commit is contained in:
1 parent
655db8dcbf
commit
f3a23ee91e
19 files changed
+709
-268
No files matched your search
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/tiennm99/keepalive/adapter"
|
||||
)
|
||||
|
||||
type runningService struct {
|
||||
config serviceConfig
|
||||
adapter adapter.Adapter
|
||||
}
|
||||
|
||||
func runService(ctx context.Context, wg *sync.WaitGroup, svc runningService) {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
ticker := time.NewTicker(svc.config.Interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
tickCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
count, err := svc.adapter.Increment(tickCtx)
|
||||
cancel()
|
||||
if err != nil {
|
||||
log.Printf("[%s] increment: %v", svc.config.Name, err)
|
||||
continue
|
||||
}
|
||||
log.Printf("[%s] counter: %d", svc.config.Name, count)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func closeServices(services []runningService) {
|
||||
for _, svc := range services {
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
if err := svc.adapter.Close(shutdownCtx); err != nil {
|
||||
log.Printf("[%s] close: %v", svc.config.Name, err)
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user