gearman-go/example/worker.go
mikespook e0c1537e8e code refactoring, upgrade to GO1 RC2
--HG--
rename : README => README.md
rename : src/gearman/client.go => gearman/client/client.go
rename : src/gearman/client_test.go => gearman/client/client_test.go
rename : src/gearman/clientjob.go => gearman/client/clientjob.go
rename : src/gearman/gearman.go => gearman/gearman.go
rename : src/gearman/jobagent.go => gearman/worker/jobagent.go
rename : src/gearman/worker.go => gearman/worker/worker.go
rename : src/gearman/worker_test.go => gearman/worker/worker_test.go
rename : src/gearman/workerjob.go => gearman/worker/workerjob.go
2012-03-26 13:32:59 +08:00

49 lines
1.2 KiB
Go

package main
import (
"bitbucket.org/mikespook/gearman-go/gearman"
"bitbucket.org/mikespook/gearman-go/gearman/worker"
"fmt"
"log"
"strings"
)
func ToUpper(job *worker.WorkerJob) ([]byte, error) {
data := []byte(strings.ToUpper(string(job.Data)))
return data, nil
}
func main() {
w := worker.NewWorker()
defer w.Close()
w.AddServer("127.0.0.1:4730")
w.AddFunction("ToUpper", ToUpper, 0)
w.AddFunction("ToUpperTimeOut5", ToUpper, 5)
go func() {
log.Println("start worker")
for {
print("cmd: ")
var str string
fmt.Scan(&str)
switch str {
case "echo":
w.Echo([]byte("Hello world!"))
var job *worker.WorkerJob
for job = <-w.JobQueue; job.DataType != gearman.ECHO_RES; job = <-w.JobQueue {
log.Println(job)
}
log.Println(string(job.Data))
case "quit":
return
case "result":
job := <-w.JobQueue
log.Println(string(job.Data))
default:
log.Println("Unknown command")
}
}
}()
w.Work()
}