|
@@ -402,6 +402,63 @@ func dealIndexByMonth() {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+//SwitchAlias 切换别名
|
|
|
|
+func SwitchAlias() {
|
|
|
|
+ now := time.Now()
|
|
|
|
+ // 判断当前时间是否为当前月份的最后一天
|
|
|
|
+ //按日创建索引
|
|
|
|
+ if GF.Env.SpecType == "day" {
|
|
|
|
+ dealAlias()
|
|
|
|
+ } else if GF.Env.SpecType == "month" {
|
|
|
|
+ // 获取当前月份的最后一天
|
|
|
|
+ lastDayOfMonth := time.Date(now.Year(), now.Month()+1, 0, 0, 0, 0, 0, time.UTC)
|
|
|
|
+ //按月创建索引
|
|
|
|
+ if now.Day() == lastDayOfMonth.Day() {
|
|
|
|
+ dealAlias()
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func dealAlias() {
|
|
|
|
+ now := time.Now()
|
|
|
|
+ url := GF.ES.URL
|
|
|
|
+ username := GF.ES.Username
|
|
|
|
+ password := GF.ES.Password
|
|
|
|
+
|
|
|
|
+ // 创建 Elasticsearch 客户端
|
|
|
|
+ client, err := elastic.NewClient(
|
|
|
|
+ elastic.SetURL(url),
|
|
|
|
+ elastic.SetBasicAuth(username, password),
|
|
|
|
+ elastic.SetSniff(false),
|
|
|
|
+ )
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Error("创建 Elasticsearch 客户端失败:", zap.Error(err))
|
|
|
|
+ }
|
|
|
|
+ next := now.AddDate(0, 0, 1)
|
|
|
|
+ month := int(next.Month())
|
|
|
|
+ monthStr := strconv.Itoa(month)
|
|
|
|
+ year := next.Year()
|
|
|
|
+ yearStr := strconv.Itoa(year)
|
|
|
|
+ //下一个月的索引名称
|
|
|
|
+ preBiddingIndex := "bidding_" + yearStr + monthStr
|
|
|
|
+
|
|
|
|
+ month2 := int(now.Month())
|
|
|
|
+ monthStr2 := strconv.Itoa(month2)
|
|
|
|
+ year2 := now.Year()
|
|
|
|
+ yearStr2 := strconv.Itoa(year2)
|
|
|
|
+ //当前;的索引名称
|
|
|
|
+ currIndex := "bidding_" + yearStr2 + monthStr2
|
|
|
|
+
|
|
|
|
+ _, err = client.Alias().Add(preBiddingIndex, GF.Env.Alias).Do(context.Background())
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Error("添加别名失败:", zap.Error(err))
|
|
|
|
+ }
|
|
|
|
+ _, err = client.Alias().Remove(currIndex, GF.Env.Alias).Do(context.Background())
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Error("删除别名失败:", zap.Error(err))
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
//SendMail 发送邮件
|
|
//SendMail 发送邮件
|
|
func SendMail(title, content string) {
|
|
func SendMail(title, content string) {
|
|
url := fmt.Sprintf("%s?to=%s&title=%s&body=%s", GF.Email.Api, GF.Email.To, title, content)
|
|
url := fmt.Sprintf("%s?to=%s&title=%s&body=%s", GF.Email.Api, GF.Email.To, title, content)
|