github.com/erda-project/erda-infra@v1.0.9/base/servicehub/examples/multi-task/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 "os" 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 Message string `file:"message" flag:"msg" default:"hi" desc:"message to show" env:"HELLO_MESSAGE"` 28 } 29 30 type provider struct { 31 Cfg *config 32 Log logs.Logger 33 } 34 35 func (p *provider) Init(ctx servicehub.Context) error { 36 p.Log.Info("message: ", p.Cfg.Message) 37 ctx.AddTask(p.Task1) 38 ctx.AddTask(p.Task2, servicehub.WithTaskName("task2")) 39 return nil 40 } 41 42 func (p *provider) Task1(ctx context.Context) error { 43 p.Log.Info("Task1 is running...") 44 tick := time.NewTicker(3 * time.Second) 45 defer tick.Stop() 46 for { 47 select { 48 case <-tick.C: 49 p.Log.Info("Task1 do something...") 50 case <-ctx.Done(): 51 return nil 52 } 53 } 54 } 55 56 func (p *provider) Task2(ctx context.Context) error { 57 p.Log.Info("Task2 is running...") 58 tick := time.NewTicker(3 * time.Second) 59 defer tick.Stop() 60 for { 61 select { 62 case <-tick.C: 63 p.Log.Info("Task2 do something...") 64 case <-ctx.Done(): 65 return nil 66 } 67 } 68 } 69 70 func (p *provider) Run(ctx context.Context) error { 71 p.Log.Info("provider is running...") 72 tick := time.NewTicker(3 * time.Second) 73 defer tick.Stop() 74 for { 75 select { 76 case <-tick.C: 77 p.Log.Info("Run do something...") 78 case <-ctx.Done(): 79 return nil 80 } 81 } 82 } 83 84 func init() { 85 servicehub.Register("hello-provider", &servicehub.Spec{ 86 Services: []string{"hello"}, 87 Description: "hello for example", 88 ConfigFunc: func() interface{} { return &config{} }, 89 Creator: func() servicehub.Provider { 90 return &provider{} 91 }, 92 }) 93 } 94 95 func main() { 96 hub := servicehub.New() 97 hub.Run("examples", "", os.Args...) 98 } 99 100 // INFO[2021-08-04 16:33:10.977] message: hello world module=hello-provider 101 // INFO[2021-08-04 16:33:10.977] provider hello-provider initialized 102 // INFO[2021-08-04 16:33:10.977] signals to quit: [hangup interrupt terminated quit] 103 // INFO[2021-08-04 16:33:10.977] provider hello-provider task(task2) running ... 104 // INFO[2021-08-04 16:33:10.978] Task2 is running... module=hello-provider 105 // INFO[2021-08-04 16:33:10.978] provider hello-provider task(1) running ... 106 // INFO[2021-08-04 16:33:10.978] Task1 is running... module=hello-provider 107 // INFO[2021-08-04 16:33:10.978] provider hello-provider running ... 108 // INFO[2021-08-04 16:33:10.978] provider is running... module=hello-provider 109 // INFO[2021-08-04 16:33:13.980] Task1 do something... module=hello-provider 110 // INFO[2021-08-04 16:33:13.980] Task2 do something... module=hello-provider 111 // INFO[2021-08-04 16:33:13.983] Run do something... module=hello-provider 112 // INFO[2021-08-04 16:33:16.982] Run do something... module=hello-provider 113 // INFO[2021-08-04 16:33:16.982] Task2 do something... module=hello-provider 114 // INFO[2021-08-04 16:33:16.982] Task1 do something... module=hello-provider 115 // INFO[2021-08-04 16:33:19.981] Run do something... module=hello-provider 116 // INFO[2021-08-04 16:33:19.981] Task2 do something... module=hello-provider 117 // INFO[2021-08-04 16:33:19.981] Task1 do something... module=hello-provider 118 // ^C 119 // INFO[2021-08-04 16:33:20.247] provider hello-provider Run exit 120 // INFO[2021-08-04 16:33:20.247] provider hello-provider task(task2) exit 121 // INFO[2021-08-04 16:33:20.247] provider hello-provider task(1) exit