redis_spr

package module
v0.1.30 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2023 License: MIT Imports: 11 Imported by: 3

README

redis_spr

Make sure specific job can only be run over 1 process among all machines and processes. If some process is killed or crashed, the job will be switched to other process on some machine.

usage
go get "github.com/universe-30/redis_spr"
 package main

import (
	"context"
	"log"
	"time"

	"github.com/coreservice-io/redis_spr"
)

func main() {
	sprJobMgr, err := redis_spr.New(redis_spr.RedisConfig{
		Addr:   "127.0.0.1",
		Port:   6379,
		Prefix: "sprExample",
	})
	if err != nil {
		log.Fatalln(err)
	}

	//or use function SetLogger(logger log.Logger) to use your own logger which implemented the log.Logger interface
	err = sprJobMgr.AddSprJob(context.Background(), "testJob1")
	if err != nil {
		log.Println(err)
	}
	err = sprJobMgr.AddSprJob(context.Background(), "testJob2")
	if err != nil {
		log.Println(err)
	}

	// use function IsMaster("jobName") to check whether the process get the master token or not
	// if return true means get the master token
	go func() {
		for {
			time.Sleep(time.Second)
			log.Println("testjob is master:", sprJobMgr.IsMaster("testJob1"))
			log.Println("testjob2 is master:", sprJobMgr.IsMaster("testJob2"))
		}
	}()

	time.Sleep(1 * time.Hour)
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RedisConfig

type RedisConfig struct {
	Addr     string
	Port     int
	UserName string
	Password string
	Prefix   string
	UseTLS   bool
}

type SprJob

type SprJob struct {
	JobName         string
	IsMaster        bool
	JobRand         string
	LoopIntervalSec int
	Ctx             context.Context
	LastRuntime     int64
	// contains filtered or unexported fields
}

type SprJobMgr

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

func New

func New(config RedisConfig) (*SprJobMgr, error)

func (*SprJobMgr) AddSprJob

func (smgr *SprJobMgr) AddSprJob(ctx context.Context, jobName string) error

func (*SprJobMgr) GetLogger

func (smgr *SprJobMgr) GetLogger() log.Logger

func (*SprJobMgr) IsMaster

func (smgr *SprJobMgr) IsMaster(jobName string) bool

func (*SprJobMgr) SetLogger

func (smgr *SprJobMgr) SetLogger(logger log.Logger)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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