Skip to content

Commit 31f7d43

Browse files
committed
chore: fix cron cache
1 parent c1c68d5 commit 31f7d43

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

echo/debug.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ import (
1515
"github.com/flanksource/duty/context"
1616
"github.com/google/gops/agent"
1717
"github.com/labstack/echo/v4"
18+
cmap "github.com/orcaman/concurrent-map/v2"
1819
"github.com/robfig/cron/v3"
1920
)
2021

21-
var Crons []*cron.Cron
22+
var Crons = cmap.New[*cron.Cron]()
2223

2324
func RegisterCron(cron *cron.Cron) {
24-
Crons = append(Crons, cron)
25+
26+
// Cache cron objects by their pointer
27+
Crons.SetIfAbsent(fmt.Sprintf("%p", cron), cron)
2528
}
2629

2730
func init() {
@@ -115,8 +118,8 @@ func AddDebugHandlers(e *echo.Echo, rbac echo.MiddlewareFunc) {
115118
debug.POST("/cron/run", func(c echo.Context) error {
116119
name := c.Request().FormValue("name")
117120
names := []string{}
118-
for _, cron := range Crons {
119-
for _, e := range cron.Entries() {
121+
for entry := range Crons.IterBuffered() {
122+
for _, e := range entry.Val.Entries() {
120123
entry := toEntry(&e)
121124
names = append(names, entry.GetName())
122125
if entry.GetName() == name {
@@ -172,8 +175,8 @@ func toEntry(e *cron.Entry) JobCronEntry {
172175
func CronDetailsHandler() func(c echo.Context) error {
173176
return func(c echo.Context) error {
174177
var entries []JobCronEntry
175-
for _, cron := range Crons {
176-
for _, e := range cron.Entries() {
178+
for entry := range Crons.IterBuffered() {
179+
for _, e := range entry.Val.Entries() {
177180
entries = append(entries, toEntry(&e))
178181
}
179182
}

0 commit comments

Comments
 (0)