Modified the api, added the interrupt for the example.
This commit is contained in:
parent
79e383a639
commit
b8ec3fd242
@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
client := client.NewClient()
|
client := client.New()
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
if err := client.AddServer("127.0.0.1:4730"); err != nil {
|
if err := client.AddServer("127.0.0.1:4730"); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
|
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bitbucket.org/mikespook/gearman-go/gearman"
|
"bitbucket.org/mikespook/gearman-go/gearman"
|
||||||
"bitbucket.org/mikespook/gearman-go/gearman/worker"
|
"bitbucket.org/mikespook/gearman-go/gearman/worker"
|
||||||
|
"bitbucket.org/mikespook/golib/util"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
@ -14,12 +15,18 @@ func ToUpper(job *worker.WorkerJob) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
w := worker.NewWorker()
|
w := worker.New()
|
||||||
defer w.Close()
|
|
||||||
w.AddServer("127.0.0.1:4730")
|
w.AddServer("127.0.0.1:4730")
|
||||||
w.AddFunction("ToUpper", ToUpper, 0)
|
w.AddFunction("ToUpper", ToUpper, 0)
|
||||||
w.AddFunction("ToUpperTimeOut5", ToUpper, 5)
|
w.AddFunction("ToUpperTimeOut5", ToUpper, 5)
|
||||||
|
|
||||||
|
// Catch the interrupt to exit the working loop.
|
||||||
|
sh := util.NewSignalHandler(func() bool {
|
||||||
|
w.Close()
|
||||||
|
return true
|
||||||
|
}, func() bool {return true})
|
||||||
|
go sh.Loop()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
log.Println("start worker")
|
log.Println("start worker")
|
||||||
for {
|
for {
|
||||||
|
@ -31,7 +31,7 @@ type Client struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create a new client.
|
// Create a new client.
|
||||||
func NewClient() (client *Client) {
|
func New() (client *Client) {
|
||||||
client = &Client{JobQueue: make(chan *ClientJob, gearman.QUEUE_CAP),
|
client = &Client{JobQueue: make(chan *ClientJob, gearman.QUEUE_CAP),
|
||||||
incoming: make(chan []byte, gearman.QUEUE_CAP),
|
incoming: make(chan []byte, gearman.QUEUE_CAP),
|
||||||
UId: 1}
|
UId: 1}
|
||||||
|
@ -46,7 +46,7 @@ type Worker struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get a new worker
|
// Get a new worker
|
||||||
func NewWorker() (worker *Worker) {
|
func New() (worker *Worker) {
|
||||||
worker = &Worker{
|
worker = &Worker{
|
||||||
// job server list
|
// job server list
|
||||||
clients: make([]*jobAgent, 0, gearman.WORKER_SERVER_CAP),
|
clients: make([]*jobAgent, 0, gearman.WORKER_SERVER_CAP),
|
||||||
|
Loading…
Reference in New Issue
Block a user