xxl-job hosting

  1. import (
  2. "github.com/yoyofx/yoyogo/abstractions/configuration"
  3. "github.com/yoyofx/yoyogo/pkg/scheduler"
  4. "github.com/yoyofxteam/dependencyinjection"
  5. )
  6. func main() {
  7. // -f ./conf/test_conf.yml 指定配置文件 , 默认读取 config_{profile}.yml , -profile [dev,test,prod]
  8. config := configuration.YAML("config")
  9. scheduler.NewXxlJobBuilder(config).
  10. ConfigureServices(func(collection *dependencyinjection.ServiceCollection){
  11. scheduler.AddJobs(collection, NewDemoJob)
  12. }).
  13. Build().Run()
  14. }

Job

  1. type DemoJob struct {
  2. }
  3. func NewDemoJob() *DemoJob {
  4. return &DemoJob{}
  5. }
  6. func (*DemoJob) Execute(cxt *scheduler.JobContext) (msg string) {
  7. cxt.Report("Job %d is beginning...", cxt.LogID)
  8. for i := 1; i <= 100; i++ {
  9. cxt.Report("Job Progress: %d Percent.", i)
  10. time.Sleep(time.Second)
  11. }
  12. return cxt.Done("666")
  13. }
  14. //GetJobName 自定义任务的名字
  15. func (*DemoJob) GetJobName() string {
  16. return "job1"
  17. }

config.yml

  1. yoyogo:
  2. application:
  3. name: console-xxl-job
  4. metadata: "dev"
  5. server:
  6. type: "console" # 宿主类型是 console
  7. xxl:
  8. serverAddr: http://127.0.0.1:8080/xxl-job-admin/
  9. port: 9999