github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/cmd/srv-applet-mgr/main.go (about) 1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "sync" 7 "time" 8 9 "github.com/machinefi/w3bstream/cmd/srv-applet-mgr/apis" 10 "github.com/machinefi/w3bstream/cmd/srv-applet-mgr/global" 11 "github.com/machinefi/w3bstream/pkg/depends/conf/logger" 12 "github.com/machinefi/w3bstream/pkg/depends/kit/kit" 13 "github.com/machinefi/w3bstream/pkg/depends/kit/logr" 14 "github.com/machinefi/w3bstream/pkg/modules/account" 15 "github.com/machinefi/w3bstream/pkg/modules/blockchain" 16 "github.com/machinefi/w3bstream/pkg/modules/cronjob" 17 "github.com/machinefi/w3bstream/pkg/modules/deploy" 18 "github.com/machinefi/w3bstream/pkg/modules/event" 19 "github.com/machinefi/w3bstream/pkg/modules/metrics" 20 "github.com/machinefi/w3bstream/pkg/modules/operator" 21 "github.com/machinefi/w3bstream/pkg/modules/project" 22 "github.com/machinefi/w3bstream/pkg/modules/robot_notifier" 23 "github.com/machinefi/w3bstream/pkg/modules/robot_notifier/lark" 24 "github.com/machinefi/w3bstream/pkg/modules/trafficlimit" 25 "github.com/machinefi/w3bstream/pkg/types" 26 ) 27 28 var app = global.App 29 30 func init() { 31 // global.Migrate() 32 } 33 34 func main() { 35 ctx, l := logger.NewSpanContext(global.WithContext(context.Background()), "main") 36 defer l.End() 37 38 app.Execute(func(args ...string) { 39 BatchRun( 40 func() { 41 kit.Run(apis.RootMgr, global.Server()) 42 }, 43 func() { 44 kit.Run(apis.RootEvent, global.EventServer()) 45 }, 46 func() { 47 kit.Run(apis.RootDebug, global.DebugServer()) 48 }, 49 func() { 50 ctx, l := logr.Start(ctx, "main.InitProjects") 51 defer l.End() 52 53 passwd, err := account.CreateAdminIfNotExist(ctx) 54 if err != nil { 55 l.Error(err) 56 panic(err) 57 } 58 if passwd == "" { 59 l.Info("admin already exists") 60 } else { 61 l.Info("admin created, default password is: '%s'", passwd) 62 } 63 64 if err := deploy.Init(ctx); err != nil { 65 l.Error(err) 66 panic(err) 67 } 68 if _, err = project.Init(ctx); err != nil { 69 l.Error(err) 70 panic(err) 71 } 72 l.Info("all projects initialized") 73 }, 74 func() { 75 if err := trafficlimit.Init(ctx); err != nil { 76 panic(err) 77 } 78 }, 79 func() { 80 if err := blockchain.InitChainDB(ctx); err != nil { 81 l.Error(err) 82 panic(err) 83 } 84 }, 85 func() { 86 blockchain.Monitor(ctx) 87 }, 88 func() { 89 cronjob.Run(ctx) 90 }, 91 func() { 92 operator.Migrate(ctx) 93 }, 94 func() { 95 metrics.Init(ctx) 96 }, 97 func() { 98 sche := event.NewDefaultEventCleanupScheduler() 99 sche.Run(ctx) 100 }, 101 func() { 102 filter := types.MustProjectFilterFromContext(ctx) 103 104 body, err := lark.Build( 105 ctx, 106 "service started", 107 "INFO", 108 fmt.Sprintf("service started at: %s\nblack list: %v\nwhite list: %v", 109 types.Timestamp{Time: time.Now()}.String(), 110 filter.BlackList, 111 filter.WhiteList, 112 ), 113 ) 114 if err != nil { 115 return 116 } 117 _ = robot_notifier.Push(ctx, body, nil) 118 }, 119 ) 120 }) 121 } 122 123 func BatchRun(commands ...func()) { 124 wg := &sync.WaitGroup{} 125 126 for i := range commands { 127 cmd := commands[i] 128 wg.Add(1) 129 130 go func() { 131 defer wg.Done() 132 cmd() 133 time.Sleep(200 * time.Millisecond) 134 }() 135 } 136 wg.Wait() 137 }