-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
0d419f6
commit d8d50c8
Showing
10 changed files
with
245 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
DICEDB_ADDR=localhost:7379 | ||
DICEDB_ADMIN_ADDR=localhost:7379 | ||
DICEDB_ADMIN_USERNAME=diceadmin | ||
DICEDB_ADMIN_PASSWORD= | ||
DICEDB_ADDR=localhost:7380 | ||
DICEDB_USERNAME=dice | ||
DICEDB_PASSWORD= | ||
SERVER_PORT=:8080 | ||
IS_TEST_ENVIRONMENT=false | ||
REQUEST_LIMIT_PER_MIN=1000 | ||
REQUEST_WINDOW_SEC=60 | ||
ALLOWED_ORIGINS=http://localhost:3000 | ||
ALLOWED_ORIGINS=http://localhost:3000 | ||
CRON_CLEANUP_FREQUENCY_MINS=15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"log/slog" | ||
"server/internal/db" | ||
"server/internal/server/utils" | ||
"strconv" | ||
"sync" | ||
"time" | ||
|
||
"github.com/dicedb/dicedb-go" | ||
) | ||
|
||
type CleanupManager struct { | ||
diceDBAdminClient *db.DiceDB | ||
diceDBClient *db.DiceDB | ||
cronFrequency time.Duration | ||
} | ||
|
||
func NewCleanupManager(diceDBAdminClient *db.DiceDB, | ||
diceDBClient *db.DiceDB, cronFrequency time.Duration) *CleanupManager { | ||
return &CleanupManager{ | ||
diceDBAdminClient: diceDBAdminClient, | ||
diceDBClient: diceDBClient, | ||
cronFrequency: cronFrequency, | ||
} | ||
} | ||
|
||
func (c *CleanupManager) Run(ctx context.Context, wg *sync.WaitGroup) { | ||
defer wg.Done() | ||
c.start(ctx) | ||
} | ||
|
||
func (c *CleanupManager) start(ctx context.Context) { | ||
ticker := time.NewTicker(c.cronFrequency) | ||
defer ticker.Stop() | ||
|
||
// Get the last cron run time | ||
resp := c.diceDBAdminClient.Client.Get(ctx, utils.LastCronCleanupTimeUnixMs) | ||
if resp.Err() != nil { | ||
if errors.Is(resp.Err(), dicedb.Nil) { | ||
// Default to current time | ||
cleanupTime := strconv.FormatInt(time.Now().UnixMilli(), 10) | ||
slog.Debug("Defaulting last cron cleanup time key since not set", slog.Any("cleanupTime", cleanupTime)) | ||
resp := c.diceDBAdminClient.Client.Set(ctx, utils.LastCronCleanupTimeUnixMs, cleanupTime, -1) | ||
if resp.Err() != nil { | ||
slog.Error("Failed to set default value for last cron cleanup time key", | ||
slog.Any("err", resp.Err().Error())) | ||
} | ||
} else { | ||
slog.Error("Failed to get last cron cleanup time", slog.Any("err", resp.Err().Error())) | ||
} | ||
} | ||
|
||
for { | ||
select { | ||
case <-ticker.C: | ||
c.runCronTasks() | ||
case <-ctx.Done(): | ||
slog.Info("Shutting down cleanup manager") | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (c *CleanupManager) runCronTasks() { | ||
// Flush the user DiceDB instance | ||
resp := c.diceDBClient.Client.FlushDB(c.diceDBClient.Ctx) | ||
if resp.Err() != nil { | ||
slog.Error("Failed to flush keys from DiceDB user instance.") | ||
} | ||
|
||
// Update last cron run time on DiceDB instance | ||
cleanupTime := strconv.FormatInt(time.Now().UnixMilli(), 10) | ||
resp = c.diceDBAdminClient.Client.Set(c.diceDBClient.Ctx, utils.LastCronCleanupTimeUnixMs, | ||
cleanupTime, -1) | ||
slog.Debug("Updating last cron cleanup time key", slog.Any("cleanupTime", cleanupTime)) | ||
if resp.Err() != nil { | ||
slog.Error("Failed to set LastCronCleanupTimeUnixMs") | ||
} | ||
} |
Oops, something went wrong.