dao.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/job/main/push/conf"
  5. "go-common/library/cache/memcache"
  6. xsql "go-common/library/database/sql"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/stat/prom"
  9. )
  10. // Dao .
  11. type Dao struct {
  12. c *conf.Config
  13. db *xsql.DB
  14. mc *memcache.Pool
  15. httpClient *bm.Client
  16. dpClient *bm.Client
  17. delCallbacksStmt *xsql.Stmt
  18. delTasksStmt *xsql.Stmt
  19. reportLastIDStmt *xsql.Stmt
  20. reportsByRangeStmt *xsql.Stmt
  21. updateTaskStatusStmt *xsql.Stmt
  22. updateTaskStmt *xsql.Stmt
  23. updateDpCondStmt *xsql.Stmt
  24. }
  25. var (
  26. errorsCount = prom.BusinessErrCount
  27. infosCount = prom.BusinessInfoCount
  28. )
  29. // New creates a dao instance.
  30. func New(c *conf.Config) (d *Dao) {
  31. d = &Dao{
  32. c: c,
  33. db: xsql.NewMySQL(c.MySQL),
  34. mc: memcache.NewPool(c.Memcache.Config),
  35. httpClient: bm.NewClient(c.HTTPClient),
  36. dpClient: bm.NewClient(c.DpClient),
  37. }
  38. d.delCallbacksStmt = d.db.Prepared(_delCallbacksSQL)
  39. d.delTasksStmt = d.db.Prepared(_delTasksSQL)
  40. d.reportLastIDStmt = d.db.Prepared(_reportLastIDSQL)
  41. d.reportsByRangeStmt = d.db.Prepared(_reportsByRangeSQL)
  42. d.updateTaskStatusStmt = d.db.Prepared(_upadteTaskStatusSQL)
  43. d.updateTaskStmt = d.db.Prepared(_upadteTaskSQL)
  44. d.updateDpCondStmt = d.db.Prepared(_updateDpCondSQL)
  45. return
  46. }
  47. // PromError prometheus error count.
  48. func PromError(name string) {
  49. errorsCount.Incr(name)
  50. }
  51. // PromInfo prometheus info count.
  52. func PromInfo(name string) {
  53. infosCount.Incr(name)
  54. }
  55. // Ping reports the health of the db/cache etc.
  56. func (d *Dao) Ping(c context.Context) (err error) {
  57. if err = d.db.Ping(c); err != nil {
  58. return
  59. }
  60. err = d.pingMC(c)
  61. return
  62. }
  63. // Close .
  64. func (d *Dao) Close() {
  65. d.db.Close()
  66. d.mc.Close()
  67. }