backfillqueue.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. ORDER BY priority, queue_id
  62. LIMIT 1
  63. `
  64. )
  65. /// Returns the next backfill to perform
  66. func (bq *BackfillQuery) GetNext(userID id.UserID, backfillType BackfillType) (backfill *Backfill) {
  67. rows, err := bq.db.Query(getNextBackfillQuery, userID, backfillType)
  68. defer rows.Close()
  69. if err != nil || rows == nil {
  70. bq.log.Error(err)
  71. return
  72. }
  73. if rows.Next() {
  74. backfill = bq.New().Scan(rows)
  75. }
  76. return
  77. }
  78. func (bq *BackfillQuery) DeleteAll(userID id.UserID) error {
  79. _, err := bq.db.Exec("DELETE FROM backfill_queue WHERE user_mxid=$1", userID)
  80. return err
  81. }
  82. type Backfill struct {
  83. db *Database
  84. log log.Logger
  85. // Fields
  86. QueueID int
  87. UserID id.UserID
  88. BackfillType BackfillType
  89. Priority int
  90. Portal *PortalKey
  91. TimeStart *time.Time
  92. TimeEnd *time.Time
  93. MaxBatchEvents int
  94. MaxTotalEvents int
  95. BatchDelay int
  96. }
  97. func (b *Backfill) Scan(row Scannable) *Backfill {
  98. 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)
  99. if err != nil {
  100. if !errors.Is(err, sql.ErrNoRows) {
  101. b.log.Errorln("Database scan failed:", err)
  102. }
  103. return nil
  104. }
  105. return b
  106. }
  107. func (b *Backfill) Insert() {
  108. rows, err := b.db.Query(`
  109. INSERT INTO backfill_queue
  110. (user_mxid, type, priority, portal_jid, portal_receiver, time_start, time_end, max_batch_events, max_total_events, batch_delay)
  111. VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
  112. RETURNING queue_id
  113. `, b.UserID, b.BackfillType, b.Priority, b.Portal.JID, b.Portal.Receiver, b.TimeStart, b.TimeEnd, b.MaxBatchEvents, b.MaxTotalEvents, b.BatchDelay)
  114. defer rows.Close()
  115. if err != nil || !rows.Next() {
  116. b.log.Warnfln("Failed to insert %v/%s with priority %d: %v", b.BackfillType, b.Portal.JID, b.Priority, err)
  117. return
  118. }
  119. err = rows.Scan(&b.QueueID)
  120. if err != nil {
  121. b.log.Warnfln("Failed to insert %s/%s with priority %s: %v", b.BackfillType, b.Portal.JID, b.Priority, err)
  122. }
  123. }
  124. func (b *Backfill) Delete() {
  125. if b.QueueID == 0 {
  126. b.log.Errorf("Cannot delete backfill without queue_id. Maybe it wasn't actually inserted in the database?")
  127. return
  128. }
  129. _, err := b.db.Exec("DELETE FROM backfill_queue WHERE queue_id=$1", b.QueueID)
  130. if err != nil {
  131. b.log.Warnfln("Failed to delete %s/%s: %v", b.BackfillType, b.Priority, err)
  132. }
  133. }