feat: added a cron service

This commit is contained in:
Derrick Hammer 2024-01-19 15:46:37 -05:00
parent 5323e43bdb
commit 0eb6a9a3a3
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 87 additions and 7 deletions

View File

@ -4,11 +4,13 @@ import (
"crypto/ed25519"
"git.lumeweb.com/LumeWeb/portal/account"
"git.lumeweb.com/LumeWeb/portal/api"
"git.lumeweb.com/LumeWeb/portal/cron"
"git.lumeweb.com/LumeWeb/portal/db"
"git.lumeweb.com/LumeWeb/portal/interfaces"
"git.lumeweb.com/LumeWeb/portal/protocols"
"git.lumeweb.com/LumeWeb/portal/storage"
"github.com/casbin/casbin/v2"
"github.com/go-co-op/gocron/v2"
"github.com/spf13/viper"
"go.uber.org/zap"
"gorm.io/gorm"
@ -29,13 +31,7 @@ type PortalImpl struct {
database interfaces.Database
casbin *casbin.Enforcer
accounts interfaces.AccountService
}
func (p *PortalImpl) DatabaseService() interfaces.Database {
return p.database
}
func (p *PortalImpl) Database() *gorm.DB {
return p.database.Get()
cron interfaces.CronService
}
func NewPortal() interfaces.Portal {
@ -49,13 +45,29 @@ func NewPortal() interfaces.Portal {
storageServ := storage.NewStorageService(portal)
database := db.NewDatabase(portal)
accountService := account.NewAccountService(portal)
cronService := cron.NewCronServiceImpl(portal)
portal.storage = storageServ
portal.database = database
portal.accounts = accountService
portal.cron = cronService
return portal
}
func (p *PortalImpl) DatabaseService() interfaces.Database {
return p.database
}
func (p *PortalImpl) Database() *gorm.DB {
return p.database.Get()
}
func (p *PortalImpl) Cron() gocron.Scheduler {
return p.cron.Scheduler()
}
func (p *PortalImpl) CronService() interfaces.CronService {
return p.cron
}
func (p *PortalImpl) Initialize() error {
for _, initFunc := range getInitList() {
if err := initFunc(p); err != nil {

52
cron/cron.go Normal file
View File

@ -0,0 +1,52 @@
package cron
import (
"git.lumeweb.com/LumeWeb/portal/interfaces"
"go.uber.org/zap"
"github.com/go-co-op/gocron/v2"
)
var (
_ interfaces.CronService = (*CronServiceImpl)(nil)
)
type CronServiceImpl struct {
scheduler gocron.Scheduler
services []interfaces.CronableService
portal interfaces.Portal
}
func (c *CronServiceImpl) Scheduler() gocron.Scheduler {
return c.scheduler
}
func NewCronServiceImpl(portal interfaces.Portal) interfaces.CronService {
return &CronServiceImpl{}
}
func (c *CronServiceImpl) Init() error {
s, err := gocron.NewScheduler()
if err != nil {
return err
}
c.scheduler = s
return nil
}
func (c *CronServiceImpl) Start() error {
for _, service := range c.services {
err := service.LoadInitialTasks(c)
if err != nil {
c.portal.Logger().Fatal("Failed to load initial tasks for service", zap.Error(err))
}
}
return nil
}
func (c *CronServiceImpl) RegisterService(service interfaces.CronableService) {
c.services = append(c.services, service)
}

13
interfaces/cron.go Normal file
View File

@ -0,0 +1,13 @@
package interfaces
import "github.com/go-co-op/gocron/v2"
type CronService interface {
Scheduler() gocron.Scheduler
Service
}
type CronableService interface {
LoadInitialTasks(cron CronService) error
Service
}

View File

@ -3,6 +3,7 @@ package interfaces
import (
"crypto/ed25519"
"github.com/casbin/casbin/v2"
"github.com/go-co-op/gocron/v2"
"github.com/spf13/viper"
"go.uber.org/zap"
"gorm.io/gorm"
@ -24,4 +25,6 @@ type Portal interface {
Casbin() *casbin.Enforcer
SetCasbin(e *casbin.Enforcer)
Accounts() AccountService
CronService() CronService
Cron() gocron.Scheduler
}