backfillqueue.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2021 Tulir Asokan, Sumner Evans
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package database
  17. import (
  18. "database/sql"
  19. "errors"
  20. "time"
  21. log "maunium.net/go/maulogger/v2"
  22. "maunium.net/go/mautrix/id"
  23. )
  24. type BackfillType int
  25. const (
  26. BackfillImmediate BackfillType = 0
  27. BackfillDeferred = 1
  28. )
  29. type BackfillQuery struct {
  30. db *Database
  31. log log.Logger
  32. }
  33. func (bq *BackfillQuery) New() *Backfill {
  34. return &Backfill{
  35. db: bq.db,
  36. log: bq.log,
  37. Portal: &PortalKey{},
  38. }
  39. }
  40. func (bq *BackfillQuery) NewWithValues(userID id.UserID, backfillType BackfillType, priority int, portal *PortalKey, timeStart *time.Time, timeEnd *time.Time, maxBatchEvents, maxTotalEvents, batchDelay int) *Backfill {
  41. return &Backfill{
  42. db: bq.db,
  43. log: bq.log,
  44. UserID: userID,
  45. BackfillType: backfillType,
  46. Priority: priority,
  47. Portal: portal,
  48. TimeStart: timeStart,
  49. TimeEnd: timeEnd,
  50. MaxBatchEvents: maxBatchEvents,
  51. MaxTotalEvents: maxTotalEvents,
  52. BatchDelay: batchDelay,
  53. }
  54. }
  55. const (
  56. getNextBackfillQuery = `
  57. SELECT queue_id, user_mxid, type, priority, portal_jid, portal_receiver, time_start, time_end, max_batch_events, max_total_events, batch_delay
  58. FROM backfill_queue
  59. WHERE user_mxid=$1
  60. AND type=$2
  61. AND completed_at IS NULL
  62. ORDER BY priority, queue_id
  63. LIMIT 1
  64. `
  65. )
  66. /// Returns the next backfill to perform
  67. func (bq *BackfillQuery) GetNext(userID id.UserID, backfillType BackfillType) (backfill *Backfill) {
  68. rows, err := bq.db.Query(getNextBackfillQuery, userID, backfillType)
  69. defer rows.Close()
  70. if err != nil || rows == nil {
  71. bq.log.Error(err)
  72. return
  73. }
  74. if rows.Next() {
  75. backfill = bq.New().Scan(rows)
  76. }
  77. return
  78. }
  79. func (bq *BackfillQuery) DeleteAll(userID id.UserID) error {
  80. _, err := bq.db.Exec("DELETE FROM backfill_queue WHERE user_mxid=$1", userID)
  81. return err
  82. }
  83. type Backfill struct {
  84. db *Database
  85. log log.Logger
  86. // Fields
  87. QueueID int
  88. UserID id.UserID
  89. BackfillType BackfillType
  90. Priority int
  91. Portal *PortalKey
  92. TimeStart *time.Time
  93. TimeEnd *time.Time
  94. MaxBatchEvents int
  95. MaxTotalEvents int
  96. BatchDelay int
  97. CompletedAt *time.Time
  98. }
  99. func (b *Backfill) Scan(row Scannable) *Backfill {
  100. err := row.Scan(&b.QueueID, &b.UserID, &b.BackfillType, &b.Priority, &b.Portal.JID, &b.Portal.Receiver, &b.TimeStart, &b.TimeEnd, &b.MaxBatchEvents, &b.MaxTotalEvents, &b.BatchDelay)
  101. if err != nil {
  102. if !errors.Is(err, sql.ErrNoRows) {
  103. b.log.Errorln("Database scan failed:", err)
  104. }
  105. return nil
  106. }
  107. return b
  108. }
  109. func (b *Backfill) Insert() {
  110. rows, err := b.db.Query(`
  111. INSERT INTO backfill_queue
  112. (user_mxid, type, priority, portal_jid, portal_receiver, time_start, time_end, max_batch_events, max_total_events, batch_delay, completed_at)
  113. VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
  114. RETURNING queue_id
  115. `, b.UserID, b.BackfillType, b.Priority, b.Portal.JID, b.Portal.Receiver, b.TimeStart, b.TimeEnd, b.MaxBatchEvents, b.MaxTotalEvents, b.BatchDelay, b.CompletedAt)
  116. defer rows.Close()
  117. if err != nil || !rows.Next() {
  118. b.log.Warnfln("Failed to insert %v/%s with priority %d: %v", b.BackfillType, b.Portal.JID, b.Priority, err)
  119. return
  120. }
  121. err = rows.Scan(&b.QueueID)
  122. if err != nil {
  123. b.log.Warnfln("Failed to insert %s/%s with priority %s: %v", b.BackfillType, b.Portal.JID, b.Priority, err)
  124. }
  125. }
  126. func (b *Backfill) MarkDone() {
  127. if b.QueueID == 0 {
  128. b.log.Errorf("Cannot delete backfill without queue_id. Maybe it wasn't actually inserted in the database?")
  129. return
  130. }
  131. _, err := b.db.Exec("UPDATE backfill_queue SET completed_at=$1 WHERE queue_id=$2", time.Now(), b.QueueID)
  132. if err != nil {
  133. b.log.Warnfln("Failed to mark %s/%s as complete: %v", b.BackfillType, b.Priority, err)
  134. }
  135. }