Files
identity-service/src/internal/chronos/chronos.go

53 lines
1.0 KiB
Go

package chronos
import (
"log"
"time"
"git.ego.freeddns.org/egommerce/identity-service/common"
"github.com/go-redis/redis/v8"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/onatm/clockwerk"
)
type Chronos struct {
handlers map[string]any
}
func New(c *common.Config) *Chronos {
return &Chronos{
handlers: make(map[string]any),
}
}
func (c *Chronos) Start() error {
job := NewCachePermissionsJob(c)
sch := clockwerk.New()
sch.Every(30 * time.Minute).Do(job)
sch.Start()
return nil
}
func (c *Chronos) RegisterHandler(name string, fn func() any) {
c.handlers[name] = fn()
}
func (c *Chronos) OnShutdown() {
log.Println("Chronos is going down...")
c.GetDatabase().Close()
c.GetCache().Close()
}
// Plugin helper funcitons - refactor needed cause funcs are duplcated in server.go
// TODO: move functions below to some common place
func (c *Chronos) GetCache() *redis.Client {
return (c.handlers["cache"]).(*redis.Client)
}
func (c *Chronos) GetDatabase() *pgxpool.Pool {
return (c.handlers["database"]).(*pgxpool.Pool)
}