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.
 
 
 

172 lines
4.3 KiB

  1. // Copyright 2011 Xing Xing <mikespook@gmail.com> All rights reserved.
  2. // Use of this source code is governed by a MIT
  3. // license that can be found in the LICENSE file.
  4. package worker
  5. import (
  6. "io"
  7. "net"
  8. "bitbucket.org/mikespook/gearman-go/common"
  9. )
  10. // The agent of job server.
  11. type agent struct {
  12. conn net.Conn
  13. worker *Worker
  14. in chan []byte
  15. out chan *Job
  16. addr string
  17. }
  18. // Create the agent of job server.
  19. func newAgent(addr string, worker *Worker) (a *agent, err error) {
  20. conn, err := net.Dial(common.NETWORK, addr)
  21. if err != nil {
  22. return
  23. }
  24. a = &agent{
  25. conn: conn,
  26. worker: worker,
  27. addr: addr,
  28. in: make(chan []byte, common.QUEUE_SIZE),
  29. out: make(chan *Job, common.QUEUE_SIZE),
  30. }
  31. // reset abilities
  32. a.WriteJob(newJob(common.REQ, common.RESET_ABILITIES, nil))
  33. return
  34. }
  35. // outputing loop
  36. func (a *agent) outLoop() {
  37. ok := true
  38. var job *Job
  39. for ok {
  40. if job, ok = <-a.out; ok {
  41. if err := a.write(job.Encode()); err != nil {
  42. a.worker.err(err)
  43. }
  44. }
  45. }
  46. }
  47. // inputing loop
  48. func (a *agent) inLoop() {
  49. defer func() {
  50. if r := recover(); r != nil {
  51. a.worker.err(common.Errorf("Exiting: %s", r))
  52. }
  53. close(a.in)
  54. close(a.out)
  55. a.worker.removeAgent(a)
  56. }()
  57. for a.worker.running {
  58. a.WriteJob(newJob(common.REQ, common.PRE_SLEEP, nil))
  59. RESTART:
  60. // got noop msg and in queue is zero, grab job
  61. rel, err := a.read()
  62. if err != nil {
  63. if err == common.ErrConnection {
  64. for i:= 0; i < 3 && a.worker.running; i++ {
  65. if conn, err := net.Dial(common.NETWORK, a.addr); err != nil {
  66. a.worker.err(common.Errorf("Reconnection: %d faild", i))
  67. continue
  68. } else {
  69. a.conn = conn
  70. goto RESTART
  71. }
  72. }
  73. a.worker.err(err)
  74. break
  75. }
  76. a.worker.err(err)
  77. continue
  78. }
  79. job, err := decodeJob(rel)
  80. if err != nil {
  81. a.worker.err(err)
  82. continue
  83. }
  84. switch job.DataType {
  85. case common.NOOP:
  86. a.WriteJob(newJob(common.REQ, common.GRAB_JOB_UNIQ, nil))
  87. case common.ERROR, common.ECHO_RES, common.JOB_ASSIGN_UNIQ, common.JOB_ASSIGN:
  88. job.agent = a
  89. a.worker.in <- job
  90. }
  91. }
  92. }
  93. func (a *agent) Close() {
  94. a.conn.Close()
  95. }
  96. func (a *agent) Work() {
  97. go a.outLoop()
  98. go a.inLoop()
  99. }
  100. // Internal read
  101. func (a *agent) read() (data []byte, err error) {
  102. if len(a.in) > 0 {
  103. // in queue is not empty
  104. data = <-a.in
  105. } else {
  106. for {
  107. buf := make([]byte, common.BUFFER_SIZE)
  108. var n int
  109. if n, err = a.conn.Read(buf); err != nil {
  110. if err == io.EOF && n == 0 {
  111. if data == nil {
  112. err = common.ErrConnection
  113. return
  114. }
  115. break
  116. }
  117. return
  118. }
  119. data = append(data, buf[0:n]...)
  120. if n < common.BUFFER_SIZE {
  121. break
  122. }
  123. }
  124. }
  125. // split package
  126. tl := len(data)
  127. start := 0
  128. for i := 0; i < tl; i++ {
  129. if string(data[start:start+4]) == common.RES_STR {
  130. l := int(common.BytesToUint32([4]byte{data[start+8],
  131. data[start+9], data[start+10], data[start+11]}))
  132. total := l + 12
  133. if total == tl {
  134. return
  135. } else {
  136. a.in <- data[total:]
  137. data = data[:total]
  138. return
  139. }
  140. } else {
  141. start++
  142. }
  143. }
  144. return nil, common.Errorf("Invalid data: %V", data)
  145. }
  146. // Send a job to the job server.
  147. func (a *agent) WriteJob(job *Job) {
  148. a.out <- job
  149. }
  150. // Internal write the encoded job.
  151. func (a *agent) write(buf []byte) (err error) {
  152. var n int
  153. for i := 0; i < len(buf); i += n {
  154. n, err = a.conn.Write(buf[i:])
  155. if err != nil {
  156. return err
  157. }
  158. }
  159. return
  160. }