task_redis.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/pkg/errors"
  7. "go-common/app/job/main/aegis/model"
  8. "go-common/library/cache/redis"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _hashexpire = 24 * 60 * 60
  13. )
  14. func personalKey(businessID, flowID int64, uid int64) string {
  15. return fmt.Sprintf("personal_%d_%d_%d", businessID, flowID, uid)
  16. }
  17. func publicKey(businessID, flowID int64) string {
  18. return fmt.Sprintf("{%d-%d}public_%d_%d", businessID, flowID, businessID, flowID)
  19. }
  20. func publicBackKey(businessID, flowID int64) string {
  21. return fmt.Sprintf("{%d-%d}publicBackup_%d_%d", businessID, flowID, businessID, flowID)
  22. }
  23. func delayKey(businessID, flowID int64, uid int64) string {
  24. return fmt.Sprintf("delay_%d_%d_%d", businessID, flowID, uid)
  25. }
  26. func haskKey(taskid int64) string {
  27. return fmt.Sprintf("task_%d", taskid)
  28. }
  29. func zsetKey(taskid int64) string {
  30. return fmt.Sprintf("%.11d", taskid)
  31. }
  32. // SetTask .
  33. func (d *Dao) SetTask(c context.Context, task *model.Task) (err error) {
  34. conn := d.redis.Get(c)
  35. defer conn.Close()
  36. var bs []byte
  37. key := haskKey(task.ID)
  38. if bs, err = json.Marshal(task); err != nil {
  39. log.Error("json.Marshal(%+v) error(%v)", task, err)
  40. return
  41. }
  42. if err = conn.Send("SET", key, bs); err != nil {
  43. log.Error("HSET error(%v)", err)
  44. return
  45. }
  46. if err = conn.Send("EXPIRE", key, _hashexpire); err != nil {
  47. log.Error("EXPIRE error(%v)", err)
  48. return
  49. }
  50. if err = conn.Flush(); err != nil {
  51. log.Error("conn.Flush error(%v)", err)
  52. return
  53. }
  54. for i := 0; i < 2; i++ {
  55. if _, err = conn.Receive(); err != nil {
  56. log.Error("conn.Receive error(%v)", err)
  57. return
  58. }
  59. }
  60. return
  61. }
  62. // GetTask .
  63. func (d *Dao) GetTask(c context.Context, id int64) (task *model.Task, err error) {
  64. var bs []byte
  65. conn := d.redis.Get(c)
  66. defer conn.Close()
  67. key := haskKey(id)
  68. if bs, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  69. log.Error("conn.Get(%s) error(%v)", key, err)
  70. return
  71. }
  72. task = new(model.Task)
  73. if err = json.Unmarshal(bs, task); err != nil {
  74. log.Error("json.Unmarshal(%s) error(%v)", string(bs), err)
  75. return
  76. }
  77. return
  78. }
  79. // RemovePersonalTask 任务延迟或完成
  80. func (d *Dao) RemovePersonalTask(c context.Context, businessID, flowID int64, uid, taskid int64) (err error) {
  81. key := personalKey(businessID, flowID, uid)
  82. return d.removeList(c, key, taskid)
  83. }
  84. // RemoveDelayTask 延迟任务完成
  85. func (d *Dao) RemoveDelayTask(c context.Context, businessID, flowID int64, uid, taskid int64) (err error) {
  86. key := delayKey(businessID, flowID, uid)
  87. return d.removeList(c, key, taskid)
  88. }
  89. func (d *Dao) removeList(c context.Context, key string, id int64) (err error) {
  90. conn := d.redis.Get(c)
  91. defer conn.Close()
  92. if _, err = conn.Do("LREM", key, 0, id); err != nil {
  93. log.Error("LREM error(%v)", errors.WithStack(err))
  94. }
  95. return
  96. }
  97. // PushPersonalTask 放入本人任务池
  98. func (d *Dao) PushPersonalTask(c context.Context, businessID, flowID int64, uid, taskid int64) (err error) {
  99. key := personalKey(businessID, flowID, uid)
  100. return d.pushList(c, key, taskid)
  101. }
  102. // PushDelayTask 延迟任务队列
  103. func (d *Dao) PushDelayTask(c context.Context, businessID, flowID int64, uid, taskid int64) (err error) {
  104. key := delayKey(businessID, flowID, uid)
  105. return d.pushList(c, key, taskid)
  106. }
  107. func (d *Dao) pushList(c context.Context, key string, values ...interface{}) (err error) {
  108. var (
  109. conn = d.redis.Get(c)
  110. )
  111. defer conn.Close()
  112. args1 := []interface{}{key, 0}
  113. args1 = append(args1, values...)
  114. if _, err = conn.Do("LREM", args1...); err != nil {
  115. log.Error("conn.Do(RPUSH, %v) error(%v)", args1, err)
  116. return
  117. }
  118. args2 := []interface{}{key}
  119. args2 = append(args2, values...)
  120. if _, err = conn.Do("RPUSH", args2...); err != nil {
  121. log.Error("conn.Do(RPUSH, %v) error(%v)", args2, err)
  122. }
  123. return
  124. }
  125. // RemovePublicTask 移除
  126. func (d *Dao) RemovePublicTask(c context.Context, businessID, flowID int64, taskid int64) (err error) {
  127. conn := d.redis.Get(c)
  128. defer conn.Close()
  129. key := publicKey(businessID, flowID)
  130. args := []interface{}{key}
  131. args = append(args, zsetKey(taskid))
  132. if _, err = conn.Do("ZREM", args...); err != nil {
  133. log.Error("(ZREM,%v) error(%v)", args, errors.WithStack(err))
  134. }
  135. return err
  136. }
  137. // PushPublicTask 放入实时任务池
  138. func (d *Dao) PushPublicTask(c context.Context, tasks ...*model.Task) (err error) {
  139. conn := d.redis.Get(c)
  140. defer conn.Close()
  141. for _, task := range tasks {
  142. key := publicKey(task.BusinessID, task.FlowID)
  143. if _, err = conn.Do("ZADD", key, -task.Weight, zsetKey(task.ID)); err != nil {
  144. log.Error("conn.Do(ZADD,%s) error(%v)", key, errors.WithStack(err))
  145. }
  146. }
  147. return
  148. }
  149. // SetWeight set weight
  150. func (d *Dao) SetWeight(c context.Context, businessID, flowID int64, id, weight int64) (err error) {
  151. conn := d.redis.Get(c)
  152. defer conn.Close()
  153. var (
  154. ow int64
  155. key = publicKey(businessID, flowID)
  156. zid = zsetKey(id)
  157. )
  158. if ow, err = redis.Int64(conn.Do("ZSCORE", key, zid)); err != nil {
  159. if err == redis.ErrNil {
  160. err = nil
  161. } else {
  162. log.Error("redis (ZSCORE,%s,%s) error(%v)", key, zid, err)
  163. }
  164. return
  165. }
  166. // 为了从大到小排序,weight取负值
  167. nw := -(weight + ow)
  168. if _, err = conn.Do("ZINCRBY", key, nw, zid); err != nil {
  169. log.Error("redis (ZINCRBY,%s,%s,%d) error(%v)", key, nw, zid, err)
  170. return
  171. }
  172. return
  173. }
  174. // GetWeight get Weight
  175. func (d *Dao) GetWeight(c context.Context, businessID, flowID int64, id int64) (weight int64, err error) {
  176. conn := d.redis.Get(c)
  177. defer conn.Close()
  178. key := publicKey(businessID, flowID)
  179. weight, err = redis.Int64(conn.Do("ZSCORE", key, zsetKey(id)))
  180. if err != nil {
  181. if err == redis.ErrNil {
  182. err = nil
  183. } else {
  184. log.Error("conn.Do(ZSCORE %s %s) error(%v)", key, zsetKey(id), errors.WithStack(err))
  185. }
  186. }
  187. weight = -weight
  188. return
  189. }
  190. // TopWeights .
  191. func (d *Dao) TopWeights(c context.Context, businessID, flowID int64, toplen int64) (wis []*model.WeightItem, err error) {
  192. key := publicKey(businessID, flowID)
  193. return d.zrange(c, key, 0, toplen)
  194. }
  195. // CreateUnionSet 创建分身集合
  196. func (d *Dao) CreateUnionSet(c context.Context, businessID, flowID int64) (err error) {
  197. conn := d.redis.Get(c)
  198. defer conn.Close()
  199. key := publicKey(businessID, flowID)
  200. backKey := publicBackKey(businessID, flowID)
  201. if _, err = conn.Do("ZUNIONSTORE", backKey, 1, key); err != nil {
  202. log.Error("conn.Do(ZUNIONSTORE,%s) error(%v)", key, errors.WithStack(err))
  203. }
  204. return
  205. }
  206. // RangeUinonSet 批次取出
  207. func (d *Dao) RangeUinonSet(c context.Context, businessID, flowID int64, start, stop int64) (wis []*model.WeightItem, err error) {
  208. key := publicBackKey(businessID, flowID)
  209. return d.zrange(c, key, start, stop)
  210. }
  211. // DeleteUinonSet 清空分身
  212. func (d *Dao) DeleteUinonSet(c context.Context, businessID, flowID int64) (err error) {
  213. conn := d.redis.Get(c)
  214. defer conn.Close()
  215. backKey := publicBackKey(businessID, flowID)
  216. if _, err = conn.Do("DEL", backKey); err != nil {
  217. log.Error("conn.Do(ZUNIODELNSTORE,%s) error(%v)", backKey, errors.WithStack(err))
  218. }
  219. return
  220. }
  221. func (d *Dao) zrange(c context.Context, key string, start, stop int64) (wis []*model.WeightItem, err error) {
  222. conn := d.redis.Get(c)
  223. defer conn.Close()
  224. reply, err := redis.Int64s(conn.Do("ZRANGE", key, start, stop, "WITHSCORES"))
  225. if err != nil {
  226. log.Error("conn.Do(ZADD,%s) error(%v)", key, errors.WithStack(err))
  227. return
  228. }
  229. // 单数是id,双数是weight
  230. for i := 0; i < len(reply); i += 2 {
  231. wi := &model.WeightItem{}
  232. wi.ID = reply[i]
  233. wi.Weight = -reply[i+1]
  234. wis = append(wis, wi)
  235. }
  236. return
  237. }