feat(t240): cache freight item images
This commit is contained in:
@@ -22,6 +22,7 @@ const (
|
||||
freightWatermarkOverlap = 10 * time.Minute
|
||||
orderFreightSyncTimeout = 55 * time.Second
|
||||
freightCleanupTimeout = 5 * time.Second
|
||||
freightImageCacheBudget = 15 * time.Second
|
||||
)
|
||||
|
||||
type FreightService struct {
|
||||
@@ -33,6 +34,19 @@ type FreightService struct {
|
||||
orderTimeout time.Duration
|
||||
cleanupTimeout time.Duration
|
||||
orderSyncGate chan struct{}
|
||||
imageCache FreightImageCache
|
||||
}
|
||||
|
||||
type FreightServiceOption func(*FreightService) error
|
||||
|
||||
func WithFreightImageCache(cache FreightImageCache) FreightServiceOption {
|
||||
return func(service *FreightService) error {
|
||||
if cache == nil {
|
||||
return errors.New("freight image cache is required")
|
||||
}
|
||||
service.imageCache = cache
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type FreightSourcePreflight interface {
|
||||
@@ -66,12 +80,13 @@ func NewFreightService(
|
||||
clock Clock,
|
||||
ids IDGenerator,
|
||||
timeout time.Duration,
|
||||
options ...FreightServiceOption,
|
||||
) (*FreightService, error) {
|
||||
if repository == nil || source == nil || clock == nil || ids == nil ||
|
||||
timeout <= 0 {
|
||||
return nil, errors.New("freight service dependencies are required")
|
||||
}
|
||||
return &FreightService{
|
||||
service := &FreightService{
|
||||
repository: repository,
|
||||
source: source,
|
||||
clock: clock,
|
||||
@@ -80,7 +95,16 @@ func NewFreightService(
|
||||
orderTimeout: orderFreightSyncTimeout,
|
||||
cleanupTimeout: freightCleanupTimeout,
|
||||
orderSyncGate: make(chan struct{}, 1),
|
||||
}, nil
|
||||
}
|
||||
for _, option := range options {
|
||||
if option == nil {
|
||||
return nil, errors.New("freight service option is required")
|
||||
}
|
||||
if err := option(service); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return service, nil
|
||||
}
|
||||
|
||||
func (service *FreightService) CreateOrderSync(
|
||||
@@ -424,6 +448,18 @@ func (service *FreightService) executeRun(
|
||||
run.OrderCount = len(batch.Orders)
|
||||
run.ItemCount = itemCount
|
||||
run.FinishedAt = &finishedAt
|
||||
if service.imageCache != nil {
|
||||
cacheCtx, cancel := context.WithTimeout(
|
||||
ctx,
|
||||
freightImageCacheBudget,
|
||||
)
|
||||
_ = service.imageCache.CacheRun(
|
||||
cacheCtx,
|
||||
run.CreatorSubject,
|
||||
run.ID,
|
||||
)
|
||||
cancel()
|
||||
}
|
||||
return run, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user