-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP
- Loading branch information
Showing
8 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module healthcheck | ||
|
||
go 1.22.1 | ||
|
||
require github.com/mikespook/gearman-go v0.0.0-20220520031403-2a518e866145 // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/mikespook/gearman-go v0.0.0-20220520031403-2a518e866145 h1:6kTCi6p3Hd6JYROnq+1UOdewoXj90zKKDQPlsHYTSEs= | ||
github.com/mikespook/gearman-go v0.0.0-20220520031403-2a518e866145/go.mod h1:77Th6O6AZfMU6i5hLJnjN5xxUBoio7LN0aOyxGhqV1U= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"sync" | ||
|
||
"github.com/mikespook/gearman-go/client" | ||
) | ||
|
||
func main() { | ||
var wg sync.WaitGroup | ||
|
||
c, err := client.New(client.Network, "127.0.0.1:4730") | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
defer c.Close() | ||
c.ErrorHandler = func(e error) { | ||
log.Println(e) | ||
} | ||
echo := []byte("ping") | ||
wg.Add(1) | ||
|
||
_, err = c.Echo(echo) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
wg.Done() | ||
jobHandler := func(resp *client.Response) { | ||
if string(resp.Data) == "pong" { | ||
log.Println("Health check passed") | ||
} else { | ||
log.Println("Health check failed") | ||
} | ||
log.Printf("%s", resp.Data) | ||
wg.Done() | ||
} | ||
handle, err := c.Do("ping", echo, client.JobNormal, jobHandler) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
wg.Add(1) | ||
status, err := c.Status(handle) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
log.Printf("%v", *status) | ||
|
||
wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module healthcheck | ||
|
||
go 1.22.1 | ||
|
||
require ( | ||
github.com/mikespook/gearman-go v0.0.0-20220520031403-2a518e866145 // indirect | ||
github.com/mikespook/golib v0.0.0-20151119134446-38fe6917d34b // indirect | ||
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
github.com/mikespook/gearman-go v0.0.0-20220520031403-2a518e866145 h1:6kTCi6p3Hd6JYROnq+1UOdewoXj90zKKDQPlsHYTSEs= | ||
github.com/mikespook/gearman-go v0.0.0-20220520031403-2a518e866145/go.mod h1:77Th6O6AZfMU6i5hLJnjN5xxUBoio7LN0aOyxGhqV1U= | ||
github.com/mikespook/golib v0.0.0-20151119134446-38fe6917d34b h1:v99/2GZfv/W/HuD17/QcHucnDtUsY9strTTwG82EuZY= | ||
github.com/mikespook/golib v0.0.0-20151119134446-38fe6917d34b/go.mod h1:3lBg8mdFpiF0C4ibbSFsxr+rgPWWkFlEVD/s11OG5k4= | ||
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw= | ||
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/mikespook/gearman-go/worker" | ||
"github.com/mikespook/golib/signal" | ||
) | ||
|
||
func ping(job worker.Job) ([]byte, error) { | ||
log.Printf("Received job: %s", job.Data()) | ||
return []byte("pong"), nil | ||
} | ||
|
||
func main() { | ||
w := worker.New(worker.Unlimited) | ||
defer w.Close() | ||
|
||
connect := func() error { | ||
return w.AddServer("tcp4", "localhost:4730") | ||
} | ||
for i := 0; i < 5; i++ { | ||
err := connect() | ||
if err == nil { | ||
break | ||
} | ||
if i == 4 { | ||
log.Fatalf("Failed to connect to gearman server after several retries: %v", err) | ||
} | ||
log.Printf("Could not connect to server: %v. Retrying in 5 seconds...", err) | ||
time.Sleep(5 * time.Second) | ||
} | ||
w.AddFunc("ping", ping, worker.Unlimited) | ||
|
||
if err := w.Ready(); err != nil { | ||
log.Fatal(err) | ||
return | ||
} | ||
go w.Work() | ||
signal.Bind(os.Interrupt, func() uint { return signal.BreakExit }) | ||
signal.Wait() | ||
} |