Skip to content

Commit

Permalink
dev: Fix mage flush target
Browse files Browse the repository at this point in the history
  • Loading branch information
ryaplots committed Dec 18, 2024
1 parent 2e42e4a commit 3024e38
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
16 changes: 4 additions & 12 deletions cypress/plugins/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,10 @@ const sqlTask = on => {
},
dropAndSeedDatabase: async () => {
const exec = util.promisify(childProcess.exec)

// Restore SQL data
await exec('tools/bin/mage dev:sqlRestore', { cwd: '..' })

// Flush Redis database
try {
await exec('tools/bin/mage dev:redisFlush', { cwd: '..' })
} catch (e) {
// eslint-disable-next-line no-console
console.log('Error flushing Redis:', e)
}

await Promise.all([
exec('tools/bin/mage dev:sqlRestore', { cwd: '..' }),
exec('tools/bin/mage -v dev:redisFlush', { cwd: '..' }),
])
return null
},
})
Expand Down
24 changes: 21 additions & 3 deletions tools/mage/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,39 @@ func (Dev) SQLRestore() error {
)
}

// RedisFlush deletes all keys from redis.
// RedisFlush deletes all keys from redis except specific task queues.
func (Dev) RedisFlush() error {
if mg.Verbose() {
fmt.Println("Deleting all keys from redis")
fmt.Println("Deleting keys from redis (preserving task queues)")
}

keys, err := sh.Output("docker", dockerComposeFlags("exec", "-T", "redis", "redis-cli", "keys", "ttn:v3:*")...)
if err != nil {
return err
}

ks := strings.Split(keys, "\n")
if len(ks) == 0 {
return nil
}
flags := dockerComposeFlags(append([]string{"exec", "-T", "redis", "redis-cli", "del"}, ks...)...)

// Delete all keys except preserved ones
baseArgs := []string{"exec", "-T", "redis", "redis-cli", "del"}
keysToDelete := make([]string, 0, len(ks))

for _, k := range ks {
if k != "ttn:v3:ns:tasks:downlink:ready" &&
k != "ttn:v3:ns:tasks:downlink:input" &&
k != "ttn:v3:ns:application-uplinks:uplinks" {
keysToDelete = append(keysToDelete, k)
}
}
// No keys to delete
if len(keysToDelete) == len(baseArgs) {
return nil
}

flags := dockerComposeFlags(append(baseArgs, keysToDelete...)...)
_, err = sh.Exec(nil, nil, os.Stderr, "docker", flags...)
return err
}
Expand Down

0 comments on commit 3024e38

Please sign in to comment.