generated from daystram/go-gin-gorm-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
44 lines (39 loc) · 1.08 KB
/
scheduler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package handlers
import (
"log"
"time"
"github.com/robfig/cron/v3"
"gorm.io/gorm"
"github.com/daystram/audit/audit-be/constants"
"github.com/daystram/audit/audit-be/models"
"github.com/daystram/audit/audit-be/utils"
"github.com/daystram/audit/proto"
)
func (m *module) InitializeScheduler() (err error) {
m.schedulerCron = cron.New()
if _, err = m.schedulerCron.AddFunc(constants.ShedluerCronSpec, m.TriggerTracking); err != nil {
return err
}
m.schedulerCron.Start()
return
}
func (m *module) TriggerTracking() {
var err error
var services []models.Service
if services, err = m.db.serviceOrmer.GetAllEnabled(); err != nil && err != gorm.ErrRecordNotFound {
log.Println(err)
return
}
for _, service := range services {
if err = m.trackerServer.SendTrackingRequest(&proto.TrackingRequest{
ApplicationId: service.ApplicationID,
ServiceId: service.ID,
Endpoint: service.Endpoint,
Type: utils.ServiceTypeToProto(service.Type),
Config: service.Config,
RequestedAt: time.Now().UnixNano(),
}); err != nil {
log.Println(err)
}
}
}