diff --git a/example/Makefile b/example/Makefile new file mode 100644 index 0000000..5bf1f39 --- /dev/null +++ b/example/Makefile @@ -0,0 +1,18 @@ +# Copyright 2009 The Go Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +include $(GOROOT)/src/Make.inc + +TARG=gearman + +# GOFILES=\ +# worker.go\ + +CLEANFILES+= worker + +include $(GOROOT)/src/Make.pkg + +%: %.go + $(GC) $*.go + $(LD) -o $@ $*.$O diff --git a/example/worker.go b/example/worker.go new file mode 100644 index 0000000..b5a9853 --- /dev/null +++ b/example/worker.go @@ -0,0 +1,45 @@ +package main + +import ( + "gearman" + "fmt" + "log" + "os" + "strings" +) + +func ToUpper(job *gearman.Job) ([]byte, os.Error) { + data := []byte(strings.ToUpper(string(job.Data))) + return data, nil +} + +func main() { + worker := gearman.NewWorker() + worker.AddServer("127.0.0.1:4730") + worker.AddFunction("ToUpper", ToUpper, 0) + worker.AddFunction("ToUpperTimeOut5", ToUpper, 5) + + go func() { + log.Println("start worker") + for { + print("cmd: ") + var str string + fmt.Scan(&str) + switch str { + case "echo": + worker.Echo([]byte("Hello world!")) + job := <-worker.Queue + log.Println(string(job.Data)) + case "quit": + worker.Close() + return + case "result": + job := <-worker.Queue + log.Println(string(job.Data)) + default: + log.Println("Unknown command") + } + } + }() + worker.Work() +}