job

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 10, 2024 License: MIT Imports: 8 Imported by: 0

README

go job

Usage

Install
go get github.com/eiixy/go-job
Example

./example

package main

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

	"github.com/go-kratos/kratos/v2/log"
	"golang.org/x/time/rate"

	"github.com/eiixy/go-job"
	"github.com/eiixy/go-job/report"
)

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
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler func(ctx context.Context) error

type Job

type Job struct {
	// contains filtered or unexported fields
}

func NewJob

func NewJob(logger log.Logger, works ...*Worker) *Job

func NewJobWithReport added in v0.2.1

func NewJobWithReport(logger log.Logger, report ReportError, works ...*Worker) *Job

func (Job) Start

func (j Job) Start(ctx context.Context) error

type ReportError

type ReportError interface {
	Report(ctx context.Context, worker, content string) error
}

type Worker

type Worker struct {
	// contains filtered or unexported fields
}

func NewWorker

func NewWorker(name string, job Handler, opts ...WorkerOption) *Worker

type WorkerOption

type WorkerOption func(worker *Worker)

func WithLimiter

func WithLimiter(limiter *rate.Limiter) WorkerOption

func WithLimiterDuration added in v0.2.1

func WithLimiterDuration(duration time.Duration) WorkerOption

func WithReport

func WithReport(report ReportError) WorkerOption

func WithSleep

func WithSleep(duration time.Duration) WorkerOption

Directories

Path Synopsis
report

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL