2012-03-26 13:32:59 +08:00
|
|
|
// Copyright 2011 Xing Xing <mikespook@gmail.com> All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
2013-12-23 17:05:42 +08:00
|
|
|
"encoding/binary"
|
2013-08-30 18:01:10 +08:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2013-12-23 17:05:42 +08:00
|
|
|
"time"
|
2012-03-26 13:32:59 +08:00
|
|
|
)
|
|
|
|
|
2012-05-10 21:25:33 +08:00
|
|
|
const (
|
2013-08-30 12:36:57 +08:00
|
|
|
Immediately = 0
|
2012-05-10 21:25:33 +08:00
|
|
|
)
|
2012-06-01 14:28:10 +08:00
|
|
|
|
2012-03-26 13:32:59 +08:00
|
|
|
/*
|
2012-05-23 17:45:52 +08:00
|
|
|
Worker side api for gearman
|
2012-03-26 13:32:59 +08:00
|
|
|
|
|
|
|
usage:
|
2013-12-24 17:41:11 +08:00
|
|
|
w = worker.New()
|
2012-05-24 16:49:35 +08:00
|
|
|
w.AddFunction("foobar", foobar)
|
|
|
|
w.AddServer("127.0.0.1:4730")
|
|
|
|
w.Work() // Enter the worker's main loop
|
2012-03-26 13:32:59 +08:00
|
|
|
|
|
|
|
The definition of the callback function 'foobar' should suit for the type 'JobFunction'.
|
|
|
|
It looks like this:
|
|
|
|
|
2012-05-23 17:45:52 +08:00
|
|
|
func foobar(job *Job) (data []byte, err os.Error) {
|
2012-03-26 13:32:59 +08:00
|
|
|
//sth. here
|
|
|
|
//plaplapla...
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
type Worker struct {
|
2013-12-24 17:41:11 +08:00
|
|
|
agents []*agent
|
2013-08-30 12:36:57 +08:00
|
|
|
funcs JobFuncs
|
2013-12-21 11:09:13 +08:00
|
|
|
in chan *inPack
|
2013-08-30 12:36:57 +08:00
|
|
|
running bool
|
|
|
|
|
|
|
|
Id string
|
|
|
|
// assign a ErrFunc to handle errors
|
2013-08-30 18:01:10 +08:00
|
|
|
ErrorHandler ErrorHandler
|
2013-12-23 17:05:42 +08:00
|
|
|
JobHandler JobHandler
|
|
|
|
mutex sync.Mutex
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get a new worker
|
2013-12-24 17:41:11 +08:00
|
|
|
func New() (worker *Worker) {
|
2013-08-30 12:36:57 +08:00
|
|
|
worker = &Worker{
|
2013-12-24 17:41:11 +08:00
|
|
|
agents: make([]*agent, 0),
|
2013-08-30 12:36:57 +08:00
|
|
|
funcs: make(JobFuncs),
|
2013-12-21 11:09:13 +08:00
|
|
|
in: make(chan *inPack, QUEUE_SIZE),
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
|
|
|
return
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
2013-08-30 12:36:57 +08:00
|
|
|
//
|
|
|
|
func (worker *Worker) err(e error) {
|
2013-08-30 18:01:10 +08:00
|
|
|
if worker.ErrorHandler != nil {
|
|
|
|
worker.ErrorHandler(e)
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
2012-05-10 21:25:33 +08:00
|
|
|
}
|
|
|
|
|
2012-03-26 13:32:59 +08:00
|
|
|
// Add a server. The addr should be 'host:port' format.
|
|
|
|
// The connection is established at this time.
|
2013-08-30 18:01:10 +08:00
|
|
|
func (worker *Worker) AddServer(net, addr string) (err error) {
|
2013-08-30 12:36:57 +08:00
|
|
|
// Create a new job server's client as a agent of server
|
2013-08-30 18:01:10 +08:00
|
|
|
a, err := newAgent(net, addr, worker)
|
2013-08-30 12:36:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-12-24 17:41:11 +08:00
|
|
|
worker.agents = append(worker.agents, a)
|
2013-08-30 12:36:57 +08:00
|
|
|
return
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
2012-05-24 16:49:35 +08:00
|
|
|
// Write a job to job server.
|
|
|
|
// Here, the job's mean is not the oraginal mean.
|
|
|
|
// Just looks like a network package for job's result or tell job server, there was a fail.
|
2013-12-21 11:09:13 +08:00
|
|
|
func (worker *Worker) broadcast(outpack *outPack) {
|
2013-08-30 12:36:57 +08:00
|
|
|
for _, v := range worker.agents {
|
2013-12-21 11:09:13 +08:00
|
|
|
v.write(outpack)
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
2012-05-24 16:49:35 +08:00
|
|
|
}
|
|
|
|
|
2012-03-26 13:32:59 +08:00
|
|
|
// Add a function.
|
|
|
|
// Plz added job servers first, then functions.
|
|
|
|
// The API will tell every connected job server that 'I can do this'
|
2012-05-24 16:49:35 +08:00
|
|
|
func (worker *Worker) AddFunc(funcname string,
|
2013-08-30 12:36:57 +08:00
|
|
|
f JobFunc, timeout uint32) (err error) {
|
2013-08-30 18:01:10 +08:00
|
|
|
worker.mutex.Lock()
|
|
|
|
defer worker.mutex.Unlock()
|
2013-08-30 12:36:57 +08:00
|
|
|
if _, ok := worker.funcs[funcname]; ok {
|
2013-08-30 18:01:10 +08:00
|
|
|
return fmt.Errorf("The function already exists: %s", funcname)
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
|
|
|
worker.funcs[funcname] = &jobFunc{f: f, timeout: timeout}
|
|
|
|
if worker.running {
|
|
|
|
worker.addFunc(funcname, timeout)
|
|
|
|
}
|
|
|
|
return
|
2012-05-24 16:49:35 +08:00
|
|
|
}
|
2012-03-26 13:32:59 +08:00
|
|
|
|
2012-05-24 16:49:35 +08:00
|
|
|
// inner add function
|
|
|
|
func (worker *Worker) addFunc(funcname string, timeout uint32) {
|
2013-12-21 11:09:13 +08:00
|
|
|
outpack := getOutPack()
|
2013-08-30 12:36:57 +08:00
|
|
|
if timeout == 0 {
|
2013-12-21 11:09:13 +08:00
|
|
|
outpack.dataType = CAN_DO
|
|
|
|
outpack.data = []byte(funcname)
|
2013-08-30 12:36:57 +08:00
|
|
|
} else {
|
2013-12-21 11:09:13 +08:00
|
|
|
outpack.dataType = CAN_DO_TIMEOUT
|
2013-08-30 18:01:10 +08:00
|
|
|
l := len(funcname)
|
2013-12-21 11:09:13 +08:00
|
|
|
outpack.data = getBuffer(l + 5)
|
|
|
|
copy(outpack.data, []byte(funcname))
|
|
|
|
outpack.data[l] = '\x00'
|
2013-12-23 17:05:42 +08:00
|
|
|
binary.BigEndian.PutUint32(outpack.data[l+1:], timeout)
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
2013-12-21 11:09:13 +08:00
|
|
|
worker.broadcast(outpack)
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a function.
|
2012-05-24 16:49:35 +08:00
|
|
|
func (worker *Worker) RemoveFunc(funcname string) (err error) {
|
2013-08-30 18:01:10 +08:00
|
|
|
worker.mutex.Lock()
|
|
|
|
defer worker.mutex.Unlock()
|
2013-08-30 12:36:57 +08:00
|
|
|
if _, ok := worker.funcs[funcname]; !ok {
|
2013-08-30 18:01:10 +08:00
|
|
|
return fmt.Errorf("The function does not exist: %s", funcname)
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
|
|
|
delete(worker.funcs, funcname)
|
|
|
|
if worker.running {
|
|
|
|
worker.removeFunc(funcname)
|
|
|
|
}
|
|
|
|
return
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
2012-05-24 16:49:35 +08:00
|
|
|
// inner remove function
|
|
|
|
func (worker *Worker) removeFunc(funcname string) {
|
2013-12-21 11:09:13 +08:00
|
|
|
outpack := getOutPack()
|
|
|
|
outpack.dataType = CANT_DO
|
|
|
|
outpack.data = []byte(funcname)
|
|
|
|
worker.broadcast(outpack)
|
2012-05-24 16:49:35 +08:00
|
|
|
}
|
|
|
|
|
2013-12-21 11:09:13 +08:00
|
|
|
func (worker *Worker) handleInPack(inpack *inPack) {
|
|
|
|
switch inpack.dataType {
|
2013-12-23 17:01:01 +08:00
|
|
|
case NO_JOB:
|
|
|
|
inpack.a.PreSleep()
|
|
|
|
case NOOP:
|
|
|
|
inpack.a.Grab()
|
2013-08-30 18:01:10 +08:00
|
|
|
case ERROR:
|
2013-12-21 11:09:13 +08:00
|
|
|
worker.err(GetError(inpack.data))
|
2013-08-30 18:01:10 +08:00
|
|
|
case JOB_ASSIGN, JOB_ASSIGN_UNIQ:
|
2013-12-21 11:09:13 +08:00
|
|
|
if err := worker.exec(inpack); err != nil {
|
2013-08-30 12:36:57 +08:00
|
|
|
worker.err(err)
|
|
|
|
}
|
|
|
|
default:
|
2013-12-21 11:09:13 +08:00
|
|
|
worker.customeHandler(inpack)
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
2012-12-29 23:54:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 17:01:01 +08:00
|
|
|
func (worker *Worker) Ready() (err error) {
|
|
|
|
for _, v := range worker.agents {
|
|
|
|
if err = v.Connect(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for funcname, f := range worker.funcs {
|
|
|
|
worker.addFunc(funcname, f.timeout)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-03-26 13:32:59 +08:00
|
|
|
// Main loop
|
|
|
|
func (worker *Worker) Work() {
|
2013-08-30 12:36:57 +08:00
|
|
|
worker.running = true
|
2013-12-24 14:35:33 +08:00
|
|
|
for _, v := range worker.agents {
|
|
|
|
v.Grab()
|
|
|
|
}
|
2013-12-21 11:09:13 +08:00
|
|
|
var inpack *inPack
|
|
|
|
for inpack = range worker.in {
|
|
|
|
go worker.handleInPack(inpack)
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
2012-05-24 16:49:35 +08:00
|
|
|
// job handler
|
2013-12-21 11:09:13 +08:00
|
|
|
func (worker *Worker) customeHandler(inpack *inPack) {
|
2013-08-30 12:36:57 +08:00
|
|
|
if worker.JobHandler != nil {
|
2013-12-21 11:09:13 +08:00
|
|
|
if err := worker.JobHandler(inpack); err != nil {
|
2013-08-30 12:36:57 +08:00
|
|
|
worker.err(err)
|
|
|
|
}
|
|
|
|
}
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
2012-05-24 16:49:35 +08:00
|
|
|
// Close.
|
|
|
|
func (worker *Worker) Close() {
|
2013-08-30 12:36:57 +08:00
|
|
|
worker.running = false
|
|
|
|
close(worker.in)
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send a something out, get the samething back.
|
2012-05-24 16:49:35 +08:00
|
|
|
func (worker *Worker) Echo(data []byte) {
|
2013-12-21 11:09:13 +08:00
|
|
|
outpack := getOutPack()
|
|
|
|
outpack.dataType = ECHO_REQ
|
|
|
|
outpack.data = data
|
|
|
|
worker.broadcast(outpack)
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all of functions.
|
|
|
|
// Both from the worker or job servers.
|
2012-05-24 16:49:35 +08:00
|
|
|
func (worker *Worker) Reset() {
|
2013-12-21 11:09:13 +08:00
|
|
|
outpack := getOutPack()
|
|
|
|
outpack.dataType = RESET_ABILITIES
|
|
|
|
worker.broadcast(outpack)
|
2013-08-30 12:36:57 +08:00
|
|
|
worker.funcs = make(JobFuncs)
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the worker's unique id.
|
2012-05-24 16:49:35 +08:00
|
|
|
func (worker *Worker) SetId(id string) {
|
2013-08-30 12:36:57 +08:00
|
|
|
worker.Id = id
|
2013-12-21 11:09:13 +08:00
|
|
|
outpack := getOutPack()
|
|
|
|
outpack.dataType = SET_CLIENT_ID
|
|
|
|
outpack.data = []byte(id)
|
|
|
|
worker.broadcast(outpack)
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the job. And send back the result.
|
2013-12-21 11:09:13 +08:00
|
|
|
func (worker *Worker) exec(inpack *inPack) (err error) {
|
2013-08-30 12:36:57 +08:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
if e, ok := r.(error); ok {
|
|
|
|
err = e
|
|
|
|
} else {
|
2013-08-30 18:01:10 +08:00
|
|
|
err = ErrUnknown
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2013-12-21 11:09:13 +08:00
|
|
|
f, ok := worker.funcs[inpack.fn]
|
2013-08-30 12:36:57 +08:00
|
|
|
if !ok {
|
2013-12-21 11:09:13 +08:00
|
|
|
return fmt.Errorf("The function does not exist: %s", inpack.fn)
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
|
|
|
var r *result
|
|
|
|
if f.timeout == 0 {
|
2013-12-21 11:09:13 +08:00
|
|
|
d, e := f.f(inpack)
|
2013-08-30 12:36:57 +08:00
|
|
|
r = &result{data: d, err: e}
|
|
|
|
} else {
|
2013-12-21 11:09:13 +08:00
|
|
|
r = execTimeout(f.f, inpack, time.Duration(f.timeout)*time.Second)
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
2013-12-24 14:35:33 +08:00
|
|
|
if worker.running {
|
|
|
|
outpack := getOutPack()
|
|
|
|
if r.err == nil {
|
|
|
|
outpack.dataType = WORK_COMPLETE
|
2013-08-30 12:36:57 +08:00
|
|
|
} else {
|
2013-12-24 14:35:33 +08:00
|
|
|
if len(r.data) == 0 {
|
|
|
|
outpack.dataType = WORK_FAIL
|
|
|
|
} else {
|
|
|
|
outpack.dataType = WORK_EXCEPTION
|
|
|
|
}
|
|
|
|
err = r.err
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
2013-12-24 14:35:33 +08:00
|
|
|
outpack.handle = inpack.handle
|
|
|
|
outpack.data = r.data
|
2013-12-21 11:09:13 +08:00
|
|
|
inpack.a.write(outpack)
|
2013-12-23 17:01:01 +08:00
|
|
|
inpack.a.Grab()
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
|
|
|
return
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
2012-05-24 16:49:35 +08:00
|
|
|
|
2012-06-05 14:36:39 +08:00
|
|
|
type result struct {
|
2013-08-30 12:36:57 +08:00
|
|
|
data []byte
|
|
|
|
err error
|
2012-06-05 14:36:39 +08:00
|
|
|
}
|
2012-09-02 22:42:54 +08:00
|
|
|
|
2013-08-30 18:01:10 +08:00
|
|
|
func execTimeout(f JobFunc, job Job, timeout time.Duration) (r *result) {
|
2013-08-30 12:36:57 +08:00
|
|
|
rslt := make(chan *result)
|
|
|
|
defer close(rslt)
|
|
|
|
go func() {
|
|
|
|
defer func() { recover() }()
|
|
|
|
d, e := f(job)
|
|
|
|
rslt <- &result{data: d, err: e}
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case r = <-rslt:
|
|
|
|
case <-time.After(timeout):
|
2013-08-30 18:01:10 +08:00
|
|
|
return &result{err: ErrTimeOut}
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
|
|
|
return r
|
2012-09-02 22:42:54 +08:00
|
|
|
}
|