From 08b72cdf10c66449b9d586e622a81c343d2d658d Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 4 May 2022 19:59:50 +0200 Subject: [PATCH] go:embed LUA script for the sake of syntax highlighting. --- pkg/icingadb/overdue/get_overdues.lua | 30 ++++++++++++++++++++ pkg/icingadb/overdue/sync.go | 40 ++++++--------------------- 2 files changed, 38 insertions(+), 32 deletions(-) create mode 100644 pkg/icingadb/overdue/get_overdues.lua diff --git a/pkg/icingadb/overdue/get_overdues.lua b/pkg/icingadb/overdue/get_overdues.lua new file mode 100644 index 00000000..328209d2 --- /dev/null +++ b/pkg/icingadb/overdue/get_overdues.lua @@ -0,0 +1,30 @@ +-- get_overdues.lua takes the following KEYS: +-- * either icinga:nextupdate:host or icinga:nextupdate:service +-- * either icingadb:overdue:host or icingadb:overdue:service +-- * a random one +-- +-- It takes the following ARGV: +-- * the current date and time as *nix timestamp float in seconds +-- +-- It returns the following: +-- * overdue monitored objects not yet marked overdue +-- * not overdue monitored objects not yet unmarked overdue + +local icingaNextupdate = KEYS[1] +local icingadbOverdue = KEYS[2] +local tempOverdue = KEYS[3] +local now = ARGV[1] + +redis.call('DEL', tempOverdue) + +local zrbs = redis.call('ZRANGEBYSCORE', icingaNextupdate, '-inf', '(' .. now) +for i = 1, #zrbs do + redis.call('SADD', tempOverdue, zrbs[i]) +end +zrbs = nil + +local res = { redis.call('SDIFF', tempOverdue, icingadbOverdue), redis.call('SDIFF', icingadbOverdue, tempOverdue) } + +redis.call('DEL', tempOverdue) + +return res diff --git a/pkg/icingadb/overdue/sync.go b/pkg/icingadb/overdue/sync.go index 0c0a7dbe..d8ba24bd 100644 --- a/pkg/icingadb/overdue/sync.go +++ b/pkg/icingadb/overdue/sync.go @@ -2,6 +2,7 @@ package overdue import ( "context" + _ "embed" "fmt" "github.com/go-redis/redis/v8" "github.com/google/uuid" @@ -16,7 +17,9 @@ import ( "github.com/icinga/icingadb/pkg/periodic" "github.com/pkg/errors" "golang.org/x/sync/errgroup" + "regexp" "strconv" + "strings" "time" ) @@ -129,39 +132,12 @@ func (s Sync) log(ctx context.Context, objectType string, counter *com.Counter) }) } -// luaGetOverdues takes the following KEYS: -// * either icinga:nextupdate:host or icinga:nextupdate:service -// * either icingadb:overdue:host or icingadb:overdue:service -// * a random one -// -// It takes the following ARGV: -// * the current date and time as *nix timestamp float in seconds -// -// It returns the following: -// * overdue monitored objects not yet marked overdue -// * not overdue monitored objects not yet unmarked overdue -var luaGetOverdues = redis.NewScript(` +//go:embed get_overdues.lua +var getOverduesLua string -local icingaNextupdate = KEYS[1] -local icingadbOverdue = KEYS[2] -local tempOverdue = KEYS[3] -local now = ARGV[1] - -redis.call('DEL', tempOverdue) - -local zrbs = redis.call('ZRANGEBYSCORE', icingaNextupdate, '-inf', '(' .. now) -for i = 1, #zrbs do - redis.call('SADD', tempOverdue, zrbs[i]) -end -zrbs = nil - -local res = {redis.call('SDIFF', tempOverdue, icingadbOverdue), redis.call('SDIFF', icingadbOverdue, tempOverdue)} - -redis.call('DEL', tempOverdue) - -return res - -`) +var luaGetOverdues = redis.NewScript(strings.TrimSpace( + regexp.MustCompile(`(?m)^--.*?$`).ReplaceAllString(getOverduesLua, ""), +)) // sync synchronizes Redis overdue sets from s.redis to s.db for objectType. func (s Sync) sync(ctx context.Context, objectType string, factory factory, counter *com.Counter) error {