Skip to content

Commit

Permalink
Make tibber2mqtt more resilient
Browse files Browse the repository at this point in the history
  • Loading branch information
joehil committed Mar 11, 2024
1 parent c639b94 commit 3fd4c5d
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions tibber2mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"syscall"
Expand All @@ -22,6 +23,7 @@ import (
)

var do_trace bool = true
var bPanic bool = false

var ownlog string
var jsonpath string
Expand All @@ -38,8 +40,6 @@ var mqttport string
var mclient mqtt.Client
var opts = mqtt.NewClientOptions()

var start_time time.Time

//var elapsed time.Duration

type headerRoundTripper struct {
Expand Down Expand Up @@ -85,7 +85,11 @@ func main() {
if token := mclient.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
getTibberPricesNew()
// getTibberPricesNew()
safeRun(getTibberPricesNew)
if bPanic {
panic("Program abend")
}
os.Exit(0)
}
if a1 == "subPower" {
Expand Down Expand Up @@ -688,3 +692,29 @@ func fileExists(filename string) bool {
}
return !info.IsDir()
}

func printStackTrace() {
buffer := make([]byte, 1024)
for {
n := runtime.Stack(buffer, false)
if n < len(buffer) {
buffer = buffer[:n]
break
}
buffer = make([]byte, len(buffer)*2)
}
fmt.Printf("Stack trace:\n%s\n", buffer)
}

func safeRun(f func()) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from panic:", r)
printStackTrace()
fmt.Println("Wait for a minute")
time.Sleep(time.Minute)
bPanic = true
}
}()
f()
}

0 comments on commit 3fd4c5d

Please sign in to comment.