feat(stats): rewrite scheduled tasks runner, use timescaledb instead of influxdb
This commit is contained in:
47
scheduled_tasks/main.go
Normal file
47
scheduled_tasks/main.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.Println("connecting to databases")
|
||||
connect_dbs()
|
||||
|
||||
log.Println("starting scheduled tasks runner")
|
||||
wait_until_next_minute()
|
||||
go doforever(time.Minute*10, withtime("message stats updater", update_db_message_meta))
|
||||
doforever(time.Minute, withtime("scheduled tasks", task_main))
|
||||
}
|
||||
|
||||
func wait_until_next_minute() {
|
||||
now := time.Now().UTC().Add(time.Minute)
|
||||
after := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), 0, 0, time.UTC)
|
||||
time.Sleep(after.Sub(time.Now().UTC()))
|
||||
}
|
||||
|
||||
func get_env_var(key string) string {
|
||||
if val, ok := os.LookupEnv(key); ok {
|
||||
return val
|
||||
}
|
||||
panic(fmt.Errorf("missing `%s` in environment", key))
|
||||
}
|
||||
|
||||
func withtime(name string, todo func()) func() {
|
||||
return func() {
|
||||
timeBefore := time.Now()
|
||||
todo()
|
||||
timeAfter := time.Now()
|
||||
log.Println("ran", name, "in", timeAfter.Sub(timeBefore).String())
|
||||
}
|
||||
}
|
||||
|
||||
func doforever(dur time.Duration, todo func()) {
|
||||
for {
|
||||
go todo()
|
||||
time.Sleep(dur)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user