2012-05-24 16:49:35 +08:00
|
|
|
// Copyright 2011 Xing Xing <mikespook@gmail.com>
|
|
|
|
// All rights reserved.
|
2012-03-26 13:32:59 +08:00
|
|
|
// Use of this source code is governed by a MIT
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
2012-12-21 11:11:37 +08:00
|
|
|
"github.com/mikespook/gearman-go/common"
|
2012-03-26 13:32:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Worker side job
|
2012-05-23 17:45:52 +08:00
|
|
|
type Job struct {
|
2012-03-26 13:32:59 +08:00
|
|
|
Data []byte
|
|
|
|
Handle, UniqueId string
|
2012-05-24 16:49:35 +08:00
|
|
|
agent *agent
|
2012-03-26 13:32:59 +08:00
|
|
|
magicCode, DataType uint32
|
2012-06-05 14:36:39 +08:00
|
|
|
c chan bool
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new job
|
2012-05-23 17:45:52 +08:00
|
|
|
func newJob(magiccode, datatype uint32, data []byte) (job *Job) {
|
|
|
|
return &Job{magicCode: magiccode,
|
2012-03-26 13:32:59 +08:00
|
|
|
DataType: datatype,
|
2012-06-05 14:36:39 +08:00
|
|
|
Data: data,
|
|
|
|
c: make(chan bool),
|
|
|
|
}
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decode job from byte slice
|
2012-05-23 17:45:52 +08:00
|
|
|
func decodeJob(data []byte) (job *Job, err error) {
|
2012-03-26 13:32:59 +08:00
|
|
|
if len(data) < 12 {
|
2012-05-24 16:49:35 +08:00
|
|
|
return nil, common.Errorf("Invalid data: %V", data)
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
2012-05-23 17:45:52 +08:00
|
|
|
datatype := common.BytesToUint32([4]byte{data[4], data[5], data[6], data[7]})
|
|
|
|
l := common.BytesToUint32([4]byte{data[8], data[9], data[10], data[11]})
|
2012-03-26 13:32:59 +08:00
|
|
|
if len(data[12:]) != int(l) {
|
2012-05-24 16:49:35 +08:00
|
|
|
return nil, common.Errorf("Invalid data: %V", data)
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
data = data[12:]
|
2012-05-23 17:45:52 +08:00
|
|
|
job = newJob(common.RES, datatype, data)
|
2012-03-26 13:32:59 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode a job to byte slice
|
2012-05-23 17:45:52 +08:00
|
|
|
func (job *Job) Encode() (data []byte) {
|
2012-09-25 14:39:50 +08:00
|
|
|
var l int
|
|
|
|
if job.DataType == common.WORK_FAIL {
|
|
|
|
l = len(job.Handle)
|
|
|
|
} else {
|
|
|
|
l = len(job.Data)
|
|
|
|
if job.Handle != "" {
|
|
|
|
l += len(job.Handle) + 1
|
|
|
|
}
|
2012-05-23 17:45:52 +08:00
|
|
|
}
|
2012-06-12 12:22:20 +08:00
|
|
|
data = make([]byte, 0, l + 12)
|
2012-05-23 17:45:52 +08:00
|
|
|
|
|
|
|
magiccode := common.Uint32ToBytes(job.magicCode)
|
|
|
|
datatype := common.Uint32ToBytes(job.DataType)
|
2012-06-12 12:22:20 +08:00
|
|
|
datalength := common.Uint32ToBytes(uint32(l))
|
2012-05-23 17:45:52 +08:00
|
|
|
|
2012-03-26 13:32:59 +08:00
|
|
|
data = append(data, magiccode[:]...)
|
|
|
|
data = append(data, datatype[:]...)
|
2012-05-23 17:45:52 +08:00
|
|
|
data = append(data, datalength[:]...)
|
2012-03-26 13:32:59 +08:00
|
|
|
if job.Handle != "" {
|
|
|
|
data = append(data, []byte(job.Handle)...)
|
2012-09-25 14:39:50 +08:00
|
|
|
if job.DataType != common.WORK_FAIL {
|
|
|
|
data = append(data, 0)
|
|
|
|
}
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
data = append(data, job.Data...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send some datas to client.
|
|
|
|
// Using this in a job's executing.
|
2012-05-29 08:45:37 +08:00
|
|
|
func (job *Job) UpdateData(data []byte, iswarning bool) {
|
2012-03-26 13:32:59 +08:00
|
|
|
result := append([]byte(job.Handle), 0)
|
|
|
|
result = append(result, data...)
|
|
|
|
var datatype uint32
|
2012-05-29 08:45:37 +08:00
|
|
|
if iswarning {
|
2012-05-23 17:45:52 +08:00
|
|
|
datatype = common.WORK_WARNING
|
2012-03-26 13:32:59 +08:00
|
|
|
} else {
|
2012-05-23 17:45:52 +08:00
|
|
|
datatype = common.WORK_DATA
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
2012-05-24 16:49:35 +08:00
|
|
|
job.agent.WriteJob(newJob(common.REQ, datatype, result))
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update status.
|
|
|
|
// Tall client how many percent job has been executed.
|
2012-05-24 16:49:35 +08:00
|
|
|
func (job *Job) UpdateStatus(numerator, denominator int) {
|
2012-03-26 13:32:59 +08:00
|
|
|
n := []byte(strconv.Itoa(numerator))
|
|
|
|
d := []byte(strconv.Itoa(denominator))
|
|
|
|
result := append([]byte(job.Handle), 0)
|
|
|
|
result = append(result, n...)
|
|
|
|
result = append(result, d...)
|
2012-05-24 16:49:35 +08:00
|
|
|
job.agent.WriteJob(newJob(common.REQ, common.WORK_STATUS, result))
|
2012-03-26 13:32:59 +08:00
|
|
|
}
|
2012-06-05 14:36:39 +08:00
|
|
|
|
2012-09-02 22:42:54 +08:00
|
|
|
// close the job
|
|
|
|
func (job *Job) Close() {
|
|
|
|
close(job.c)
|
|
|
|
}
|
|
|
|
|
2012-06-05 14:36:39 +08:00
|
|
|
// cancel the job executing
|
|
|
|
func (job *Job) cancel() {
|
2012-09-02 22:42:54 +08:00
|
|
|
defer func() {recover()}()
|
2012-06-05 14:36:39 +08:00
|
|
|
job.c <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
// When a job was canceled, return a true form a channel
|
2012-09-02 22:42:54 +08:00
|
|
|
func (job *Job) Canceled() <-chan bool {
|
2012-06-05 14:36:39 +08:00
|
|
|
return job.c
|
|
|
|
}
|