Skip to content

Commit

Permalink
Delete instances that are not found during sync (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentaTomas authored Nov 28, 2023
2 parents 6f1e898 + 90d6e2b commit 65c6eb4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.48
0.0.49
18 changes: 17 additions & 1 deletion packages/api/internal/nomad/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

const (
InstanceExpiration = time.Second * 15
cacheSyncTime = time.Hour * 24
cacheSyncTime = time.Minute * 3
maxInstanceLength = time.Hour * 24
)

Expand Down Expand Up @@ -123,6 +123,22 @@ func (c *InstanceCache) Exists(instanceID string) bool {
}

func (c *InstanceCache) Sync(instances []*api.Instance) {
instanceMap := make(map[string]*api.Instance)

// Use map for faster lookup
for _, instance := range instances {
instanceMap[instance.InstanceID] = instance
}

// Delete instances that are not in Nomad anymore
for _, item := range c.cache.Items() {
_, found := instanceMap[item.Key()]
if !found {
c.cache.Delete(item.Key())
}
}

// Add instances that are not in the cache with the default TTL
for _, instance := range instances {
if !c.Exists(instance.InstanceID) {
err := c.Add(instance, nil, nil)
Expand Down
38 changes: 32 additions & 6 deletions packages/envd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,21 @@ import (
// There isn't an explicit documentation, I'm using source code of tests as a reference:
// https://cs.github.com/ethereum/go-ethereum/blob/440c9fcf75d9d5383b72646a65d5e21fa7ab6a26/rpc/testservice_test.go#L160

const (
Version = "dev"
defaultServerPort uint = 49982

startCmdID = "_startCmd"
)

var (
logger *zap.SugaredLogger
wsHandler http.Handler

debug bool
serverPort uint
versionFlag bool

Version = "dev"
defaultServerPort uint = 49982
debug bool
serverPort uint
versionFlag bool
startCmdFlag string
)

func serveWs(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -85,6 +90,13 @@ func parseFlags() {
"a port on which the daemon should run",
)

flag.StringVar(
&startCmdFlag,
"cmd",
"",
"a command to run on the daemon start",
)

flag.Parse()
}

Expand Down Expand Up @@ -145,6 +157,20 @@ func main() {
logger.Panicw("failed to register process service", "error", err)
}

// Start the command passed via the -cmd flag.
if startCmdFlag != "" {
_, err := processService.Start(startCmdID, startCmdFlag, nil, "/")
// TODO: Do we need to cache the process logs if they are not retrieved?
// TODO: Should we cache all process logs always?
if err != nil {
logger.Errorf(
"failed to start the command passed via the -cmd flag",
"cmd", startCmdFlag,
"err", err,
)
}
}

terminalService := terminal.NewService(logger.Named("terminalSvc"), envConfig)
if err := rpcServer.RegisterName("terminal", terminalService); err != nil {
logger.Panicw("failed to register terminal service", "error", err)
Expand Down

0 comments on commit 65c6eb4

Please sign in to comment.