-
Notifications
You must be signed in to change notification settings - Fork 0
/
svc_eventlog.go
78 lines (69 loc) · 2.28 KB
/
svc_eventlog.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package cron
import (
"github.com/go-co-op/gocron"
"github.com/kataras/iris/v12"
"github.com/kmilodenisglez/drones.restapi/repo/db"
"github.com/kmilodenisglez/drones.restapi/schema"
"github.com/kmilodenisglez/drones.restapi/schema/dto"
"github.com/kmilodenisglez/drones.restapi/service/utils"
"log"
"time"
)
// ISvcEventLog EventLog request service interface
type ISvcEventLog interface {
GetEventLogs() (*[]dto.LogEvent, *dto.Problem)
MeinerCronJob() error
}
type svcEventLogReqs struct {
svcConf *utils.SvcConfig
reposEventLog *db.RepoEventLog
reposDrones *db.RepoDrones
}
// endregion =============================================================================
// NewSvcRepoEventLog instantiate the Drones request services
func NewSvcRepoEventLog(svcConf *utils.SvcConfig) ISvcEventLog {
reposEventLog := db.NewRepoEventLog(svcConf)
reposDrones := db.NewRepoDrones(svcConf)
return &svcEventLogReqs{svcConf, &reposEventLog, &reposDrones}
}
// GetEventLogs get event log
func (e svcEventLogReqs) GetEventLogs() (*[]dto.LogEvent, *dto.Problem) {
logs, err := (*e.reposEventLog).GetEventLogs()
if err != nil {
return nil, dto.NewProblem(iris.StatusExpectationFailed, schema.ErrBuntdb, err.Error())
}
return logs, nil
}
// MeinerCronJob periodic task to check drones battery levels and create history/audit event log for this
func (e svcEventLogReqs) MeinerCronJob() error {
// cron job is started only if it is active in configuration
if e.svcConf.CronEnabled {
log.Printf("schedules a new periodic Job with an interval: %d seconds", e.svcConf.EveryTime)
cron := gocron.NewScheduler(time.UTC)
_, err := cron.Every(e.svcConf.EveryTime).Seconds().WaitForSchedule().Do(e.doFunc)
if err != nil {
return err
}
// starts the scheduler asynchronously
cron.StartAsync()
}
return nil
}
func (e svcEventLogReqs) doFunc() {
log.Println("cron job executing")
// If the drone database has not been populated then the cron is skipped
isPopulated := (*e.reposDrones).IsPopulated()
if !isPopulated {
return
}
// drones are requested to populate the event log database
drones, err := (*e.reposDrones).GetDrones("")
if err != nil || drones == nil {
return
}
err = (*e.reposEventLog).CheckBatteryLevelsDrones(drones)
if err != nil {
return
}
log.Println("cron job ending")
}