You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

143 lines
3.6 KiB

  1. // Copyright 2011 Xing Xing <mikespook@gmail.com>.
  2. // All rights reserved.
  3. // Use of this source code is governed by a MIT
  4. // license that can be found in the LICENSE file.
  5. package client
  6. import (
  7. "bytes"
  8. "github.com/mikespook/gearman-go/common"
  9. )
  10. const (
  11. // Job type
  12. // JOB_NORMAL | JOB_BG means a normal level job run in background
  13. // normal level
  14. JOB_NORMAL = 0
  15. // background job
  16. JOB_BG = 1
  17. // low level
  18. JOB_LOW = 2
  19. // high level
  20. JOB_HIGH = 4
  21. )
  22. // An error handler
  23. type ErrorHandler func(error)
  24. // Client side job
  25. type Job struct {
  26. Data []byte
  27. Handle, UniqueId string
  28. magicCode, DataType uint32
  29. }
  30. // Create a new job
  31. func newJob(magiccode, datatype uint32, data []byte) (job *Job) {
  32. return &Job{magicCode: magiccode,
  33. DataType: datatype,
  34. Data: data}
  35. }
  36. // Decode a job from byte slice
  37. func decodeJob(data []byte) (job *Job, err error) {
  38. if len(data) < 12 {
  39. return nil, common.Errorf("Invalid data: %V", data)
  40. }
  41. datatype := common.BytesToUint32([4]byte{data[4], data[5], data[6], data[7]})
  42. l := common.BytesToUint32([4]byte{data[8], data[9], data[10], data[11]})
  43. if len(data[12:]) != int(l) {
  44. return nil, common.Errorf("Invalid data: %V", data)
  45. }
  46. data = data[12:]
  47. var handle string
  48. switch datatype {
  49. case common.WORK_DATA, common.WORK_WARNING, common.WORK_STATUS,
  50. common.WORK_COMPLETE, common.WORK_FAIL, common.WORK_EXCEPTION:
  51. i := bytes.IndexByte(data, '\x00')
  52. if i != -1 {
  53. handle = string(data[:i])
  54. data = data[i + 1:]
  55. }
  56. }
  57. return &Job{magicCode: common.RES,
  58. DataType: datatype,
  59. Data: data,
  60. Handle: handle}, nil
  61. }
  62. // Encode a job to byte slice
  63. func (job *Job) Encode() (data []byte) {
  64. l := len(job.Data)
  65. tl := l + 12
  66. data = make([]byte, tl)
  67. magiccode := common.Uint32ToBytes(job.magicCode)
  68. datatype := common.Uint32ToBytes(job.DataType)
  69. datalength := common.Uint32ToBytes(uint32(l))
  70. for i := 0; i < tl; i ++ {
  71. switch {
  72. case i < 4:
  73. data[i] = magiccode[i]
  74. case i < 8:
  75. data[i] = datatype[i - 4]
  76. case i < 12:
  77. data[i] = datalength[i - 8]
  78. default:
  79. data[i] = job.Data[i - 12]
  80. }
  81. }
  82. // Alternative
  83. /*
  84. data = append(data, magiccode[:] ...)
  85. data = append(data, datatype[:] ...)
  86. data = append(data, datalength[:] ...)
  87. data = append(data, job.Data ...)
  88. */
  89. return
  90. }
  91. // Extract the job's result.
  92. func (job *Job) Result() (data []byte, err error) {
  93. switch job.DataType {
  94. case common.WORK_FAIL:
  95. job.Handle = string(job.Data)
  96. return nil, common.ErrWorkFail
  97. case common.WORK_EXCEPTION:
  98. err = common.ErrWorkException
  99. fallthrough
  100. case common.WORK_COMPLETE:
  101. s := bytes.SplitN(job.Data, []byte{'\x00'}, 2)
  102. if len(s) != 2 {
  103. return nil, common.Errorf("Invalid data: %V", job.Data)
  104. }
  105. job.Handle = string(s[0])
  106. data = s[1]
  107. default:
  108. err = common.ErrDataType
  109. }
  110. return
  111. }
  112. // Extract the job's update
  113. func (job *Job) Update() (data []byte, err error) {
  114. if job.DataType != common.WORK_DATA && job.DataType != common.WORK_WARNING {
  115. err = common.ErrDataType
  116. return
  117. }
  118. s := bytes.SplitN(job.Data, []byte{'\x00'}, 2)
  119. if len(s) != 2 {
  120. err = common.ErrInvalidData
  121. return
  122. }
  123. if job.DataType == common.WORK_WARNING {
  124. err = common.ErrWorkWarning
  125. }
  126. job.Handle = string(s[0])
  127. data = s[1]
  128. return
  129. }