feat(tenant): 隔离客服通知文件与后台任务
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/sms"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
@@ -77,13 +78,32 @@ func (j *Job) run(ctx context.Context) {
|
||||
}
|
||||
defer release()
|
||||
|
||||
enqueued, err := j.RunOnce(ctx)
|
||||
tenantIDs, err := j.activeTenantIDs(ctx)
|
||||
if err != nil {
|
||||
j.logger.Warn("上架未成交降价提醒扫描失败", zap.Error(err))
|
||||
j.logger.Warn("读取经营租户失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
if enqueued > 0 {
|
||||
j.logger.Info("上架未成交降价提醒已入队", zap.Int("count", enqueued))
|
||||
total := 0
|
||||
if len(tenantIDs) == 0 {
|
||||
tenantIDs = []uint64{0}
|
||||
}
|
||||
for _, tenantID := range tenantIDs {
|
||||
tenantCtx := ctx
|
||||
if tenantID > 0 {
|
||||
tenantCtx = tenancy.WithScope(ctx, tenancy.Scope{TenantID: tenantID})
|
||||
}
|
||||
enqueued, err := j.RunOnce(tenantCtx)
|
||||
if err != nil {
|
||||
j.logger.Warn("上架未成交降价提醒扫描失败",
|
||||
zap.Uint64("tenant_id", tenantID),
|
||||
zap.Error(err),
|
||||
)
|
||||
continue
|
||||
}
|
||||
total += enqueued
|
||||
}
|
||||
if total > 0 {
|
||||
j.logger.Info("上架未成交降价提醒已入队", zap.Int("count", total))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,14 +126,18 @@ func (j *Job) RunOnce(ctx context.Context) (int, error) {
|
||||
// NOT EXISTS 排除已提醒过的账号,否则已入队的记录会一直占满 LIMIT 名额:
|
||||
// 候选窗口有 24 小时,任务每小时跑一次,若窗口内候选超过 batchSize,
|
||||
// 每次都会查回同一批「已提醒」的账号,排在后面的账号会在窗口结束后被永久漏发。
|
||||
err = j.db.WithContext(ctx).
|
||||
Select("id", "owner_id", "distribution_site_id", "published_at").
|
||||
query := j.db.WithContext(ctx).
|
||||
Select("id", "owner_id", "source_tenant_id", "distribution_site_id", "published_at").
|
||||
Where("status = ? AND in_transaction = ?", "published", false).
|
||||
Where("published_at IS NOT NULL AND published_at <= ? AND published_at > ?", threshold, windowStart).
|
||||
Where(`NOT EXISTS (
|
||||
SELECT 1 FROM sms_messages sm
|
||||
WHERE sm.node = ? AND sm.biz_type = ? AND sm.biz_id = rental_listings.id
|
||||
)`, sms.NodeListingStalePriceSuggestion, "listing").
|
||||
)`, sms.NodeListingStalePriceSuggestion, "listing")
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("source_tenant_id = ?", tenantID)
|
||||
}
|
||||
err = query.
|
||||
Order("id ASC").
|
||||
Limit(j.batchSize).
|
||||
Find(&rows).Error
|
||||
@@ -130,6 +154,7 @@ func (j *Job) RunOnce(ctx context.Context) (int, error) {
|
||||
continue
|
||||
}
|
||||
inserted, err := sms.Enqueue(j.db.WithContext(ctx), sms.Entry{
|
||||
TenantID: row.SourceTenantID,
|
||||
UserID: row.OwnerID,
|
||||
Node: sms.NodeListingStalePriceSuggestion,
|
||||
BizType: "listing",
|
||||
@@ -150,6 +175,15 @@ func (j *Job) RunOnce(ctx context.Context) (int, error) {
|
||||
return enqueued, nil
|
||||
}
|
||||
|
||||
func (j *Job) activeTenantIDs(ctx context.Context) ([]uint64, error) {
|
||||
var ids []uint64
|
||||
err := j.db.WithContext(ctx).Table("tenants").
|
||||
Where("status = ?", "active").
|
||||
Order("id ASC").
|
||||
Pluck("id", &ids).Error
|
||||
return ids, err
|
||||
}
|
||||
|
||||
func (j *Job) acquireLock(ctx context.Context) (func(), bool) {
|
||||
if j.redis == nil {
|
||||
return func() {}, true
|
||||
|
||||
Reference in New Issue
Block a user