46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/worker"
|
|
)
|
|
|
|
const TaskTypeCaptainDocumentSync = "captain:document_sync"
|
|
|
|
type captainDocumentSyncJob struct {
|
|
AccountID uint `json:"account_id"`
|
|
DocumentID uint `json:"document_id"`
|
|
}
|
|
|
|
var captainDocumentRegistrations sync.Map
|
|
|
|
// RegisterCaptainDocumentJobs wires Captain::Documents::PerformSyncJob into
|
|
// the durable worker. The sync backend remains fakeable for tests and disabled
|
|
// deployments.
|
|
func RegisterCaptainDocumentJobs(wp *worker.WorkerPool, svc *CaptainDocumentService) {
|
|
if wp == nil || svc == nil {
|
|
return
|
|
}
|
|
if _, loaded := captainDocumentRegistrations.LoadOrStore(wp, struct{}{}); loaded {
|
|
return
|
|
}
|
|
wp.Register(TaskTypeCaptainDocumentSync, svc.performDocumentSyncJob)
|
|
}
|
|
|
|
func (s *CaptainDocumentService) performDocumentSyncJob(ctx context.Context, job *model.BackgroundJob) error {
|
|
var payload captainDocumentSyncJob
|
|
if err := json.Unmarshal(job.Payload, &payload); err != nil {
|
|
return fmt.Errorf("unmarshal captain document sync job: %w", err)
|
|
}
|
|
if payload.AccountID == 0 || payload.DocumentID == 0 {
|
|
return fmt.Errorf("invalid captain document sync job payload: %#v", payload)
|
|
}
|
|
_, err := s.SyncDocumentByAccount(ctx, payload.AccountID, payload.DocumentID)
|
|
return err
|
|
}
|