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.
 
 
 

129 lines
3.2 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. "bitbucket.org/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. return newJob(common.RES, datatype, data), nil
  48. }
  49. // Encode a job to byte slice
  50. func (job *Job) Encode() (data []byte) {
  51. l := len(job.Data)
  52. tl := l + 12
  53. data = make([]byte, tl)
  54. magiccode := common.Uint32ToBytes(job.magicCode)
  55. datatype := common.Uint32ToBytes(job.DataType)
  56. datalength := common.Uint32ToBytes(uint32(l))
  57. for i := 0; i < tl; i ++ {
  58. switch {
  59. case i < 4:
  60. data[i] = magiccode[i]
  61. case i < 8:
  62. data[i] = datatype[i - 4]
  63. case i < 12:
  64. data[i] = datalength[i - 8]
  65. default:
  66. data[i] = job.Data[i - 12]
  67. }
  68. }
  69. // Alternative
  70. /*
  71. data = append(data, magiccode[:] ...)
  72. data = append(data, datatype[:] ...)
  73. data = append(data, datalength[:] ...)
  74. data = append(data, job.Data ...)
  75. */
  76. return
  77. }
  78. // Extract the job's result.
  79. func (job *Job) Result() (data []byte, err error) {
  80. switch job.DataType {
  81. case common.WORK_FAIL:
  82. job.Handle = string(job.Data)
  83. return nil, common.ErrWorkFail
  84. case common.WORK_EXCEPTION:
  85. err = common.ErrWorkException
  86. fallthrough
  87. case common.WORK_COMPLETE:
  88. s := bytes.SplitN(job.Data, []byte{'\x00'}, 2)
  89. if len(s) != 2 {
  90. return nil, common.Errorf("Invalid data: %V", job.Data)
  91. }
  92. job.Handle = string(s[0])
  93. data = s[1]
  94. default:
  95. err = common.ErrDataType
  96. }
  97. return
  98. }
  99. // Extract the job's update
  100. func (job *Job) Update() (data []byte, err error) {
  101. if job.DataType != common.WORK_DATA && job.DataType != common.WORK_WARNING {
  102. err = common.ErrDataType
  103. return
  104. }
  105. s := bytes.SplitN(job.Data, []byte{'\x00'}, 2)
  106. if len(s) != 2 {
  107. err = common.ErrInvalidData
  108. return
  109. }
  110. if job.DataType == common.WORK_WARNING {
  111. err = common.ErrWorkWarning
  112. }
  113. job.Handle = string(s[0])
  114. data = s[1]
  115. return
  116. }