crontab

package
v2.26.3 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2024 License: MIT Imports: 4 Imported by: 0

README

Crontab

Example

package main

import (
	"context"

	"github.com/flc1125/go-cron/v4"
	"github.com/go-kratos/kratos/v2"
	"github.com/redis/go-redis/v9"

	"github.com/go-kratos-ecosystem/components/v2/crontab"
	"github.com/go-kratos-ecosystem/components/v2/crontab/middleware/distributednooverlapping"
	"github.com/go-kratos-ecosystem/components/v2/crontab/middleware/distributednooverlapping/redismutex"
)

type testJob struct {
	distributednooverlapping.DefaultTTLJobWithMutex // optional, if you don't need to implement GetMutexTTL
}

var _ distributednooverlapping.JobWithMutex = (*testJob)(nil)

func (m *testJob) Run(context.Context) error {
	// do something
	return nil
}

func (m *testJob) GetMutexKey() string {
	return "test"
}

func main() {
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
	})

	c := cron.New(
		cron.WithSeconds(),
		cron.WithMiddleware(
			distributednooverlapping.New(
				redismutex.New(rdb, redismutex.WithPrefix("cron:mutex")),
			),
		),
	)

	_, _ = c.AddJob("* * * * * *", &testJob{})

	// kratos app start
	app := kratos.New(
		kratos.Server(
			crontab.NewServer(c),
		),
	)

	err := app.Run()
	if err != nil {
		panic(err)
	}
}

Documentation

Overview

Example
package main

import (
	"context"

	"github.com/flc1125/go-cron/v4"
	"github.com/go-kratos/kratos/v2"
	"github.com/redis/go-redis/v9"

	"github.com/go-kratos-ecosystem/components/v2/crontab"
	"github.com/go-kratos-ecosystem/components/v2/crontab/middleware/distributednooverlapping"
	"github.com/go-kratos-ecosystem/components/v2/crontab/middleware/distributednooverlapping/redismutex"
)

type testJob struct {
	distributednooverlapping.DefaultTTLJobWithMutex // optional, if you don't need to implement GetMutexTTL
}

var _ distributednooverlapping.JobWithMutex = (*testJob)(nil)

func (m *testJob) Run(context.Context) error {
	// do something
	return nil
}

func (m *testJob) GetMutexKey() string {
	return "test"
}

func main() {
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
	})

	c := cron.New(
		cron.WithSeconds(),
		cron.WithMiddleware(
			distributednooverlapping.New(
				redismutex.New(rdb, redismutex.WithPrefix("cron:mutex")),
			),
		),
	)

	_, _ = c.AddJob("* * * * * *", &testJob{})

	// kratos app start
	app := kratos.New(
		kratos.Server(
			crontab.NewServer(c),
		),
	)

	err := app.Run()
	if err != nil {
		panic(err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger added in v2.10.0

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

func NewLogger added in v2.10.0

func NewLogger(logger log.Logger) *Logger

func (*Logger) Printf added in v2.10.0

func (l *Logger) Printf(format string, v ...any)

type Server

type Server struct {
	*cron.Cron
}

func NewServer

func NewServer(c *cron.Cron) *Server

func (*Server) Start

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

func (*Server) Stop

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

Directories

Path Synopsis
middleware

Jump to

Keyboard shortcuts

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