github.com/erda-project/erda-infra@v1.0.9/base/servicehub/examples/error-on-start/main.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "context" 19 "fmt" 20 "time" 21 22 "github.com/erda-project/erda-infra/base/logs" 23 "github.com/erda-project/erda-infra/base/servicehub" 24 ) 25 26 type config struct { 27 Error bool `file:"error"` 28 } 29 30 type provider struct { 31 Cfg *config 32 Log logs.Logger 33 } 34 35 func (p *provider) Run(ctx context.Context) error { 36 if p.Cfg.Error { 37 time.Sleep(3 * time.Second) 38 return fmt.Errorf("run error") 39 } 40 p.Log.Info("run with no error") 41 for { 42 select { 43 case <-ctx.Done(): 44 return nil 45 } 46 } 47 } 48 49 func init() { 50 servicehub.Register("hello-provider", &servicehub.Spec{ 51 Services: []string{"hello"}, 52 Description: "hello for example", 53 ConfigFunc: func() interface{} { return &config{} }, 54 Creator: func() servicehub.Provider { 55 return &provider{} 56 }, 57 }) 58 } 59 60 func main() { 61 servicehub.Run(&servicehub.RunOptions{ 62 Content: ` 63 hello-provider: 64 error: false 65 hello-provider@error1: 66 error: true 67 hello-provider@error2: 68 error: true 69 `, 70 }) 71 }