github.com/songzhibin97/gkit@v1.2.13/distributed/example/example.go (about) 1 package example 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "strings" 8 "time" 9 10 "github.com/songzhibin97/gkit/distributed/backend/backend_redis" 11 12 "github.com/songzhibin97/gkit/distributed/backend" 13 zlog "github.com/songzhibin97/gkit/log" 14 15 "github.com/go-redis/redis/v8" 16 17 "github.com/songzhibin97/gkit/distributed" 18 "github.com/songzhibin97/gkit/distributed/broker" 19 "github.com/songzhibin97/gkit/distributed/controller/controller_redis" 20 "github.com/songzhibin97/gkit/distributed/locker/lock_ridis" 21 ) 22 23 // Add ... 24 func Add(args ...int64) (int64, error) { 25 sum := int64(0) 26 for _, arg := range args { 27 sum += arg 28 } 29 return sum, nil 30 } 31 32 // Multiply ... 33 func Multiply(args ...int64) (int64, error) { 34 sum := int64(1) 35 for _, arg := range args { 36 sum *= arg 37 } 38 return sum, nil 39 } 40 41 // SumInts ... 42 func SumInts(numbers []int64) (int64, error) { 43 var sum int64 44 for _, num := range numbers { 45 sum += num 46 } 47 return sum, nil 48 } 49 50 // SumFloats ... 51 func SumFloats(numbers []float64) (float64, error) { 52 var sum float64 53 for _, num := range numbers { 54 sum += num 55 } 56 return sum, nil 57 } 58 59 // Concat ... 60 func Concat(strs []string) (string, error) { 61 var res string 62 for _, s := range strs { 63 res += s 64 } 65 return res, nil 66 } 67 68 // Split ... 69 func Split(str string) ([]string, error) { 70 return strings.Split(str, ""), nil 71 } 72 73 // PanicTask ... 74 func PanicTask() (string, error) { 75 panic(errors.New("oops")) 76 } 77 78 // LongRunningTask ... 79 func LongRunningTask() error { 80 fmt.Println("Long running task started") 81 for i := 0; i < 10; i++ { 82 fmt.Println(10 - i) 83 time.Sleep(1 * time.Second) 84 } 85 fmt.Println("Long running task finished") 86 return nil 87 } 88 89 func InitServer() *distributed.Server { 90 opt := redis.UniversalOptions{ 91 Addrs: []string{"127.0.0.1:6379"}, 92 } 93 client := redis.NewUniversalClient(&opt) 94 if client == nil { 95 return nil 96 } 97 lock := lock_ridis.NewRedisLock(client) 98 bk := broker.NewBroker(broker.NewRegisteredTask(), context.Background()) 99 c := controller_redis.NewControllerRedis(bk, client, "gkit:queue", "delayed") 100 101 var backendClient backend.Backend 102 { 103 // redis 104 backendClient = backend_redis.NewBackendRedis(client, -1) 105 } 106 //{ 107 // // mongodb 108 // mongoClient, err := mongo.NewClient() 109 // if err != nil { 110 // return nil 111 // } 112 // err = mongoClient.Connect(context.Background()) 113 // if err != nil { 114 // return nil 115 // } 116 // backendClient = backend_mongodb.NewBackendMongoDB(mongoClient, -1) 117 //} 118 { 119 //dsn := "root:123456@tcp(127.0.0.1:3306)/gkit?charset=utf8mb4&parseTime=True&loc=Local" 120 //sqlDB, err := sql.Open("mysql", dsn) 121 //if err != nil { 122 // return nil 123 //} 124 //backendClient = backend_db.NewBackendSQLDB(sqlDB, -1, "mysql", &gorm.Config{ 125 // Logger: logger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Config{ 126 // SlowThreshold: time.Second, 127 // Colorful: false, 128 // IgnoreRecordNotFoundError: true, 129 // LogLevel: logger.Error, 130 // }), 131 //}) 132 } 133 134 // Register tasks 135 tasksMap := map[string]interface{}{ 136 "add": Add, 137 "multiply": Multiply, 138 "sum_ints": SumInts, 139 "sum_floats": SumFloats, 140 "concat": Concat, 141 "split": Split, 142 "panic_task": PanicTask, 143 "long_running_task": LongRunningTask, 144 } 145 s := distributed.NewServer(c, backendClient, lock, zlog.NewHelper(zlog.With(zlog.DefaultLogger)), nil) 146 err := s.RegisteredTasks(tasksMap) 147 if err != nil { 148 panic(err) 149 } 150 return s 151 }