@@ -38,19 +38,21 @@ type DiscoveryConfig struct {
3838
3939type TrafficConfig struct {
4040 WarningPercent float64
41+ ExceededAction string
4142}
4243
4344type LoggingConfig struct {
4445 Level string
4546}
4647
4748type NotificationConfig struct {
48- Enabled bool
49- WeChatCorpID string
50- WeChatCorpSecret string
51- WeChatAgentID int
52- WeChatToUser []string
53- NotifyEvents []string
49+ Enabled bool
50+ WeChatCorpID string
51+ WeChatCorpSecret string
52+ WeChatAgentID int
53+ WeChatToUser []string
54+ NotifyEvents []string
55+ ManualRequiredNotifyInterval time.Duration
5456}
5557
5658type KeepAliveConfig struct {
@@ -207,10 +209,11 @@ func defaultConfig() Config {
207209 RegionRefreshInterval : 24 * time .Hour ,
208210 MaxConcurrency : 4 ,
209211 },
210- Traffic : TrafficConfig {WarningPercent : 95 },
212+ Traffic : TrafficConfig {WarningPercent : 95 , ExceededAction : "notify_only" },
211213 Logging : LoggingConfig {Level : "info" },
212214 Notification : NotificationConfig {
213- NotifyEvents : []string {"auto_start" , "manual_start" , "manual_stop" , "manual_required" , "traffic_exceeded" , "error" },
215+ NotifyEvents : []string {"auto_start" , "manual_start" , "manual_stop" , "manual_required" , "traffic_exceeded" , "traffic_stop" , "error" },
216+ ManualRequiredNotifyInterval : time .Hour ,
214217 },
215218 KeepAlive : KeepAliveConfig {
216219 Enabled : true ,
@@ -278,6 +281,8 @@ func applyTraffic(cfg *TrafficConfig, key, value string) error {
278281 return fmt .Errorf ("warning_percent 必须是数字" )
279282 }
280283 cfg .WarningPercent = number
284+ case "exceeded_action" :
285+ cfg .ExceededAction = scalar (value )
281286 default :
282287 return fmt .Errorf ("未知 traffic 字段 %q" , key )
283288 }
@@ -316,6 +321,12 @@ func applyNotification(cfg *NotificationConfig, key, value string) error {
316321 cfg .WeChatToUser = parseList (value )
317322 case "notify_events" :
318323 cfg .NotifyEvents = parseList (value )
324+ case "manual_required_notify_interval" :
325+ duration , err := parseDuration (value )
326+ if err != nil {
327+ return err
328+ }
329+ cfg .ManualRequiredNotifyInterval = duration
319330 default :
320331 return fmt .Errorf ("未知 notification 字段 %q" , key )
321332 }
@@ -395,6 +406,14 @@ func validate(cfg *Config) error {
395406 if cfg .Traffic .WarningPercent <= 0 {
396407 return errors .New ("traffic.warning_percent 必须大于 0" )
397408 }
409+ if cfg .Traffic .ExceededAction == "" {
410+ cfg .Traffic .ExceededAction = "notify_only"
411+ }
412+ switch cfg .Traffic .ExceededAction {
413+ case "notify_only" , "notify_and_stop" :
414+ default :
415+ return fmt .Errorf ("不支持的 traffic.exceeded_action: %s" , cfg .Traffic .ExceededAction )
416+ }
398417 if cfg .KeepAlive .Target == "" {
399418 cfg .KeepAlive .Target = "spot_only"
400419 }
@@ -413,7 +432,10 @@ func validate(cfg *Config) error {
413432 return fmt .Errorf ("不支持的 logging.level: %s" , cfg .Logging .Level )
414433 }
415434 if cfg .Notification .NotifyEvents == nil {
416- cfg .Notification .NotifyEvents = []string {"auto_start" , "manual_start" , "manual_stop" , "manual_required" , "traffic_exceeded" , "error" }
435+ cfg .Notification .NotifyEvents = []string {"auto_start" , "manual_start" , "manual_stop" , "manual_required" , "traffic_exceeded" , "traffic_stop" , "error" }
436+ }
437+ if cfg .Notification .ManualRequiredNotifyInterval <= 0 {
438+ return errors .New ("notification.manual_required_notify_interval 必须大于 0" )
417439 }
418440 if err := validateNotifyEvents (cfg .Notification .NotifyEvents ); err != nil {
419441 return err
@@ -423,6 +445,9 @@ func validate(cfg *Config) error {
423445 default :
424446 return fmt .Errorf ("不支持的 traffic_policy: %s" , cfg .KeepAlive .TrafficPolicy )
425447 }
448+ if cfg .Traffic .ExceededAction == "notify_and_stop" && cfg .KeepAlive .TrafficPolicy == "ignore_limit" {
449+ return errors .New ("traffic.exceeded_action=notify_and_stop 不能与 keep_alive.traffic_policy=ignore_limit 同时使用" )
450+ }
426451 switch cfg .KeepAlive .StopMode {
427452 case "StopCharging" , "KeepCharging" :
428453 default :
@@ -469,7 +494,7 @@ func validate(cfg *Config) error {
469494func validateNotifyEvents (events []string ) error {
470495 for _ , event := range events {
471496 switch event {
472- case "all" , "auto_start" , "manual_start" , "manual_stop" , "manual_required" , "traffic_exceeded" , "error" :
497+ case "all" , "auto_start" , "manual_start" , "manual_stop" , "manual_required" , "traffic_exceeded" , "traffic_stop" , " error" :
473498 default :
474499 return fmt .Errorf ("不支持的 notification.notify_events: %s" , event )
475500 }
@@ -510,6 +535,9 @@ func applyEnv(cfg *Config, includeGlobal bool) error {
510535 } else if ok {
511536 cfg .Traffic .WarningPercent = value
512537 }
538+ if value , ok := lookupEnvString ("EC_TRAFFIC_EXCEEDED_ACTION" ); ok {
539+ cfg .Traffic .ExceededAction = value
540+ }
513541 if value , ok := lookupEnvString ("EC_LOG_LEVEL" ); ok {
514542 cfg .Logging .Level = strings .ToLower (value )
515543 }
@@ -533,6 +561,11 @@ func applyEnv(cfg *Config, includeGlobal bool) error {
533561 if value , ok := lookupEnvList ("EC_NOTIFY_EVENTS" ); ok {
534562 cfg .Notification .NotifyEvents = value
535563 }
564+ if value , ok , err := lookupEnvDuration ("EC_MANUAL_REQUIRED_NOTIFY_INTERVAL" ); err != nil {
565+ return err
566+ } else if ok {
567+ cfg .Notification .ManualRequiredNotifyInterval = value
568+ }
536569 if value , ok := lookupEnvBool ("EC_KEEP_ALIVE_ENABLED" ); ok {
537570 cfg .KeepAlive .Enabled = value
538571 }
0 commit comments