| 43 | } |
| 44 | |
| 45 | func (this *Manager) Update(items []*serverconfigs.MetricItemConfig) { |
| 46 | if this.isQuiting { |
| 47 | return |
| 48 | } |
| 49 | |
| 50 | this.locker.Lock() |
| 51 | defer this.locker.Unlock() |
| 52 | |
| 53 | var newMap = map[int64]*serverconfigs.MetricItemConfig{} |
| 54 | for _, item := range items { |
| 55 | newMap[item.Id] = item |
| 56 | } |
| 57 | |
| 58 | // 停用以前的 或 修改现在的 |
| 59 | for itemId, task := range this.taskMap { |
| 60 | newItem, ok := newMap[itemId] |
| 61 | if !ok || !newItem.IsOn { // 停用以前的 |
| 62 | remotelogs.Println("METRIC_MANAGER", "stop task '"+strconv.FormatInt(itemId, 10)+"'") |
| 63 | err := task.Stop() |
| 64 | if err != nil { |
| 65 | remotelogs.Error("METRIC_MANAGER", "stop task '"+strconv.FormatInt(itemId, 10)+"' failed: "+err.Error()) |
| 66 | } |
| 67 | |
| 68 | // deleted |
| 69 | if newItem != nil && !newItem.IsOn { |
| 70 | deleteErr := task.Delete() |
| 71 | if deleteErr != nil { |
| 72 | remotelogs.Error("METRIC_MANAGER", "delete task '"+strconv.FormatInt(itemId, 10)+"' failed: "+err.Error()) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | delete(this.taskMap, itemId) |
| 77 | } else { // 更新已存在的 |
| 78 | if newItem.Version != task.Item().Version { |
| 79 | remotelogs.Println("METRIC_MANAGER", "update task '"+strconv.FormatInt(itemId, 10)+"'") |
| 80 | task.SetItem(newItem) |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // 启动新的 |
| 86 | for _, newItem := range items { |
| 87 | if !newItem.IsOn { |
| 88 | continue |
| 89 | } |
| 90 | _, ok := this.taskMap[newItem.Id] |
| 91 | if !ok { |
| 92 | remotelogs.Println("METRIC_MANAGER", "start task '"+strconv.FormatInt(newItem.Id, 10)+"'") |
| 93 | var task Task |
| 94 | |
| 95 | if CheckSQLiteDB(newItem.Id) || !teaconst.EnableKVCacheStore { |
| 96 | task = NewSQLiteTask(newItem) |
| 97 | } else { |
| 98 | task = NewKVTask(newItem) |
| 99 | } |
| 100 | |
| 101 | err := task.Init() |
| 102 | if err != nil { |