cal_diff.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package caldiff
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/app/job/main/appstatic/model"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _calDiffFmt = "SELECT id,`name`,type, md5, size, url, resource_id, file_type, from_ver FROM resource_file " +
  11. "WHERE file_type = ? AND url = ? AND is_deleted = 0 %s ORDER BY id DESC %s"
  12. _saveFile = "UPDATE resource_file SET url = ?, file_type = ?, md5 = ?, size = ?, `name` = ? WHERE id = ?"
  13. _parseRes = "SELECT id, `name`, version, pool_id FROM resource WHERE %s"
  14. _parseFile = "SELECT id, `name`, type, md5, size, url, resource_id, file_type, from_ver FROM resource_file WHERE" +
  15. " %s AND is_deleted = 0 %s"
  16. _updateStatus = "UPDATE resource_file SET file_type = ? WHERE id = ?"
  17. _diffPkg = 1
  18. )
  19. var (
  20. _calDiffNew = fmt.Sprintf(_calDiffFmt, " AND ctime = mtime", "LIMIT 1")
  21. _parseResID = fmt.Sprintf(_parseRes, " id = ?")
  22. _parseResVer = fmt.Sprintf(_parseRes, " pool_id = ? AND version = ?")
  23. _getReadyFile = fmt.Sprintf(_parseFile, " resource_id = ? AND file_type = ? AND url != ?", "LIMIT 1")
  24. )
  25. // UpdateStatus updates the file's status
  26. func (d *Dao) UpdateStatus(c context.Context, status int, id int) (err error) {
  27. if _, err = d.db.Exec(c, _updateStatus, status, id); err != nil {
  28. log.Error("UpdateStatus ID %d, Err %v", id, err)
  29. }
  30. return
  31. }
  32. // ReadyFile takes the already generated file
  33. func (d *Dao) ReadyFile(c context.Context, resID int, ftype int) (file *model.ResourceFile, err error) {
  34. file = &model.ResourceFile{}
  35. row := d.db.QueryRow(c, _getReadyFile, resID, ftype, "")
  36. if err = row.Scan(&file.ID, &file.Name, &file.Type, &file.Md5, &file.Size, &file.URL, &file.ResourceID, &file.FileType, &file.FromVer); err != nil {
  37. log.Error("db.QueryRow(%s) (%d,%d) error(%v)", _getReadyFile, resID, ftype, err)
  38. }
  39. return
  40. }
  41. // ParseResVer takes one resource info
  42. func (d *Dao) ParseResVer(c context.Context, poolID int, version int) (res *model.Resource, err error) {
  43. res = &model.Resource{}
  44. row := d.db.QueryRow(c, _parseResVer, poolID, version)
  45. // "SELECT id, `name`, version, pool_id FROM resource WHERE pool_id = ? AND version = ?"
  46. if err = row.Scan(&res.ID, &res.Name, &res.Version, &res.PoolID); err != nil {
  47. log.Error("db.QueryRow(%s) (%d,%d) error(%v)", _parseResVer, poolID, version, err)
  48. }
  49. return
  50. }
  51. // ParseResID takes one resource info
  52. func (d *Dao) ParseResID(c context.Context, resID int) (res *model.Resource, err error) {
  53. res = &model.Resource{}
  54. row := d.db.QueryRow(c, _parseResID, resID)
  55. // "SELECT id, `name`, version, pool_id FROM resource WHERE id = ?"
  56. if err = row.Scan(&res.ID, &res.Name, &res.Version, &res.PoolID); err != nil {
  57. log.Error("db.QueryRow(%s) (%d) error(%v)", _parseResID, resID, err)
  58. }
  59. return
  60. }
  61. // DiffNew picks the recently created diff packages
  62. func (d *Dao) DiffNew(c context.Context) (file *model.ResourceFile, err error) {
  63. file = &model.ResourceFile{}
  64. row := d.db.QueryRow(c, _calDiffNew, _diffPkg, "")
  65. if err = row.Scan(&file.ID, &file.Name, &file.Type, &file.Md5, &file.Size, &file.URL, &file.ResourceID, &file.FileType, &file.FromVer); err != nil {
  66. if err == sql.ErrNoRows {
  67. err = nil
  68. file = nil
  69. } else {
  70. log.Error("db.QueryRow(%s) error(%v)", _calDiffNew, err)
  71. return
  72. }
  73. }
  74. return
  75. }
  76. // DiffRetry picks the recently created diff packages
  77. func (d *Dao) DiffRetry(c context.Context) (file *model.ResourceFile, err error) {
  78. file = &model.ResourceFile{}
  79. query := fmt.Sprintf(_calDiffFmt, " AND ctime != mtime AND mtime < date_sub(now(), INTERVAL "+d.c.Cfg.Diff.Retry+")", "LIMIT 1")
  80. row := d.db.QueryRow(c, query, _diffPkg, "")
  81. if err = row.Scan(&file.ID, &file.Name, &file.Type, &file.Md5, &file.Size, &file.URL, &file.ResourceID, &file.FileType, &file.FromVer); err != nil {
  82. if err == sql.ErrNoRows {
  83. err = nil
  84. file = nil
  85. } else {
  86. log.Error("db.QueryRow(%s) error(%v)", _calDiffNew, err)
  87. return
  88. }
  89. }
  90. return
  91. }
  92. // SaveFile saves the file info
  93. func (d *Dao) SaveFile(c context.Context, fileID int, file *model.FileInfo) (err error) {
  94. // "UPDATE resource_file SET url = ?, file_type = ?, md5 = ?, size = ?, `name` = ? WHERE id = ?"
  95. if _, err = d.db.Exec(c, _saveFile, file.URL, _diffPkg, file.Md5, file.Size, file.Name, fileID); err != nil {
  96. log.Error("SaveFile ID %d, Err %v", fileID, err)
  97. }
  98. return
  99. }