backfillqueue.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. "fmt"
  21. "time"
  22. log "maunium.net/go/maulogger/v2"
  23. "maunium.net/go/mautrix/id"
  24. )
  25. type BackfillType int
  26. const (
  27. BackfillImmediate BackfillType = 0
  28. BackfillDeferred = 1
  29. BackfillMedia = 2
  30. )
  31. func (bt BackfillType) String() string {
  32. switch bt {
  33. case BackfillImmediate:
  34. return "IMMEDIATE"
  35. case BackfillDeferred:
  36. return "DEFERRED"
  37. case BackfillMedia:
  38. return "MEDIA"
  39. }
  40. return "UNKNOWN"
  41. }
  42. type BackfillQuery struct {
  43. db *Database
  44. log log.Logger
  45. }
  46. func (bq *BackfillQuery) New() *Backfill {
  47. return &Backfill{
  48. db: bq.db,
  49. log: bq.log,
  50. Portal: &PortalKey{},
  51. }
  52. }
  53. func (bq *BackfillQuery) NewWithValues(userID id.UserID, backfillType BackfillType, priority int, portal *PortalKey, timeStart *time.Time, timeEnd *time.Time, maxBatchEvents, maxTotalEvents, batchDelay int) *Backfill {
  54. return &Backfill{
  55. db: bq.db,
  56. log: bq.log,
  57. UserID: userID,
  58. BackfillType: backfillType,
  59. Priority: priority,
  60. Portal: portal,
  61. TimeStart: timeStart,
  62. TimeEnd: timeEnd,
  63. MaxBatchEvents: maxBatchEvents,
  64. MaxTotalEvents: maxTotalEvents,
  65. BatchDelay: batchDelay,
  66. }
  67. }
  68. const (
  69. getNextBackfillQuery = `
  70. SELECT queue_id, user_mxid, type, priority, portal_jid, portal_receiver, time_start, time_end, max_batch_events, max_total_events, batch_delay
  71. FROM backfill_queue
  72. WHERE user_mxid=$1
  73. AND type=$2
  74. AND completed_at IS NULL
  75. ORDER BY priority, queue_id
  76. LIMIT 1
  77. `
  78. )
  79. // GetNext returns the next backfill to perform
  80. func (bq *BackfillQuery) GetNext(userID id.UserID, backfillType BackfillType) (backfill *Backfill) {
  81. rows, err := bq.db.Query(getNextBackfillQuery, userID, backfillType)
  82. defer rows.Close()
  83. if err != nil || rows == nil {
  84. bq.log.Error(err)
  85. return
  86. }
  87. if rows.Next() {
  88. backfill = bq.New().Scan(rows)
  89. }
  90. return
  91. }
  92. func (bq *BackfillQuery) DeleteAll(userID id.UserID) error {
  93. _, err := bq.db.Exec("DELETE FROM backfill_queue WHERE user_mxid=$1", userID)
  94. return err
  95. }
  96. func (bq *BackfillQuery) DeleteAllForPortal(userID id.UserID, portalKey PortalKey) error {
  97. _, err := bq.db.Exec(`
  98. DELETE FROM backfill_queue
  99. WHERE user_mxid=$1
  100. AND portal_jid=$2
  101. AND portal_receiver=$3
  102. `, userID, portalKey.JID, portalKey.Receiver)
  103. return err
  104. }
  105. type Backfill struct {
  106. db *Database
  107. log log.Logger
  108. // Fields
  109. QueueID int
  110. UserID id.UserID
  111. BackfillType BackfillType
  112. Priority int
  113. Portal *PortalKey
  114. TimeStart *time.Time
  115. TimeEnd *time.Time
  116. MaxBatchEvents int
  117. MaxTotalEvents int
  118. BatchDelay int
  119. CompletedAt *time.Time
  120. }
  121. func (b *Backfill) String() string {
  122. return fmt.Sprintf("Backfill{QueueID: %d, UserID: %s, BackfillType: %s, Priority: %d, Portal: %s, TimeStart: %s, TimeEnd: %s, MaxBatchEvents: %d, MaxTotalEvents: %d, BatchDelay: %d, CompletedAt: %s}",
  123. b.QueueID, b.UserID, b.BackfillType, b.Priority, b.Portal, b.TimeStart, b.TimeEnd, b.MaxBatchEvents, b.MaxTotalEvents, b.BatchDelay, b.CompletedAt,
  124. )
  125. }
  126. func (b *Backfill) Scan(row Scannable) *Backfill {
  127. 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)
  128. if err != nil {
  129. if !errors.Is(err, sql.ErrNoRows) {
  130. b.log.Errorln("Database scan failed:", err)
  131. }
  132. return nil
  133. }
  134. return b
  135. }
  136. func (b *Backfill) Insert() {
  137. rows, err := b.db.Query(`
  138. INSERT INTO backfill_queue
  139. (user_mxid, type, priority, portal_jid, portal_receiver, time_start, time_end, max_batch_events, max_total_events, batch_delay, completed_at)
  140. VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
  141. RETURNING queue_id
  142. `, b.UserID, b.BackfillType, b.Priority, b.Portal.JID, b.Portal.Receiver, b.TimeStart, b.TimeEnd, b.MaxBatchEvents, b.MaxTotalEvents, b.BatchDelay, b.CompletedAt)
  143. defer rows.Close()
  144. if err != nil || !rows.Next() {
  145. b.log.Warnfln("Failed to insert %v/%s with priority %d: %v", b.BackfillType, b.Portal.JID, b.Priority, err)
  146. return
  147. }
  148. err = rows.Scan(&b.QueueID)
  149. if err != nil {
  150. b.log.Warnfln("Failed to insert %s/%s with priority %s: %v", b.BackfillType, b.Portal.JID, b.Priority, err)
  151. }
  152. }
  153. func (b *Backfill) MarkDone() {
  154. if b.QueueID == 0 {
  155. b.log.Errorf("Cannot delete backfill without queue_id. Maybe it wasn't actually inserted in the database?")
  156. return
  157. }
  158. _, err := b.db.Exec("UPDATE backfill_queue SET completed_at=$1 WHERE queue_id=$2", time.Now(), b.QueueID)
  159. if err != nil {
  160. b.log.Warnfln("Failed to mark %s/%s as complete: %v", b.BackfillType, b.Priority, err)
  161. }
  162. }