netmaker/logic/timer.go
Gabriel de Souza Seibel 922e7dbf2c
[NET-404] Run in limited mode when ee checks fail (#2474)
* Add limited http handlers functionality to rest handler

* Export ee.errValidation (ee.ErrValidation)

* Export a fatal error handled by the hook manager

* Export a new status variable for unlicensed server

* Mark server as unlicensed when ee checks fail

* Handle license validation failures with a (re)boot in a limited state

* Revert "Export a fatal error handled by the hook manager"

This reverts commit 069c21974a8d36e889c73ad78023448d787d62a5.

* Revert "Export ee.errValidation (ee.ErrValidation)"

This reverts commit 59dbab8c79773ca5d879f28cbaf53f3dd4297b9b.

* Revert "Add limited http handlers functionality to rest handler"

This reverts commit e2f1f28facaca54713db76a588839cd2733cf673.

* Revert "Handle license validation failures with a (re)boot in a limited state"

This reverts commit 58cfbbaf522a1345aac1fa67964ebff0a6d60cd8.

* Revert "Mark server as unlicensed when ee checks fail"

This reverts commit 77c6dbdd3c9cfa6e7d6becedef6251e8617ae367.

* Handle license validation failures with a middleware

* Forbid responses if unlicensed ee and not in status api

* Remove unused func
2023-08-03 12:16:58 +05:30

103 lines
2.5 KiB
Go

package logic
import (
"context"
"fmt"
"github.com/gravitl/netmaker/logger"
"golang.org/x/exp/slog"
"sync"
"time"
"github.com/gravitl/netmaker/models"
)
// == Constants ==
// How long to wait before sending telemetry to server (24 hours)
const timer_hours_between_runs = 24
// HookManagerCh - channel to add any new hooks
var HookManagerCh = make(chan models.HookDetails, 2)
// == Public ==
// TimerCheckpoint - Checks if 24 hours has passed since telemetry was last sent. If so, sends telemetry data to posthog
func TimerCheckpoint() error {
// get the telemetry record in the DB, which contains a timestamp
telRecord, err := fetchTelemetryRecord()
if err != nil {
return err
}
sendtime := time.Unix(telRecord.LastSend, 0).Add(time.Hour * time.Duration(timer_hours_between_runs))
// can set to 2 minutes for testing
// sendtime := time.Unix(telRecord.LastSend, 0).Add(time.Minute * 2)
enoughTimeElapsed := time.Now().After(sendtime)
// if more than 24 hours has elapsed, send telemetry to posthog
if enoughTimeElapsed {
// run any time hooks
runHooks()
return setTelemetryTimestamp(&telRecord)
}
return nil
}
// AddHook - adds a hook function to run every 24hrs
func AddHook(ifaceToAdd interface{}) {
timeHooks = append(timeHooks, ifaceToAdd)
}
// StartHookManager - listens on `HookManagerCh` to run any hook
func StartHookManager(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
for {
select {
case <-ctx.Done():
slog.Error("## Stopping Hook Manager")
return
case newhook := <-HookManagerCh:
wg.Add(1)
go addHookWithInterval(ctx, wg, newhook.Hook, newhook.Interval)
}
}
}
func addHookWithInterval(ctx context.Context, wg *sync.WaitGroup, hook func() error, interval time.Duration) {
defer wg.Done()
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if err := hook(); err != nil {
slog.Error(err.Error())
}
}
}
}
// == private ==
// timeHooks - functions to run once a day, functions must take no parameters
var timeHooks = []interface{}{
loggerDump,
sendTelemetry,
}
func loggerDump() error {
// TODO use slog?
logger.DumpFile(fmt.Sprintf("data/netmaker.log.%s", time.Now().Format(logger.TimeFormatDay)))
return nil
}
// runHooks - runs the functions currently in the timeHooks data structure
func runHooks() {
for _, hook := range timeHooks {
if err := hook.(func() error)(); err != nil {
slog.Error("error occurred when running timer function", "error", err.Error())
}
}
}