From a6fab88b22e90112d48fc179212e59acb4ecd885 Mon Sep 17 00:00:00 2001 From: mikespook Date: Wed, 18 May 2011 20:24:59 +0800 Subject: [PATCH] added the example for worker --- example/Makefile | 18 ++++++++++++++++++ example/worker.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 example/Makefile create mode 100644 example/worker.go 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() +}