2013-12-25 18:11:01 +08:00
|
|
|
// The worker package helps developers to develop Gearman's worker
|
|
|
|
// in an easy way.
|
2012-03-26 13:32:59 +08:00
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
2013-08-30 18:01:10 +08:00
|
|
|
"fmt"
|
2019-05-28 18:41:20 +08:00
|
|
|
"strconv"
|
2013-08-30 18:01:10 +08:00
|
|
|
"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-12-26 12:06:47 +08:00
|
|
|
Unlimited = iota
|
|
|
|
OneByOne
|
2012-05-10 21:25:33 +08:00
|
|
|
)
|
2012-06-01 14:28:10 +08:00
|
|
|
|
2013-12-25 18:11:01 +08:00
|
|
|
// Worker is the only structure needed by worker side developing.
|
|
|
|
// It can connect to multi-server and grab jobs.
|
2012-03-26 13:32:59 +08:00
|
|
|
type Worker struct {
|
2013-12-25 15:13:21 +08:00
|
|
|
sync.Mutex
|
2023-02-13 00:29:22 +08:00
|
|
|
agents []*agent
|
|
|
|
funcs jobFuncs
|
|
|
|
in chan *inPack
|
|
|
|
running bool
|
|
|
|
ready bool
|
|
|
|
jobLeftNum int64
|
2013-08-30 12:36:57 +08:00
|
|
|
|
2013-12-25 18:11:01 +08:00
|
|
|
Id string
|
2013-08-30 18:01:10 +08:00
|
|
|
ErrorHandler ErrorHandler
|
2013-12-23 17:05:42 +08:00
|
|
|
JobHandler JobHandler
|
2013-12-25 15:13:21 +08:00
|
|
|
limit chan bool
|
2023-02-13 00:29:22 +08:00
|
|
|
closed chan struct{}
|
|
|
|
leftJobs chan struct{}
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
2019-02-28 10:11:17 +08:00
|
|
|
// New returns a worker.
|
2013-12-25 18:11:01 +08:00
|
|
|
//
|
|
|
|
// If limit is set to Unlimited(=0), the worker will grab all jobs
|
|
|
|
// and execute them parallelly.
|
|
|
|
// If limit is greater than zero, the number of paralled executing
|
|
|
|
// jobs are limited under the number. If limit is assgined to
|
|
|
|
// OneByOne(=1), there will be only one job executed in a time.
|
2013-12-25 15:13:21 +08:00
|
|
|
func New(limit int) (worker *Worker) {
|
2013-08-30 12:36:57 +08:00
|
|
|
worker = &Worker{
|
2013-12-25 17:01:42 +08:00
|
|
|
agents: make([]*agent, 0, limit),
|
2013-12-26 12:32:27 +08:00
|
|
|
funcs: make(jobFuncs),
|
|
|
|
in: make(chan *inPack, queueSize),
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
2013-12-25 15:13:21 +08:00
|
|
|
if limit != Unlimited {
|
2013-12-25 17:01:42 +08:00
|
|
|
worker.limit = make(chan bool, limit-1)
|
2013-12-25 15:13:21 +08:00
|
|
|
}
|
2013-08-30 12:36:57 +08:00
|
|
|
return
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
2013-12-25 18:11:01 +08:00
|
|
|
// inner error handling
|
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
|
|
|
}
|
|
|
|
|
2019-02-28 10:11:17 +08:00
|
|
|
// AddServer adds a Gearman job server.
|
2013-12-25 18:11:01 +08:00
|
|
|
//
|
|
|
|
// addr should be formated as 'host:port'.
|
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
|
|
|
}
|
|
|
|
|
2013-12-25 18:11:01 +08:00
|
|
|
// Broadcast an outpack to all Gearman server.
|
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 {
|
2019-05-28 18:41:20 +08:00
|
|
|
v.Write(outpack)
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
2012-05-24 16:49:35 +08:00
|
|
|
}
|
|
|
|
|
2019-02-28 10:11:17 +08:00
|
|
|
// AddFunc adds a function.
|
2013-12-25 18:11:01 +08:00
|
|
|
// Set timeout as Unlimited(=0) to disable executing timeout.
|
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-12-25 15:13:21 +08:00
|
|
|
worker.Lock()
|
|
|
|
defer worker.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
|
|
|
|
2013-12-25 18:11:01 +08:00
|
|
|
// inner add
|
2012-05-24 16:49:35 +08:00
|
|
|
func (worker *Worker) addFunc(funcname string, timeout uint32) {
|
2015-01-06 11:34:39 +08:00
|
|
|
outpack := prepFuncOutpack(funcname, timeout)
|
2014-06-10 10:23:18 +08:00
|
|
|
worker.broadcast(outpack)
|
|
|
|
}
|
|
|
|
|
2015-01-06 11:34:39 +08:00
|
|
|
func prepFuncOutpack(funcname string, timeout uint32) *outPack {
|
2013-12-21 11:09:13 +08:00
|
|
|
outpack := getOutPack()
|
2013-08-30 12:36:57 +08:00
|
|
|
if timeout == 0 {
|
2013-12-26 12:32:27 +08:00
|
|
|
outpack.dataType = dtCanDo
|
2013-12-21 11:09:13 +08:00
|
|
|
outpack.data = []byte(funcname)
|
2013-08-30 12:36:57 +08:00
|
|
|
} else {
|
2013-12-26 12:32:27 +08:00
|
|
|
outpack.dataType = dtCanDoTimeout
|
2013-08-30 18:01:10 +08:00
|
|
|
l := len(funcname)
|
2019-02-13 18:45:35 +08:00
|
|
|
|
|
|
|
timeoutString := strconv.FormatUint(uint64(timeout), 10)
|
|
|
|
outpack.data = getBuffer(l + len(timeoutString) + 1)
|
2013-12-21 11:09:13 +08:00
|
|
|
copy(outpack.data, []byte(funcname))
|
|
|
|
outpack.data[l] = '\x00'
|
2019-02-13 18:45:35 +08:00
|
|
|
copy(outpack.data[l+1:], []byte(timeoutString))
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
2014-06-10 10:23:18 +08:00
|
|
|
return outpack
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
2019-02-28 10:11:17 +08:00
|
|
|
// RemoveFunc removes a function.
|
2012-05-24 16:49:35 +08:00
|
|
|
func (worker *Worker) RemoveFunc(funcname string) (err error) {
|
2013-12-25 15:13:21 +08:00
|
|
|
worker.Lock()
|
|
|
|
defer worker.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
|
|
|
}
|
|
|
|
|
2013-12-25 18:11:01 +08:00
|
|
|
// inner remove
|
2012-05-24 16:49:35 +08:00
|
|
|
func (worker *Worker) removeFunc(funcname string) {
|
2013-12-21 11:09:13 +08:00
|
|
|
outpack := getOutPack()
|
2013-12-26 12:32:27 +08:00
|
|
|
outpack.dataType = dtCantDo
|
2013-12-21 11:09:13 +08:00
|
|
|
outpack.data = []byte(funcname)
|
|
|
|
worker.broadcast(outpack)
|
2012-05-24 16:49:35 +08:00
|
|
|
}
|
|
|
|
|
2013-12-25 18:11:01 +08:00
|
|
|
// inner package handling
|
2013-12-21 11:09:13 +08:00
|
|
|
func (worker *Worker) handleInPack(inpack *inPack) {
|
|
|
|
switch inpack.dataType {
|
2013-12-26 12:32:27 +08:00
|
|
|
case dtNoJob:
|
2013-12-23 17:01:01 +08:00
|
|
|
inpack.a.PreSleep()
|
2013-12-26 12:32:27 +08:00
|
|
|
case dtNoop:
|
2013-12-23 17:01:01 +08:00
|
|
|
inpack.a.Grab()
|
2013-12-26 12:32:27 +08:00
|
|
|
case dtJobAssign, dtJobAssignUniq:
|
2013-12-25 15:13:21 +08:00
|
|
|
go func() {
|
2023-02-13 00:29:22 +08:00
|
|
|
go func() {
|
|
|
|
worker.incrExecJobNum()
|
|
|
|
defer func() {
|
|
|
|
worker.decrExecJobNum()
|
|
|
|
}()
|
|
|
|
if err := worker.exec(inpack); err != nil {
|
|
|
|
worker.err(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if worker.limit != nil {
|
|
|
|
worker.limit <- true
|
2013-12-25 15:13:21 +08:00
|
|
|
}
|
2023-02-13 00:29:22 +08:00
|
|
|
inpack.a.Grab()
|
2013-12-25 15:13:21 +08:00
|
|
|
}()
|
2013-12-26 12:32:27 +08:00
|
|
|
case dtError:
|
2013-12-26 12:06:47 +08:00
|
|
|
worker.err(inpack.Err())
|
|
|
|
fallthrough
|
2013-12-26 12:32:27 +08:00
|
|
|
case dtEchoRes:
|
2013-12-25 15:13:21 +08:00
|
|
|
fallthrough
|
2013-08-30 12:36:57 +08:00
|
|
|
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-25 18:11:01 +08:00
|
|
|
// Connect to Gearman server and tell every server
|
|
|
|
// what can this worker do.
|
2013-12-23 17:01:01 +08:00
|
|
|
func (worker *Worker) Ready() (err error) {
|
2013-12-26 12:06:47 +08:00
|
|
|
if len(worker.agents) == 0 {
|
|
|
|
return ErrNoneAgents
|
|
|
|
}
|
|
|
|
if len(worker.funcs) == 0 {
|
|
|
|
return ErrNoneFuncs
|
|
|
|
}
|
2013-12-26 15:55:16 +08:00
|
|
|
for _, a := range worker.agents {
|
|
|
|
if err = a.Connect(); err != nil {
|
2013-12-23 17:01:01 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for funcname, f := range worker.funcs {
|
|
|
|
worker.addFunc(funcname, f.timeout)
|
|
|
|
}
|
2014-05-29 22:35:27 +08:00
|
|
|
worker.ready = true
|
2013-12-23 17:01:01 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:41:20 +08:00
|
|
|
// Work start main loop (blocking)
|
2013-12-25 18:11:01 +08:00
|
|
|
// Most of time, this should be evaluated in goroutine.
|
2012-03-26 13:32:59 +08:00
|
|
|
func (worker *Worker) Work() {
|
2015-01-06 11:34:39 +08:00
|
|
|
if !worker.ready {
|
2014-06-01 23:59:57 +08:00
|
|
|
// didn't run Ready beforehand, so we'll have to do it:
|
|
|
|
err := worker.Ready()
|
|
|
|
if err != nil {
|
2015-01-06 11:34:39 +08:00
|
|
|
panic(err)
|
2014-06-01 23:59:57 +08:00
|
|
|
}
|
2014-05-29 22:35:27 +08:00
|
|
|
}
|
|
|
|
|
2019-05-28 18:41:20 +08:00
|
|
|
worker.Lock()
|
2013-08-30 12:36:57 +08:00
|
|
|
worker.running = true
|
2019-05-28 18:41:20 +08:00
|
|
|
worker.Unlock()
|
|
|
|
|
2013-12-25 18:11:01 +08:00
|
|
|
for _, a := range worker.agents {
|
|
|
|
a.Grab()
|
2013-12-24 14:35:33 +08:00
|
|
|
}
|
2023-02-13 00:29:22 +08:00
|
|
|
// 执行任务(阻塞)
|
2013-12-21 11:09:13 +08:00
|
|
|
var inpack *inPack
|
|
|
|
for inpack = range worker.in {
|
2013-12-25 15:13:21 +08:00
|
|
|
worker.handleInPack(inpack)
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
2023-02-13 00:29:22 +08:00
|
|
|
// 关闭Worker进程后 等待任务完成后退出
|
|
|
|
worker.Lock()
|
|
|
|
leftJobNum := int(worker.jobLeftNum)
|
|
|
|
worker.Unlock()
|
|
|
|
if worker.leftJobs != nil {
|
|
|
|
for i := 0; i < leftJobNum; i++ {
|
|
|
|
<-worker.leftJobs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
worker.Reset()
|
|
|
|
worker.close()
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
2013-12-25 18:11:01 +08:00
|
|
|
// custome handling warper
|
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
|
|
|
}
|
|
|
|
|
2013-12-25 18:11:01 +08:00
|
|
|
// Close connection and exit main loop
|
2012-05-24 16:49:35 +08:00
|
|
|
func (worker *Worker) Close() {
|
2013-12-26 12:06:47 +08:00
|
|
|
worker.Lock()
|
2015-01-06 11:34:39 +08:00
|
|
|
defer worker.Unlock()
|
2023-02-13 00:29:22 +08:00
|
|
|
if worker.running == true && worker.closed == nil {
|
|
|
|
worker.closed = make(chan struct{}, 1)
|
|
|
|
worker.closed <- struct{}{}
|
2013-12-26 12:06:47 +08:00
|
|
|
worker.running = false
|
|
|
|
close(worker.in)
|
2023-02-13 00:29:22 +08:00
|
|
|
// 创建关闭后执行中的任务列表
|
|
|
|
if worker.jobLeftNum != 0 {
|
|
|
|
worker.leftJobs = make(chan struct{}, worker.jobLeftNum+int64(len(worker.in)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (worker *Worker) close() {
|
|
|
|
for _, a := range worker.agents {
|
|
|
|
a.Close()
|
2013-12-26 12:06:47 +08:00
|
|
|
}
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
2013-12-25 18:11:01 +08:00
|
|
|
// Echo
|
2012-05-24 16:49:35 +08:00
|
|
|
func (worker *Worker) Echo(data []byte) {
|
2013-12-21 11:09:13 +08:00
|
|
|
outpack := getOutPack()
|
2013-12-26 12:32:27 +08:00
|
|
|
outpack.dataType = dtEchoReq
|
2013-12-21 11:09:13 +08:00
|
|
|
outpack.data = data
|
|
|
|
worker.broadcast(outpack)
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
2019-02-28 10:11:17 +08:00
|
|
|
// Reset removes all of functions.
|
2013-12-25 18:11:01 +08:00
|
|
|
// Both from the worker and job servers.
|
2012-05-24 16:49:35 +08:00
|
|
|
func (worker *Worker) Reset() {
|
2013-12-21 11:09:13 +08:00
|
|
|
outpack := getOutPack()
|
2013-12-26 12:32:27 +08:00
|
|
|
outpack.dataType = dtResetAbilities
|
2013-12-21 11:09:13 +08:00
|
|
|
worker.broadcast(outpack)
|
2013-12-26 12:32:27 +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()
|
2013-12-26 12:32:27 +08:00
|
|
|
outpack.dataType = dtSetClientId
|
2013-12-21 11:09:13 +08:00
|
|
|
outpack.data = []byte(id)
|
|
|
|
worker.broadcast(outpack)
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
2023-02-13 00:29:22 +08:00
|
|
|
func (worker *Worker) incrExecJobNum() int64 {
|
|
|
|
worker.Lock()
|
|
|
|
defer worker.Unlock()
|
|
|
|
worker.jobLeftNum++
|
|
|
|
return worker.jobLeftNum
|
|
|
|
}
|
|
|
|
|
|
|
|
func (worker *Worker) decrExecJobNum() int64 {
|
|
|
|
worker.Lock()
|
|
|
|
defer worker.Unlock()
|
|
|
|
worker.jobLeftNum--
|
|
|
|
if worker.jobLeftNum < 0 {
|
|
|
|
worker.jobLeftNum = 0
|
|
|
|
}
|
|
|
|
return worker.jobLeftNum
|
|
|
|
}
|
|
|
|
|
2013-12-25 18:11:01 +08:00
|
|
|
// inner job executing
|
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() {
|
2013-12-25 17:01:42 +08:00
|
|
|
if worker.limit != nil {
|
2013-12-25 15:13:21 +08:00
|
|
|
<-worker.limit
|
|
|
|
}
|
2013-08-30 12:36:57 +08:00
|
|
|
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
|
|
|
}
|
2023-02-13 00:29:22 +08:00
|
|
|
//if worker.running {
|
|
|
|
outpack := getOutPack()
|
|
|
|
if r.err == nil {
|
|
|
|
outpack.dataType = dtWorkComplete
|
|
|
|
} else {
|
|
|
|
if len(r.data) == 0 {
|
|
|
|
outpack.dataType = dtWorkFail
|
2013-08-30 12:36:57 +08:00
|
|
|
} else {
|
2023-02-13 00:29:22 +08:00
|
|
|
outpack.dataType = dtWorkException
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
2023-02-13 00:29:22 +08:00
|
|
|
err = r.err
|
|
|
|
}
|
|
|
|
outpack.handle = inpack.handle
|
|
|
|
outpack.data = r.data
|
2023-02-13 01:01:37 +08:00
|
|
|
_ = inpack.a.Write(outpack)
|
2023-02-13 00:29:22 +08:00
|
|
|
if worker.leftJobs != nil {
|
|
|
|
worker.leftJobs <- struct{}{}
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
2023-02-13 00:29:22 +08:00
|
|
|
//}
|
2013-08-30 12:36:57 +08:00
|
|
|
return
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
2015-01-06 11:34:39 +08:00
|
|
|
func (worker *Worker) reRegisterFuncsForAgent(a *agent) {
|
2014-06-10 10:23:18 +08:00
|
|
|
worker.Lock()
|
|
|
|
defer worker.Unlock()
|
|
|
|
for funcname, f := range worker.funcs {
|
2015-01-06 11:34:39 +08:00
|
|
|
outpack := prepFuncOutpack(funcname, f.timeout)
|
2014-06-10 10:23:18 +08:00
|
|
|
a.write(outpack)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-05-24 16:49:35 +08:00
|
|
|
|
2013-12-25 18:11:01 +08:00
|
|
|
// inner result
|
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-12-25 18:11:01 +08:00
|
|
|
// executing timer
|
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
|
|
|
}
|
2014-06-10 10:23:18 +08:00
|
|
|
|
|
|
|
// Error type passed when a worker connection disconnects
|
2015-01-06 11:34:39 +08:00
|
|
|
type WorkerDisconnectError struct {
|
|
|
|
err error
|
|
|
|
agent *agent
|
2014-06-10 10:23:18 +08:00
|
|
|
}
|
2015-01-06 11:34:39 +08:00
|
|
|
|
|
|
|
func (e *WorkerDisconnectError) Error() string {
|
|
|
|
return e.err.Error()
|
2014-06-10 10:46:21 +08:00
|
|
|
}
|
|
|
|
|
2014-06-10 10:23:18 +08:00
|
|
|
// Responds to the error by asking the worker to reconnect
|
2015-01-06 11:34:39 +08:00
|
|
|
func (e *WorkerDisconnectError) Reconnect() (err error) {
|
2014-06-10 10:23:18 +08:00
|
|
|
return e.agent.reconnect()
|
|
|
|
}
|
2015-01-06 11:34:39 +08:00
|
|
|
|
|
|
|
// Which server was this for?
|
|
|
|
func (e *WorkerDisconnectError) Server() (net string, addr string) {
|
2014-06-10 10:23:18 +08:00
|
|
|
return e.agent.net, e.agent.addr
|
|
|
|
}
|