Browse Source

updated to log errors

0xdcarns 3 years ago
parent
commit
0874ad1cd8
2 changed files with 39 additions and 31 deletions
  1. 7 7
      logic/telemetry.go
  2. 32 24
      logic/timer.go

+ 7 - 7
logic/telemetry.go

@@ -17,28 +17,28 @@ const posthog_pub_key = "phc_1vEXhPOA1P7HP5jP2dVU9xDTUqXHAelmtravyZ1vvES"
 const posthog_endpoint = "https://app.posthog.com"
 const posthog_endpoint = "https://app.posthog.com"
 
 
 // sendTelemetry - gathers telemetry data and sends to posthog
 // sendTelemetry - gathers telemetry data and sends to posthog
-func sendTelemetry() {
+func sendTelemetry() error {
 	if servercfg.Telemetry() == "off" {
 	if servercfg.Telemetry() == "off" {
-		return
+		return nil
 	}
 	}
 
 
 	var telRecord, err = fetchTelemetryRecord()
 	var telRecord, err = fetchTelemetryRecord()
 	if err != nil {
 	if err != nil {
-		return
+		return err
 	}
 	}
 	// get telemetry data
 	// get telemetry data
 	d, err := fetchTelemetryData()
 	d, err := fetchTelemetryData()
 	if err != nil {
 	if err != nil {
-		return
+		return err
 	}
 	}
 	client, err := posthog.NewWithConfig(posthog_pub_key, posthog.Config{Endpoint: posthog_endpoint})
 	client, err := posthog.NewWithConfig(posthog_pub_key, posthog.Config{Endpoint: posthog_endpoint})
 	if err != nil {
 	if err != nil {
-		return
+		return err
 	}
 	}
 	defer client.Close()
 	defer client.Close()
 
 
 	// send to posthog
 	// send to posthog
-	client.Enqueue(posthog.Capture{
+	return client.Enqueue(posthog.Capture{
 		DistinctId: telRecord.UUID,
 		DistinctId: telRecord.UUID,
 		Event:      "daily checkin",
 		Event:      "daily checkin",
 		Properties: posthog.NewProperties().
 		Properties: posthog.NewProperties().
@@ -80,7 +80,7 @@ func setTelemetryTimestamp(uuid string) error {
 		UUID:     uuid,
 		UUID:     uuid,
 		LastSend: lastsend,
 		LastSend: lastsend,
 	}
 	}
-	jsonObj, err := json.Marshal(serverTelData)
+	jsonObj, err := json.Marshal(&serverTelData)
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}

+ 32 - 24
logic/timer.go

@@ -7,40 +7,21 @@ import (
 	"github.com/gravitl/netmaker/logger"
 	"github.com/gravitl/netmaker/logger"
 )
 )
 
 
-// timeHooks - functions to run once a day, functions must take no parameters
-var timeHooks = []interface{}{
-	loggerDump,
-	sendTelemetry,
-}
+// == Constants ==
 
 
-func loggerDump() {
-	logger.DumpFile(fmt.Sprintf("data/netmaker.log.%s", time.Now().Format(logger.TimeFormatDay)))
-}
-
-// TIMER_HOURS_BETWEEN_RUN - How long to wait before sending telemetry to server (24 hours)
-const TIMER_HOURS_BETWEEN_RUN = 24
-
-// AddHook - adds a hook function to run every 24hrs
-func AddHook(ifaceToAdd interface{}) {
-	timeHooks = append(timeHooks, ifaceToAdd)
-}
+// How long to wait before sending telemetry to server (24 hours)
+const timer_hours_between_runs = 24
 
 
-// runHooks - runs the functions currently in the timeHooks data structure
-func runHooks() {
-	for _, hook := range timeHooks {
-		hook.(func())()
-	}
-}
+// == Public ==
 
 
 // TimerCheckpoint - Checks if 24 hours has passed since telemetry was last sent. If so, sends telemetry data to posthog
 // TimerCheckpoint - Checks if 24 hours has passed since telemetry was last sent. If so, sends telemetry data to posthog
 func TimerCheckpoint() error {
 func TimerCheckpoint() error {
-
 	// get the telemetry record in the DB, which contains a timestamp
 	// get the telemetry record in the DB, which contains a timestamp
 	telRecord, err := fetchTelemetryRecord()
 	telRecord, err := fetchTelemetryRecord()
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
-	sendtime := time.Unix(telRecord.LastSend, 0).Add(time.Hour * time.Duration(TIMER_HOURS_BETWEEN_RUN))
+	sendtime := time.Unix(telRecord.LastSend, 0).Add(time.Hour * time.Duration(timer_hours_between_runs))
 	// can set to 2 minutes for testing
 	// can set to 2 minutes for testing
 	// sendtime := time.Unix(telRecord.LastSend, 0).Add(time.Minute * 2)
 	// sendtime := time.Unix(telRecord.LastSend, 0).Add(time.Minute * 2)
 	enoughTimeElapsed := time.Now().After(sendtime)
 	enoughTimeElapsed := time.Now().After(sendtime)
@@ -52,3 +33,30 @@ func TimerCheckpoint() error {
 	// set telemetry timestamp for server, restarts 24 hour cycle
 	// set telemetry timestamp for server, restarts 24 hour cycle
 	return setTelemetryTimestamp(telRecord.UUID)
 	return setTelemetryTimestamp(telRecord.UUID)
 }
 }
+
+// AddHook - adds a hook function to run every 24hrs
+func AddHook(ifaceToAdd interface{}) {
+	timeHooks = append(timeHooks, ifaceToAdd)
+}
+
+// == private ==
+
+// timeHooks - functions to run once a day, functions must take no parameters
+var timeHooks = []interface{}{
+	loggerDump,
+	sendTelemetry,
+}
+
+func loggerDump() error {
+	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 {
+			logger.Log(1, "error occurred when running timer function:", err.Error())
+		}
+	}
+}