├── .gitignore ├── Makefile ├── call.go ├── client.go ├── gearman.go ├── gearman_test.go └── worker.go /.gitignore: -------------------------------------------------------------------------------- 1 | *.8 2 | *.6 3 | _obj 4 | _test 5 | _testmain.go 6 | *.out 7 | *~ 8 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | include $(GOROOT)/src/Make.inc 2 | 3 | TARG=danga/gearman 4 | GOFILES=\ 5 | gearman.go\ 6 | client.go\ 7 | call.go\ 8 | worker.go\ 9 | 10 | include $(GOROOT)/src/Make.pkg 11 | -------------------------------------------------------------------------------- /call.go: -------------------------------------------------------------------------------- 1 | // The "caller" side of a Gearman client. 2 | 3 | package gearman 4 | 5 | import ( 6 | "log" 7 | ) 8 | 9 | func (c *client) Call(method string, data []byte) []byte { 10 | return c.CallWithProgress(method, data, nil) 11 | } 12 | 13 | func (c *client) CallWithProgress(method string, data []byte, progress ProgressHandler) []byte { 14 | // TODO: implement 15 | 16 | // TODO: shard onto a c.hosts[] entry, wait for its connection state, etc. 17 | log.Exitf("TODO: implement CallWithProgress") 18 | return nil 19 | } 20 | -------------------------------------------------------------------------------- /client.go: -------------------------------------------------------------------------------- 1 | package gearman 2 | 3 | import ( 4 | "sync" 5 | ) 6 | 7 | type client struct { 8 | mutex sync.Mutex 9 | hosts []string 10 | hostState []hostState 11 | 12 | handlers map[string]func(job *IncomingJob) []byte 13 | } 14 | 15 | type hostState struct { 16 | } 17 | -------------------------------------------------------------------------------- /gearman.go: -------------------------------------------------------------------------------- 1 | package gearman 2 | 3 | type Job struct { 4 | Method string 5 | Data []byte 6 | } 7 | 8 | type IncomingJob struct { 9 | *Job 10 | } 11 | 12 | type ProgressHandler interface { 13 | OnProgress(done int, total int) 14 | } 15 | 16 | type Client interface { 17 | // For being a worker: 18 | RegisterWorker(method string, handler func(job *IncomingJob) []byte) 19 | Work() 20 | 21 | // For being a client: 22 | Call(method string, data []byte) []byte 23 | CallWithProgress(method string, data []byte, progress ProgressHandler) []byte 24 | } 25 | 26 | func (ij *IncomingJob) SendProgress(done int, total int) { 27 | // TODO: implement 28 | } 29 | 30 | func NewClient(hostport string) Client { 31 | return &client{hosts: []string{hostport}} 32 | } 33 | 34 | func NewLoadBalancedClient(hostports []string) Client { 35 | return &client{hosts: hostports} 36 | } 37 | -------------------------------------------------------------------------------- /gearman_test.go: -------------------------------------------------------------------------------- 1 | package gearman 2 | 3 | import ( 4 | "net" 5 | "testing" 6 | ) 7 | 8 | const TEST_ROUTER = "localhost:4730" 9 | 10 | func routerIsUp(hostPort string) bool { 11 | c, err := net.Dial("tcp", "", hostPort) 12 | if err != nil { 13 | return false 14 | } 15 | c.Close() 16 | return true 17 | } 18 | 19 | func TestFoo(t *testing.T) { 20 | if !routerIsUp(TEST_ROUTER) { 21 | t.Fatalf("Can't run unit tests without gearmand running on %s", TEST_ROUTER) 22 | } 23 | 24 | c := NewClient(TEST_ROUTER) 25 | if c == nil { 26 | t.Fatal("Got nil client") 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /worker.go: -------------------------------------------------------------------------------- 1 | // The "worker" side of a Gearman client. 2 | 3 | package gearman 4 | 5 | import ( 6 | "log" 7 | ) 8 | 9 | func (j *IncomingJob) SetProgress(done int, total int) { 10 | // TODO: send progress packets 11 | } 12 | 13 | func (c *client) RegisterWorker(method string, handler func(job *IncomingJob) []byte) { 14 | c.mutex.Lock() 15 | defer c.mutex.Unlock() 16 | if c.handlers == nil { 17 | c.handlers = make(map[string]func(job *IncomingJob) []byte) 18 | } 19 | c.handlers[method] = handler 20 | } 21 | 22 | func (c *client) Work() { 23 | log.Exitf("TODO: implement Work()") 24 | 25 | // TODO: send CANDO packets to job servers 26 | // TODO: block in a loop waiting on work packets 27 | } 28 | --------------------------------------------------------------------------------