forked from ccfos/nightingale
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
166 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package memsto | ||
|
||
import ( | ||
"encoding/json" | ||
"sync" | ||
"time" | ||
|
||
"github.com/BurntSushi/toml" | ||
"github.com/ccfos/nightingale/v6/alert/aconf" | ||
"github.com/ccfos/nightingale/v6/models" | ||
"github.com/ccfos/nightingale/v6/pkg/ctx" | ||
"github.com/toolkits/pkg/logger" | ||
) | ||
|
||
type NotifyConfigCacheType struct { | ||
ctx *ctx.Context | ||
webhooks []*models.Webhook | ||
smtp aconf.SMTPConfig | ||
script models.NotifyScript | ||
ibex aconf.Ibex | ||
|
||
sync.RWMutex | ||
} | ||
|
||
func NewNotifyConfigCache(ctx *ctx.Context) *NotifyConfigCacheType { | ||
w := &NotifyConfigCacheType{ | ||
ctx: ctx, | ||
} | ||
w.SyncNotifyConfigs() | ||
return w | ||
} | ||
|
||
func (w *NotifyConfigCacheType) SyncNotifyConfigs() { | ||
err := w.syncNotifyConfigs() | ||
if err != nil { | ||
logger.Errorf("failed to sync webhooks:", err) | ||
} | ||
|
||
go w.loopSyncNotifyConfigs() | ||
} | ||
|
||
func (w *NotifyConfigCacheType) loopSyncNotifyConfigs() { | ||
duration := time.Duration(9000) * time.Millisecond | ||
for { | ||
time.Sleep(duration) | ||
if err := w.syncNotifyConfigs(); err != nil { | ||
logger.Warning("failed to sync webhooks:", err) | ||
} | ||
} | ||
} | ||
|
||
func (w *NotifyConfigCacheType) syncNotifyConfigs() error { | ||
w.RWMutex.Lock() | ||
defer w.RWMutex.Unlock() | ||
|
||
cval, err := models.ConfigsGet(w.ctx, models.WEBHOOKKEY) | ||
if err != nil { | ||
return err | ||
} | ||
json.Unmarshal([]byte(cval), &w.webhooks) | ||
logger.Infof("timer: sync wbhooks done number: %d", len(w.webhooks)) | ||
|
||
cval, err = models.ConfigsGet(w.ctx, models.SMTP) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = toml.Unmarshal([]byte(cval), &w.smtp) | ||
if err != nil { | ||
logger.Errorf("failed to unmarshal smtp:%s config:", cval, err) | ||
} | ||
|
||
logger.Infof("timer: sync smtp done") | ||
|
||
cval, err = models.ConfigsGet(w.ctx, models.NOTIFYSCRIPT) | ||
if err != nil { | ||
return err | ||
} | ||
err = json.Unmarshal([]byte(cval), &w.script) | ||
if err != nil { | ||
logger.Errorf("failed to unmarshal notify script:%s config:", cval, err) | ||
} | ||
logger.Infof("timer: sync notify script done") | ||
|
||
cval, err = models.ConfigsGet(w.ctx, models.IBEX) | ||
if err != nil { | ||
return err | ||
} | ||
err = toml.Unmarshal([]byte(cval), &w.ibex) | ||
if err != nil { | ||
logger.Errorf("failed to unmarshal ibex:%s config:", cval, err) | ||
} | ||
logger.Infof("timer: sync ibex done") | ||
|
||
return nil | ||
} | ||
|
||
func (w *NotifyConfigCacheType) GetWebhooks() []*models.Webhook { | ||
w.RWMutex.RLock() | ||
defer w.RWMutex.RUnlock() | ||
return w.webhooks | ||
} | ||
|
||
func (w *NotifyConfigCacheType) GetSMTP() aconf.SMTPConfig { | ||
w.RWMutex.RLock() | ||
defer w.RWMutex.RUnlock() | ||
return w.smtp | ||
} | ||
|
||
func (w *NotifyConfigCacheType) GetNotifyScript() models.NotifyScript { | ||
w.RWMutex.RLock() | ||
defer w.RWMutex.RUnlock() | ||
return w.script | ||
} | ||
|
||
func (w *NotifyConfigCacheType) GetIbex() aconf.Ibex { | ||
w.RWMutex.RLock() | ||
defer w.RWMutex.RUnlock() | ||
return w.ibex | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.