Skip to content

Commit

Permalink
gocron v2 example (#28)
Browse files Browse the repository at this point in the history
* Update README.md

* feat: outlink to production example

* feat: gocron v2 example
  • Loading branch information
seriouspoop authored Jul 13, 2024
1 parent b7b8376 commit c1e48fe
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 29 deletions.
59 changes: 33 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ go get github.com/go-co-op/gocron-etcd-elector
```

## usage
### gocron v2
For a full production environment example with etcd clusters, you can see [gocron-etcd-elector-example](https://github.com/seriouspoop/gocron-etcd-elector-example)

### gocron v1
Here is an example usage that would be deployed in multiple instances.

```go
Expand All @@ -16,56 +19,60 @@ package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"

"github.com/go-co-op/gocron"
elector "github.com/go-co-op/gocron-etcd-elector"
"github.com/go-co-op/gocron/v2"
)

func main() {
// Printing some value to verify if container is running in docker logs
fmt.Println("go-app")

// Configuring elector with etdc cluster
cfg := elector.Config{
Endpoints: []string{"http://127.0.0.1:2379"},
Endpoints: []string{"http://etcd-1:2379", "http://etcd-2:2379", "http://etcd-3:2379"},
DialTimeout: 3 * time.Second,
}

// Build new elector
el, err := elector.NewElector(context.Background(), cfg, elector.WithTTL(10))
if err != nil {
panic(err)
}

// el.Start() is a blocking method
// so running with goroutine
go func() {
for {
err := el.Start("/gocron/elector")
if err == elector.ErrClosed {
return
}

time.Sleep(1e9)
err := el.Start("/rahul-gandhi/pappu") // specify a directory for storing key value for election
if err == elector.ErrClosed {
return
}
}()

s := gocron.NewScheduler(time.UTC)
s.WithDistributedElector(el)
// New scheduler with elector
sh, err := gocron.NewScheduler(gocron.WithDistributedElector(el))
if err != nil {
panic(err)
}

_, _ = s.Every("1s").Do(func() {
// The scheduler elected as the leader is only allowed to run, other instances don't execute
_, err = sh.NewJob(gocron.DurationJob(1*time.Second), gocron.NewTask(func() {
// This if statement doesn't work as intended as only the leader is running
// So true always
if el.IsLeader(context.Background()) == nil {
fmt.Println("the current instance is leader")
fmt.Println("Current instance is leader")
fmt.Println("executing job")
} else {
fmt.Println("the current leader is", el.GetLeaderID())
// To see this log printed remove gocron.WithDistributedElector(el) option from the scheduler
fmt.Printf("Not leader, current leader is %s\n", el.GetLeaderID())
}
}))
if err != nil {
panic(err)
}

fmt.Println("call 1s")
})

s.StartAsync()

c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
<-c

sh.Start()
fmt.Println("exit")
}
```
File renamed without changes.
61 changes: 61 additions & 0 deletions example/example_v2/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"context"
"fmt"
"time"

elector "github.com/go-co-op/gocron-etcd-elector"
"github.com/go-co-op/gocron/v2"
)

func main() {
// Printing some value to verify if container is running in docker logs
fmt.Println("go-app")

// Configuring elector with etdc cluster
cfg := elector.Config{
Endpoints: []string{"http://etcd-1:2379", "http://etcd-2:2379", "http://etcd-3:2379"},
DialTimeout: 3 * time.Second,
}

// Build new elector
el, err := elector.NewElector(context.Background(), cfg, elector.WithTTL(10))
if err != nil {
panic(err)
}

// el.Start() is a blocking method
// so running with goroutine
go func() {
err := el.Start("/rahul-gandhi/pappu") // specify a directory for storing key value for election
if err == elector.ErrClosed {
return
}
}()

// New scheduler with elector
sh, err := gocron.NewScheduler(gocron.WithDistributedElector(el))
if err != nil {
panic(err)
}

// The scheduler elected as the leader is only allowed to run, other instances don't execute
_, err = sh.NewJob(gocron.DurationJob(1*time.Second), gocron.NewTask(func() {
// This if statement doesn't work as intended as only the leader is running
// So true always
if el.IsLeader(context.Background()) == nil {
fmt.Println("Current instance is leader")
fmt.Println("executing job")
} else {
// To see this log printed remove gocron.WithDistributedElector(el) option from the scheduler
fmt.Printf("Not leader, current leader is %s\n", el.GetLeaderID())
}
}))
if err != nil {
panic(err)
}

sh.Start()
fmt.Println("exit")
}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
Expand All @@ -30,9 +30,13 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require github.com/go-co-op/gocron/v2 v2.10.0

require (
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.12 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
)
12 changes: 10 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-co-op/gocron v1.37.0 h1:ZYDJGtQ4OMhTLKOKMIch+/CY70Brbb1dGdooLEhh7b0=
github.com/go-co-op/gocron v1.37.0/go.mod h1:3L/n6BkO7ABj+TrfSVXLRzsP26zmikL4ISkLQ0O8iNY=
github.com/go-co-op/gocron/v2 v2.10.0 h1:qmyqSBDp1Xi59PKI+venVbwXe9N17gwup/6aXXPFJfg=
github.com/go-co-op/gocron/v2 v2.10.0/go.mod h1:xY7bJxGazKam1cz04EebrlP4S9q4iWdiAylMGP3jY9w=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4=
github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down Expand Up @@ -60,13 +65,16 @@ go.etcd.io/etcd/client/v3 v3.5.12/go.mod h1:tSbBCakoWmmddL+BKVAJHa9km+O/E+bumDe9
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/zap v1.17.0 h1:MTjgFu6ZLKvY6Pvaqk97GlxNBuMpV4Hy/3P6tRGlI2U=
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand Down

0 comments on commit c1e48fe

Please sign in to comment.