Giter Site home page Giter Site logo

spr-go's Introduction

SPR-go

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

usage

go get github.com/daqnext/SPR-go
package main

import (
	"testing"
	"time"

	localLog "github.com/daqnext/LocalLog/log"
	SPR_go "github.com/daqnext/SPR-go"
)

func main(){

	lg, err := localLog.New("logs", 10, 10, 10)
	if err != nil {
		panic(err)
	}

	sMgr, err := SPR_go.New(SPR_go.RedisConfig{
		Addr: "127.0.0.1",
		Port: 6379,
	}, lg)
	if err != nil {
		lg.Errorln(err)
		return
	}

	//add job with unique job name which used in redis
	//the process with same job name will scramble for the master token
	err = sMgr.AddJobName("testjob")
	if err != nil {
		lg.Println(err)
	}
	err = sMgr.AddJobName("testjob2")
	if err != nil {
		lg.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)
			lg.Println("testjob is master:", sMgr.IsMaster("testjob"))
			lg.Println("testjob2 is master:", sMgr.IsMaster("testjob2"))
		}
	}()

	// use function RemoveJobName("jobName") to remove the job
	// removed job always return false when use IsMaster("jobName")
	time.AfterFunc(time.Second*25, func() {
		sMgr.RemoveJobName("testjob2")
	})

	time.Sleep(1 * time.Hour)

}

spr-go's People

Watchers

hellouniverse avatar Sherlock avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.