37 lines
773 B
Go
37 lines
773 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"os"
|
||
|
"runtime/pprof"
|
||
|
"sync"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func dumpMemoryProfile(log *log.Logger) {
|
||
|
path := fmt.Sprintf("%s/hswro_alarm_bot_%s.mprof", os.Getenv("RUNTIME_DIRECTORY"), time.Now().Format(time.RFC3339))
|
||
|
f, err := os.Create(path)
|
||
|
if err != nil {
|
||
|
log.Print("Error dumping memory profile to file: ", err)
|
||
|
}
|
||
|
pprof.WriteHeapProfile(f)
|
||
|
f.Close()
|
||
|
log.Print("Dumped memory profile to: ", path)
|
||
|
}
|
||
|
|
||
|
func WriteMemoryProfilePeriodically(wg *sync.WaitGroup, log *log.Logger, close <-chan interface{}) {
|
||
|
go func() {
|
||
|
wg.Add(1)
|
||
|
defer wg.Done()
|
||
|
memoryProfileTicker := time.NewTicker(24 * time.Hour)
|
||
|
defer memoryProfileTicker.Stop()
|
||
|
select {
|
||
|
case <-memoryProfileTicker.C:
|
||
|
dumpMemoryProfile(log)
|
||
|
case <-close:
|
||
|
return
|
||
|
}
|
||
|
}()
|
||
|
}
|