@@ -3,9 +3,12 @@ package bootstrap
33import (
44 "errors"
55 "fmt"
6+ "time"
67
78 "github.com/hibiken/asynq"
8- comp "github.com/websoft9/appos/backend/domain/components"
9+ "github.com/websoft9/appos/backend/domain/monitor"
10+ swcatalog "github.com/websoft9/appos/backend/domain/software/catalog"
11+ swinventory "github.com/websoft9/appos/backend/domain/software/inventory"
912 "github.com/websoft9/appos/backend/domain/worker"
1013 "github.com/websoft9/appos/backend/infra/cronutil"
1114
@@ -26,7 +29,7 @@ func registerCronHooks(app *pocketbase.PocketBase, asynqClient *asynq.Client) {
2629 componentsInventoryCronJobID ,
2730 "*/15 * * * *" ,
2831 cronutil .Wrap (app , componentsInventoryCronJobID , func () {
29- if err := runComponentsInventoryProbe (); err != nil {
32+ if err := runComponentsInventoryProbe (app ); err != nil {
3033 panic (err )
3134 }
3235 }),
@@ -40,6 +43,9 @@ func registerCronHooks(app *pocketbase.PocketBase, asynqClient *asynq.Client) {
4043 monitorReachabilityCronJobID ,
4144 "*/1 * * * *" ,
4245 cronutil .Wrap (app , monitorReachabilityCronJobID , func () {
46+ if ! shouldRunMonitorInterval (time .Now ().UTC (), monitor .LoadSchedulingSettings (app ).ReachabilityIntervalMinutes ) {
47+ return
48+ }
4349 if err := worker .EnqueueMonitorReachabilitySweep (asynqClient ); err != nil {
4450 panic (err )
4551 }
@@ -50,6 +56,9 @@ func registerCronHooks(app *pocketbase.PocketBase, asynqClient *asynq.Client) {
5056 monitorMetricsFreshnessCronJobID ,
5157 "*/1 * * * *" ,
5258 cronutil .Wrap (app , monitorMetricsFreshnessCronJobID , func () {
59+ if ! shouldRunMonitorInterval (time .Now ().UTC (), monitor .LoadSchedulingSettings (app ).MetricsFreshnessIntervalMinutes ) {
60+ return
61+ }
5362 if err := worker .EnqueueMonitorMetricsFreshness (asynqClient ); err != nil {
5463 panic (err )
5564 }
@@ -60,6 +69,9 @@ func registerCronHooks(app *pocketbase.PocketBase, asynqClient *asynq.Client) {
6069 monitorControlReachabilityCronJobID ,
6170 "*/1 * * * *" ,
6271 cronutil .Wrap (app , monitorControlReachabilityCronJobID , func () {
72+ if ! shouldRunMonitorInterval (time .Now ().UTC (), monitor .LoadSchedulingSettings (app ).ControlReachabilityIntervalMinutes ) {
73+ return
74+ }
6375 if err := worker .EnqueueMonitorControlReachability (asynqClient ); err != nil {
6476 panic (err )
6577 }
@@ -68,8 +80,11 @@ func registerCronHooks(app *pocketbase.PocketBase, asynqClient *asynq.Client) {
6880
6981 app .Cron ().MustAdd (
7082 monitorFactsPullCronJobID ,
71- "*/15 * * * *" ,
83+ "*/1 * * * *" ,
7284 cronutil .Wrap (app , monitorFactsPullCronJobID , func () {
85+ if ! shouldRunMonitorInterval (time .Now ().UTC (), monitor .LoadSchedulingSettings (app ).FactsPullIntervalMinutes ) {
86+ return
87+ }
7388 if err := worker .EnqueueMonitorFactsPull (asynqClient ); err != nil {
7489 panic (err )
7590 }
@@ -80,6 +95,9 @@ func registerCronHooks(app *pocketbase.PocketBase, asynqClient *asynq.Client) {
8095 monitorRuntimeSnapshotPullCronJobID ,
8196 "*/1 * * * *" ,
8297 cronutil .Wrap (app , monitorRuntimeSnapshotPullCronJobID , func () {
98+ if ! shouldRunMonitorInterval (time .Now ().UTC (), monitor .LoadSchedulingSettings (app ).RuntimeSnapshotIntervalMinutes ) {
99+ return
100+ }
83101 if err := worker .EnqueueMonitorRuntimeSnapshotPull (asynqClient ); err != nil {
84102 panic (err )
85103 }
@@ -88,8 +106,11 @@ func registerCronHooks(app *pocketbase.PocketBase, asynqClient *asynq.Client) {
88106
89107 app .Cron ().MustAdd (
90108 monitorCredentialCronJobID ,
91- "*/5 * * * *" ,
109+ "*/1 * * * *" ,
92110 cronutil .Wrap (app , monitorCredentialCronJobID , func () {
111+ if ! shouldRunMonitorInterval (time .Now ().UTC (), monitor .LoadSchedulingSettings (app ).CredentialSweepIntervalMinutes ) {
112+ return
113+ }
93114 if err := worker .EnqueueMonitorCredentialSweep (asynqClient ); err != nil {
94115 panic (err )
95116 }
@@ -100,28 +121,40 @@ func registerCronHooks(app *pocketbase.PocketBase, asynqClient *asynq.Client) {
100121 monitorAppHealthCronJobID ,
101122 "*/1 * * * *" ,
102123 cronutil .Wrap (app , monitorAppHealthCronJobID , func () {
124+ if ! shouldRunMonitorInterval (time .Now ().UTC (), monitor .LoadSchedulingSettings (app ).AppHealthIntervalMinutes ) {
125+ return
126+ }
103127 if err := worker .EnqueueMonitorAppHealthSweep (asynqClient ); err != nil {
104128 panic (err )
105129 }
106130 }),
107131 )
108132}
109133
110- func runComponentsInventoryProbe () error {
111- registry , err := comp .LoadRegistry ()
134+ func shouldRunMonitorInterval (now time.Time , intervalMinutes int ) bool {
135+ if intervalMinutes <= 1 {
136+ return true
137+ }
138+ now = now .UTC ()
139+ totalMinutes := now .Hour ()* 60 + now .Minute ()
140+ return totalMinutes % intervalMinutes == 0
141+ }
142+
143+ func runComponentsInventoryProbe (app * pocketbase.PocketBase ) error {
144+ registry , err := swcatalog .LoadLocalRegistry ()
112145 if err != nil {
113146 return err
114147 }
115148
116149 var probeErrors []error
117150 for _ , component := range registry .EnabledComponents () {
118- if _ , err := comp .DetectVersion (component .VersionProbe ); err != nil {
151+ if _ , err := swinventory .DetectVersion (app , component .VersionProbe ); err != nil {
119152 probeErrors = append (probeErrors , fmt .Errorf ("%s version probe: %w" , component .ID , err ))
120153 }
121- if _ , err := comp .CheckAvailability (component .AvailabilityProbe ); err != nil {
154+ if _ , err := swinventory .CheckAvailability (app , component .AvailabilityProbe ); err != nil {
122155 probeErrors = append (probeErrors , fmt .Errorf ("%s availability probe: %w" , component .ID , err ))
123156 }
124- _ = comp .DetectUpdateTime (component .UpdateProbe )
157+ _ = swinventory .DetectUpdateTime (component .UpdateProbe )
125158 }
126159
127160 return errors .Join (probeErrors ... )
0 commit comments