Skip to content

go-keg/go-job

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go job

Usage

Install

go get github.com/go-keg/go-job

Example

./example

package main

import (
	"context"
	"errors"
	syslog "log"
	"os"
	"time"

	"github.com/go-keg/go-job"
	"github.com/go-keg/go-job/report"
	"github.com/go-kratos/kratos/v2/log"
	"golang.org/x/time/rate"
)

func main() {
	j := job.NewJob(
		log.DefaultLogger,
		job.NewWorker("test", example),
		job.NewWorker(
			"test-with-limiter",
			example,
			job.WithLimiter(rate.NewLimiter(rate.Every(time.Second), 10)),
		),
		job.NewWorker(
			"test-with-report-error",
			example,
			job.WithReport(report.NewQYWeiXinReport(os.Getenv("QY_WECHAT_TOKEN"))),
		),
	)
	err := j.Start(context.Background())
	if err != nil {
		panic(err)
	}
}

func example(ctx context.Context) error {
	syslog.Println("do something...")
	if time.Now().Second()%10 == 1 {
		// test report error
		return errors.New("test err")
	}
	return nil
}