Skip to content

Commit

Permalink
索引切换使用应用切换
Browse files Browse the repository at this point in the history
  • Loading branch information
jianyun8023 committed Aug 22, 2024
1 parent e8b4001 commit 8d1132d
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions internal/calibre/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Api struct {
client *meilisearch.Client
baseDir string
http *client.Client
useIndex string
}

func (c Api) SetupRouter(r *gin.Engine) {
Expand All @@ -52,7 +53,7 @@ func (c Api) SetupRouter(r *gin.Engine) {
}

func (c Api) currentIndex() *meilisearch.Index {
return c.client.Index(c.config.Search.Index)
return c.client.Index(c.useIndex)
}

func NewClient(config *Config) Api {
Expand Down Expand Up @@ -85,6 +86,7 @@ func NewClient(config *Config) Api {
baseDir: config.TmpDir,
contentApi: &newClient,
http: newClient.Client,
useIndex: config.Search.Index,
}
}

Expand Down Expand Up @@ -362,17 +364,10 @@ func (c Api) switchIndex(c2 *gin.Context) {
return
}

_, err = c.client.SwapIndexes(
[]meilisearch.SwapIndexesParams{
{
Indexes: []string{c.config.Search.Index, c.config.Search.Index + "-bak"},
},
},
)
if err != nil {
log.Warn(err)
c2.JSON(http.StatusOK, gin.H{"code": 500, "error": err.Error()})
return
if c.useIndex == c.config.Search.Index {
c.useIndex = c.config.Search.Index + "-bak"
} else {
c.useIndex = c.config.Search.Index
}
c2.JSON(http.StatusOK, gin.H{
"code": 200,
Expand Down Expand Up @@ -466,13 +461,11 @@ func waitForTask(c Api, taskIds []int64) error {
return fmt.Errorf("Timeout reached while waiting for tasks to complete")
case <-done:
log.Info("Tasks completed successfully")
_, _ = c.client.SwapIndexes(
[]meilisearch.SwapIndexesParams{
{
Indexes: []string{c.config.Search.Index, c.config.Search.Index + "-bak"},
},
},
)
if c.useIndex == c.config.Search.Index {
c.useIndex = c.config.Search.Index + "-bak"
} else {
c.useIndex = c.config.Search.Index
}
return nil
}
}
Expand Down

0 comments on commit 8d1132d

Please sign in to comment.