Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
feat(service): add reset mode
Browse files Browse the repository at this point in the history
  • Loading branch information
a-wing committed Jul 27, 2023
1 parent 2904ab4 commit 02ff0cd
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Variable Name | Description | Defa

Variable Name | Description | Default
-------------- | -------------------------------------------------------- | -------------------------------------------------------------
`RESET_MODE` | Enable Auto Reset Status, **NOTE: In cluster** | `true`
`SCHEDULE` | Only Single Node. **Not support cluster** | `true`
`EMQX_AUTH` | Use Emqx redis auth plugin. If Mosquitto, Set `false` | `false`
`LUA_FILE` | Task lua > `LUA_FILE` > Default(`luavm/lua/default.lua`) | `default.lua`
Expand Down
2 changes: 2 additions & 0 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Config struct {
ListenAddr string `env:"LISTEN_ADDR"`

// Feature Flags
ResetMode bool `env:"RESET_MODE"`
Schedule bool `env:"SCHEDULE"`
EmqxAuth bool `env:"EMQX_AUTH"`
LuaFilePath string `env:"LUA_FILE"`
Expand Down Expand Up @@ -63,6 +64,7 @@ func DefaultConfig() *Config {
ApiMqttWs: "ws://localhost:8083/mqtt",
ListenAddr: "0.0.0.0:8000",

ResetMode: true,
Schedule: true,
EmqxAuth: false,
LuaFilePath: "default.lua",
Expand Down
4 changes: 4 additions & 0 deletions app/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func NewHandler(ctx context.Context, cfg *config.Config) http.Handler {
go rpcServer.Run(ctx)

srv := service.NewService(cfg, orm, rdb, ofs)
if cfg.ResetMode {
srv.Reset(ctx)
}

if cfg.Schedule {
go srv.RunSchedule(ctx)
}
Expand Down
27 changes: 27 additions & 0 deletions app/service/reset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package service

import (
"context"
)

func (s *Service) Reset(ctx context.Context) error {
prefix := "luavm.*"
var cursor uint64
for {
var keys []string
var err error
keys, cursor, err = s.rdb.Scan(ctx, cursor, prefix, 0).Result()
if err != nil {
panic(err)
}

for _, key := range keys {
s.rdb.Del(ctx, key)
}

if cursor == 0 {
break
}
}
return nil
}
67 changes: 67 additions & 0 deletions app/service/reset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package service

import (
"context"
"testing"

"sb.im/gosd/app/config"
"sb.im/gosd/app/storage"

"github.com/redis/go-redis/v9"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

func TestReset(t *testing.T) {
id := "test_id"
srv := testNewService(t)

if err := srv.LockTaskSet(id); err != nil {
t.Error(err)
}
if _, err := srv.LockTaskGet(id); err != nil {
t.Error(err)
}

if err := srv.LockNodeSet(id); err != nil {
t.Error(err)
}
if _, err := srv.LockNodeGet(id); err != nil {
t.Error(err)
}

srv.LockTaskGet(id)
if err := srv.Reset(context.Background()); err != nil {
t.Error(err)
}

if key, err := srv.LockTaskGet(id); err == nil {
t.Error(key)
}
if key, err := srv.LockNodeGet(id); err == nil {
t.Error(key)
}
}

func testNewService(t *testing.T) *Service {
cfg := config.Parse()
orm, err := gorm.Open(postgres.Open(cfg.DatabaseURL), nil)
if err != nil {
t.Error(err)
}

redisOpt, err := redis.ParseURL(cfg.RedisURL)
if err != nil {
t.Error(err)
}
rdb := redis.NewClient(redisOpt)

// Enable Redis Events
// K: store
// Ex: luavm
rdb.ConfigSet(context.Background(), "notify-keyspace-events", "$KEx")

ofs := storage.NewStorage(cfg.StorageURL)

return NewService(cfg, orm, rdb, ofs)
}

0 comments on commit 02ff0cd

Please sign in to comment.