Centralized Lock for Distributed Systems for Go Schedule Tasks
goInterLock
is cron-job/Schedule tasks with a centralized locking mechanism for Go (AKA Golang).
In the distributed systems, the lock prevents the tasks to been executed in every instant that has the scheduler.
For example, if your application has a scheduled task of calling some external APIs or doing some expensive DataBase querying every 10 minutes, the lock prevents the process to be run in every instance of that application, and you ended up running that task multiple times every 10 minutes.
Use case:
- Kubernetes
- Docker Swarm
- AWS ECS Fargate
Quick Start
go get github.com/ehsaniara/gointerlock
Supported Lock
- Redis
- AWS DynamoDB
- Postgres DB (coming soon)
Local Scheduler (Single App)
(Interval every 2 seconds)
var job = gointerlock.GoInterval{
Interval: 2 * time.Second,
Arg: myJob,
}
err := job.Run(ctx)
if err != nil {
log.Fatalf("Error: %s", err)
}
Examples
Basic Local Task: Simple Task Interval (Single App).
Application Cache: An example of periodically cached value update on http server.
Distributed Mode (Scaled Up)
Redis (Recommended)
Existing Redis Connection
you should already configure your Redis connection and pass it into the GoInterLock
. Also, make sure you are giving the unique name per job
Step 1: configure Redis connection redisConnection.Rdb
from the existing application and pass it into the Job. for example:
var redisConnector = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "myRedisPassword",
DB: 0,
})
Step 2: Pass the Redis connection into the GoInterval
var job = gointerlock.GoInterval{
Interval: 2 * time.Second,
Arg: myJob,
Name: "MyTestJob",
RedisConnector: redisConnector,
}
err := jobTicker.Run(ctx)
if err != nil {
log.Fatalf("Error: %s", err)
}
in both examples myJob
is your function, for example:
func myJob() {
fmt.Println(time.Now(), " - called")
}
Note: currently GoInterLock
does not support any argument for the job function