Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/skynet2/singleton-task
https://github.com/skynet2/singleton-task
Last synced: about 12 hours ago
JSON representation
- Host: GitHub
- URL: https://github.com/skynet2/singleton-task
- Owner: skynet2
- License: mit
- Created: 2022-10-19T14:49:19.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-03-17T00:54:32.000Z (8 months ago)
- Last Synced: 2024-06-20T06:24:46.140Z (5 months ago)
- Language: Go
- Size: 34.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# singleton-task
![build workflow](https://github.com/skynet2/singleton-task/actions/workflows/build.yaml/badge.svg?branch=master)
[![codecov](https://codecov.io/gh/skynet2/singleton-task/branch/master/graph/badge.svg?token=Y71ZQHTLKC)](https://codecov.io/gh/skynet2/singleton-task)
[![go-report](https://goreportcard.com/badge/github.com/skynet2/singleton-task?nocache=true)](https://goreportcard.com/report/github.com/skynet2/singleton-task)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/skynet2/singleton-task)](https://pkg.go.dev/github.com/skynet2/singleton-task?tab=doc)## Description
Due to the nature of microservices, k8s etc., we have multiple pods\instances of the same service, but in some cases, there is a
the requirement to run the specific job in a single instance, for example, some parsing jobs with rate limiting, database migrations, etc.
Here come the leader election algorithms.This implementation is targeted for [redlock](https://redis.io/docs/reference/patterns/distributed-locks/) approach
## Installation
```shell
go get github.com/skynet2/singleton-task
```## Quickstart
```go
package mainimport (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time""github.com/bsm/redislock"
"github.com/redis/go-redis/v9"
singletonTask "github.com/skynet2/singleton-task"
)func main() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)instance := singletonTask.NewSingletonRedLock(redislock.New(redis.NewClient(&redis.Options{
Network: "tcp",
Addr: "127.0.0.1:6379",
})), "long-running-job", func(ctx context.Context) {
for ctx.Err() == nil {
fmt.Println("dequeue from queue and send http request.")
time.Sleep(1 * time.Second)
}
}, context.Background(), 30*time.Second)if err := instance.StartAsync(); err != nil {
panic(err)
}fmt.Println("awaiting signal")
<-sig
_ = instance.Close()
}
```
More examples can be found inside tests