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-05-23 17:45:52 +08:00
|
|
|
"bitbucket.org/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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
Data: data}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
l := len(job.Data)
|
2012-05-24 16:49:35 +08:00
|
|
|
tl := l
|
2012-05-23 17:45:52 +08:00
|
|
|
if job.Handle != "" {
|
|
|
|
tl += len(job.Handle) + 1
|
|
|
|
}
|
2012-05-24 16:49:35 +08:00
|
|
|
data = make([]byte, 0, tl + 12)
|
2012-05-23 17:45:52 +08:00
|
|
|
|
|
|
|
magiccode := common.Uint32ToBytes(job.magicCode)
|
|
|
|
datatype := common.Uint32ToBytes(job.DataType)
|
|
|
|
datalength := common.Uint32ToBytes(uint32(tl))
|
|
|
|
|
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)...)
|
|
|
|
data = append(data, 0)
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|