147 lines
3.3 KiB
Go
147 lines
3.3 KiB
Go
/*
|
|
* Tencent is pleased to support the open source community by making Blueking Container Service available.
|
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
|
* Licensed under the MIT License (the "License"); you may not use this file except
|
|
* in compliance with the License. You may obtain a copy of the License at
|
|
* http://opensource.org/licenses/MIT
|
|
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
* either express or implied. See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
// Package main xxx
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"git.ifooth.com/common/pkg/task"
|
|
etcdbackend "git.ifooth.com/common/pkg/task/backends/etcd"
|
|
etcdbroker "git.ifooth.com/common/pkg/task/brokers/etcd"
|
|
etcdlock "git.ifooth.com/common/pkg/task/locks/etcd"
|
|
istep "git.ifooth.com/common/pkg/task/steps/iface"
|
|
mysqlstore "git.ifooth.com/common/pkg/task/stores/mysql"
|
|
"git.ifooth.com/common/pkg/task/types"
|
|
"github.com/RichardKnop/machinery/v2/config"
|
|
)
|
|
|
|
/*
|
|
场景测试
|
|
1. 正常分发任务并成功执行
|
|
2. 任务执行失败并暂停执行任务
|
|
3. 重试失败任务 / 设置skipOnFailed并重试成功
|
|
4. 任务跳过失败测试
|
|
5. step超时控制
|
|
6. task超时控制
|
|
7. 任务回调机制
|
|
*/
|
|
|
|
var (
|
|
moduleName = "example"
|
|
mysqlDSN = "root:%s@tcp(127.0.0.1:3306)/tasks?charset=utf8mb4&parseTime=True&loc=Local"
|
|
)
|
|
|
|
// nolint
|
|
func main() {
|
|
pwd := os.Getenv("MYSQL_PASSWORD")
|
|
|
|
ctx := context.Background()
|
|
|
|
broker, err := etcdbroker.New(ctx, &config.Config{})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
lock, err := etcdlock.New(ctx, &config.Config{}, 3)
|
|
if err != nil {
|
|
panic(lock)
|
|
}
|
|
|
|
dns := fmt.Sprintf(mysqlDSN, pwd)
|
|
store, err := mysqlstore.New(dns)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
err = store.EnsureTable(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
backend, err := etcdbackend.New(ctx, &config.Config{})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
btm := task.NewTaskManager()
|
|
config := &task.ManagerConfig{
|
|
ModuleName: moduleName,
|
|
WorkerNum: 100,
|
|
Broker: broker,
|
|
Backend: backend,
|
|
Lock: lock,
|
|
Store: store,
|
|
}
|
|
|
|
// init task manager
|
|
err = btm.Init(config)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// run task manager
|
|
go func() {
|
|
_ = btm.Run()
|
|
}()
|
|
|
|
// wait task server run
|
|
time.Sleep(3 * time.Second)
|
|
|
|
// build tak && run
|
|
sum := NewExampleTask("3", "5")
|
|
|
|
info := types.TaskInfo{
|
|
TaskIndex: "example",
|
|
TaskType: "example-test",
|
|
TaskName: "example",
|
|
Creator: "bcs",
|
|
}
|
|
sumTask, err := sum.BuildTask(info, types.WithTaskMaxExecutionSeconds(0),
|
|
types.WithTaskCallback(callBackName))
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
err = btm.Dispatch(sumTask)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
// listening OS shutdown singal
|
|
signalChan := make(chan os.Signal, 1)
|
|
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
|
|
<-signalChan
|
|
|
|
btm.Stop()
|
|
|
|
fmt.Printf("Got OS shutdown signal, shutting down server gracefully...")
|
|
}
|
|
|
|
// nolint
|
|
func registerSteps() []istep.StepExecutor {
|
|
steps := make([]istep.StepExecutor, 0)
|
|
|
|
sum := NewSumStep()
|
|
steps = append(steps, sum)
|
|
|
|
hello := NewHelloStep()
|
|
steps = append(steps, hello)
|
|
|
|
return steps
|
|
}
|