Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Fix Space drip scheduling and candidate scan

Anand Baburajanf79258ccf129
diff --git a/server/space/controller/drips.go b/server/space/controller/drips.go
index c9caef373f..326b929d9b 100644
--- a/server/space/controller/drips.go
+++ b/server/space/controller/drips.go
@@ -65,10 +65,9 @@ func (c *SpaceDripController) ProcessSpaceDrips() {
}
if c.LockController != nil {
if !c.LockController.TryLock(spaceDripMailLock, timeutil.MicrosecondsAfterHours(24)) {
- log.Info("Skipping space drip emails because another instance is running")
+ log.Info("Skipping space drip emails because the daily lock is held")
return
}
- defer c.LockController.ReleaseLock(spaceDripMailLock)
}
stats, err := c.processSpaceDrips(context.Background(), timeutil.Microseconds())
if err != nil {
diff --git a/server/space/controller/drips_test.go b/server/space/controller/drips_test.go
index 8732aebc5e..34bc220765 100644
--- a/server/space/controller/drips_test.go
+++ b/server/space/controller/drips_test.go
@@ -6,12 +6,43 @@ import (
"testing"
"github.com/ente/museum/internal/testutil"
+ lockctrl "github.com/ente/museum/pkg/controller/lock"
baserepo "github.com/ente/museum/pkg/repo"
timeutil "github.com/ente/museum/pkg/utils/time"
spacerepo "github.com/ente/museum/space/repo"
"github.com/stretchr/testify/require"
)
+func TestProcessSpaceDripsKeepsDailyLock(t *testing.T) {
+ testutil.WithServerRoot(t)
+ db := testutil.RequireTestDB(t)
+ testutil.ResetTables(t, db)
+ t.Cleanup(func() {
+ testutil.ResetTables(t, db)
+ })
+
+ repos := spacerepo.NewModule(db, nil)
+ controller := &SpaceDripController{
+ DripsRepo: repos.Drips,
+ UserRepo: &baserepo.UserRepository{
+ DB: db,
+ SecretEncryptionKey: testutil.SecretEncryptionKey(),
+ HashingKey: testutil.HashingKey(),
+ },
+ NotificationHistoryRepo: &baserepo.NotificationHistoryRepository{DB: db},
+ LockController: &lockctrl.LockController{
+ TaskLockingRepo: &baserepo.TaskLockRepository{DB: db},
+ HostName: "space-drip-test",
+ },
+ }
+
+ controller.ProcessSpaceDrips()
+
+ var lockUntil int64
+ require.NoError(t, db.QueryRow(`SELECT lock_until FROM task_lock WHERE task_name = $1`, spaceDripMailLock).Scan(&lockUntil))
+ require.Greater(t, lockUntil, timeutil.Microseconds())
+}
+
func TestSpaceDripsSendMatureProfileNudgeOnly(t *testing.T) {
testutil.WithServerRoot(t)
db := testutil.RequireTestDB(t)
diff --git a/server/space/repo/drips.go b/server/space/repo/drips.go
index 11a652c238..7d275784e2 100644
--- a/server/space/repo/drips.go
+++ b/server/space/repo/drips.go
@@ -132,18 +132,27 @@ func (r *DripsRepository) ListFirstPostCandidates(ctx context.Context, threshold
func (r *DripsRepository) ListFeedbackCandidates(ctx context.Context, threshold int64, excludeTemplateIDs []string, limit int) ([]SpaceDripCandidate, error) {
rows, err := r.DB.QueryContext(ctx, `
- WITH activities AS (
- SELECT s.owner_id AS user_id, p.created_at AS event_at
+ WITH eligible_spaces AS MATERIALIZED (
+ SELECT s.space_id, s.owner_id
FROM spaces s
JOIN users u ON u.user_id = s.owner_id AND u.encrypted_email IS NOT NULL
+ WHERE NOT EXISTS (
+ SELECT 1
+ FROM notification_history nh
+ WHERE nh.user_id = s.owner_id
+ AND nh.template_id = ANY($2)
+ )
+ ),
+ activities AS (
+ SELECT s.owner_id AS user_id, p.created_at AS event_at
+ FROM eligible_spaces s
JOIN space_posts p ON p.space_id = s.space_id
WHERE p.created_at <= $1
UNION ALL
SELECT s.owner_id AS user_id, m.created_at AS event_at
- FROM spaces s
- JOIN users u ON u.user_id = s.owner_id AND u.encrypted_email IS NOT NULL
+ FROM eligible_spaces s
JOIN space_messages m ON m.sender_space_id = s.space_id
WHERE m.kind = 'post_reply'
AND m.created_at <= $1
@@ -155,12 +164,6 @@ func (r *DripsRepository) ListFeedbackCandidates(ctx context.Context, threshold
)
SELECT fa.user_id
FROM first_activity fa
- WHERE NOT EXISTS (
- SELECT 1
- FROM notification_history nh
- WHERE nh.user_id = fa.user_id
- AND nh.template_id = ANY($2)
- )
ORDER BY fa.event_at ASC, fa.user_id ASC
LIMIT $3
`, threshold, pq.Array(excludeTemplateIDs), spaceDripCandidateLimit(limit))
diff --git a/server/space/repo/drips_test.go b/server/space/repo/drips_test.go
index a1cf2a2499..099366fb61 100644
--- a/server/space/repo/drips_test.go
+++ b/server/space/repo/drips_test.go
@@ -119,6 +119,15 @@ func TestFirstPostAndFeedbackDripCandidates(t *testing.T) {
feedbackCandidates, err := module.Drips.ListFeedbackCandidates(ctx, now-7*24*timeutil.MicroSecondsInOneHour, []string{testSpaceDripFeedback7d}, 50)
require.NoError(t, err)
require.ElementsMatch(t, []int64{aliceID, bobID}, spaceDripCandidateUserIDs(feedbackCandidates))
+
+ testutil.InsertNotificationHistory(t, module.Drips.DB, testutil.NotificationHistoryFixture{
+ UserID: aliceID,
+ TemplateID: testSpaceDripFeedback7d,
+ SentTime: now,
+ })
+ feedbackCandidates, err = module.Drips.ListFeedbackCandidates(ctx, now-7*24*timeutil.MicroSecondsInOneHour, []string{testSpaceDripFeedback7d}, 50)
+ require.NoError(t, err)
+ require.Equal(t, []int64{bobID}, spaceDripCandidateUserIDs(feedbackCandidates))
}
func insertSpaceBrowserSession(t *testing.T, module *Module, userID int64, createdAt int64, expiresAt int64) {