RedisSpr

package module
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2022 License: MIT Imports: 9 Imported by: 1

README

RedisSpr

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/URedisSpr"
package main

import (
	"log"
	"time"

	"github.com/universe-30/RedisSpr"
)

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

	//or use function SetLogger(logger ULog.Logger) to use your own logger which implemented the ULog.Logger interface
	err = sprJobMgr.AddSprJob("testJob1")
	if err != nil {
		log.Println(err)
	}
	err = sprJobMgr.AddSprJob("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"))
		}
	}()

	// use function RemoveSprJob("jobName") to remove the job
	// removed job always return false when use IsMaster("jobName")
	time.AfterFunc(time.Second*25, func() {
		sprJobMgr.RemoveSprJob("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
}

type SprJob

type SprJob struct {
	JobName         string
	IsMaster        bool
	JobRand         string
	LoopIntervalSec int
	StopFlag        bool
	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(jobName string) error

func (*SprJobMgr) GetULogger added in v0.1.10

func (smgr *SprJobMgr) GetULogger() ULog.Logger

func (*SprJobMgr) IsMaster

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

func (*SprJobMgr) RemoveSprJob

func (smgr *SprJobMgr) RemoveSprJob(jobName string)

func (*SprJobMgr) SetULogger added in v0.1.10

func (smgr *SprJobMgr) SetULogger(logger ULog.Logger)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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