(ctx context.Context)
| 46 | } |
| 47 | |
| 48 | func (e *executor) Init(ctx context.Context) error { |
| 49 | orgInfo, err := e.client.OrganizationsAPI().FindOrganizationByName(ctx, e.cfg.Org) |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | orgID := *orgInfo.Id |
| 54 | //初始化bucket |
| 55 | bucketsAPI := e.client.BucketsAPI() |
| 56 | buckets, err := bucketsAPI.FindBucketsByOrgName(ctx, e.cfg.Org) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | |
| 61 | bucketsConf := flux.GetBucketConfigList() |
| 62 | //要创建的bucket |
| 63 | toCreateBuckets := utils.SliceToMap(bucketsConf, func(t *flux.BucketConf) string { |
| 64 | return t.BucketName |
| 65 | }) |
| 66 | if buckets != nil { |
| 67 | for _, bucket := range *buckets { |
| 68 | if _, has := toCreateBuckets[bucket.Name]; has { |
| 69 | delete(toCreateBuckets, bucket.Name) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | expire := domain.RetentionRuleTypeExpire |
| 74 | rule := domain.RetentionRule{ |
| 75 | ShardGroupDurationSeconds: nil, |
| 76 | Type: &expire, |
| 77 | } |
| 78 | //创建bucket |
| 79 | for _, bucketConf := range toCreateBuckets { |
| 80 | rule.EverySeconds = bucketConf.Retention |
| 81 | _, err := e.client.BucketsAPI().CreateBucketWithNameWithID(ctx, orgID, bucketConf.BucketName, rule) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | log.Infof("Save bucket %s success. organization: %s", bucketConf.BucketName, e.cfg.Org) |
| 86 | } |
| 87 | |
| 88 | //创建定时脚本 |
| 89 | tasksApi := e.client.TasksAPI() |
| 90 | taskFilter := &api.TaskFilter{ |
| 91 | OrgID: orgID, |
| 92 | } |
| 93 | existedTasks, err := tasksApi.FindTasks(ctx, taskFilter) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | tasksConf := flux.GetTaskConfigList() |
| 98 | //要创建的bucket |
| 99 | toCreateTasks := utils.SliceToMap(tasksConf, func(t *flux.TaskConf) string { |
| 100 | return t.TaskName |
| 101 | }) |
| 102 | toDeleteTaskIDs := make([]string, 0, len(toCreateTasks)) |
| 103 | |
| 104 | /* |
| 105 | 将influxDB已存在的定时脚本 与 定时脚本配置的进行对比 |
nothing calls this directly
no test coverage detected