crontab

package
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2024 License: MIT Imports: 7 Imported by: 0

README

Crontab

Usage

package main

import (
	"log"

	"github.com/go-kratos/kratos/v2"
	"github.com/redis/go-redis/v9"
	"github.com/robfig/cron/v3"

	"github.com/go-packagist/go-kratos-components/crontab"
	redisMutex "github.com/go-packagist/go-kratos-components/crontab/mutex/redis"
)

func main() {
	c := cron.New(
		cron.WithSeconds(),
	)

	c.AddFunc("* * * * * *", func() {
		log.Println("Hello world")
	})

	app := kratos.New(
		kratos.Server(
			crontab.NewServer(c,
				crontab.WithMutex(redisMutex.New(redis.NewClient(&redis.Options{
					Addr: "localhost:6379",
				}))),
				crontab.WithDebug(),
			),
		),
	)

	app.Run()
}

output:

2023/12/25 14:25:56 crontab: server started
2023/12/25 14:25:57 Hello world
2023/12/25 14:25:58 Hello world
2023/12/25 14:25:59 Hello world
2023/12/25 14:26:00 Hello world

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAnotherServerRunning = errors.New("crontab: another server running")
)

Functions

This section is empty.

Types

type Mutex

type Mutex interface {
	Lock(ctx context.Context, name string) error
	Unlock(ctx context.Context, name string) error
}

type Option

type Option func(*Server)

func WithDebug

func WithDebug() Option

func WithMutex

func WithMutex(m Mutex) Option

func WithName

func WithName(name string) Option

type Server

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

func NewServer

func NewServer(c *cron.Cron, opts ...Option) *Server

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

func (*Server) Stop

func (s *Server) Stop(ctx context.Context) error

Directories

Path Synopsis
mutex

Jump to

Keyboard shortcuts

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