fix
This commit is contained in:
1 parent
feebf7d3c1
commit
486502852e
41 files changed
+1311
-259
No files matched your search
@@ -135,16 +135,26 @@ type Job struct {
|
||||
// of ≤maxJobProducts so ClaimNext SKIP LOCKED workers stay parallelizable.
|
||||
// Absolute request cap is MaxStartProducts (100k–1M scale).
|
||||
func (p *Pipeline) StartJob(ctx context.Context, companyID, userID uuid.UUID, rawIDs []uuid.UUID, processingType string) ([]Job, error) {
|
||||
return p.startJobWithOrigin(ctx, companyID, userID, rawIDs, processingType, "dashboard")
|
||||
return p.startJobWithOrigin(ctx, companyID, userID, rawIDs, processingType, "dashboard", nil)
|
||||
}
|
||||
|
||||
// StartAPIJob uses the same processing pipeline and company-scoped catalog as the
|
||||
// dashboard, but completed API results do not require a dashboard approval action.
|
||||
func (p *Pipeline) StartAPIJob(ctx context.Context, companyID, userID uuid.UUID, rawIDs []uuid.UUID, processingType string) ([]Job, error) {
|
||||
return p.startJobWithOrigin(ctx, companyID, userID, rawIDs, processingType, "api")
|
||||
return p.startJobWithOrigin(ctx, companyID, userID, rawIDs, processingType, "api", nil)
|
||||
}
|
||||
|
||||
func (p *Pipeline) startJobWithOrigin(ctx context.Context, companyID, userID uuid.UUID, rawIDs []uuid.UUID, processingType, origin string) ([]Job, error) {
|
||||
// StartJobWithTypes preserves the exact outputs selected in the dashboard.
|
||||
func (p *Pipeline) StartJobWithTypes(ctx context.Context, companyID, userID uuid.UUID, rawIDs []uuid.UUID, processingType string, outputs []string) ([]Job, error) {
|
||||
return p.startJobWithOrigin(ctx, companyID, userID, rawIDs, processingType, "dashboard", outputs)
|
||||
}
|
||||
|
||||
// StartAPIJobWithTypes preserves explicit output selections for public API jobs.
|
||||
func (p *Pipeline) StartAPIJobWithTypes(ctx context.Context, companyID, userID uuid.UUID, rawIDs []uuid.UUID, processingType string, outputs []string) ([]Job, error) {
|
||||
return p.startJobWithOrigin(ctx, companyID, userID, rawIDs, processingType, "api", outputs)
|
||||
}
|
||||
|
||||
func (p *Pipeline) startJobWithOrigin(ctx context.Context, companyID, userID uuid.UUID, rawIDs []uuid.UUID, processingType, origin string, outputs []string) ([]Job, error) {
|
||||
if processingType == "" {
|
||||
processingType = "full"
|
||||
}
|
||||
@@ -174,7 +184,7 @@ func (p *Pipeline) startJobWithOrigin(ctx context.Context, companyID, userID uui
|
||||
}
|
||||
}
|
||||
if p.Billing != nil {
|
||||
if err := p.assertProcessingGates(ctx, companyID, processingType, len(owned)); err != nil {
|
||||
if err := p.assertProcessingGates(ctx, companyID, processingType, len(owned), outputs...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -189,6 +199,9 @@ func (p *Pipeline) startJobWithOrigin(ctx context.Context, companyID, userID uui
|
||||
firstStep = progress[0].Step
|
||||
}
|
||||
|
||||
if outputs == nil {
|
||||
outputs = []string{}
|
||||
}
|
||||
chunks := chunkUUIDs(owned, perJobProductCap())
|
||||
tx, err := p.Pool.Begin(ctx)
|
||||
if err != nil {
|
||||
@@ -201,9 +214,9 @@ func (p *Pipeline) startJobWithOrigin(ctx context.Context, companyID, userID uui
|
||||
var jobID uuid.UUID
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO processing_jobs (
|
||||
company_id, user_id, status, total_products, processing_type, current_step, step_progress, origin
|
||||
) VALUES ($1, $2, 'pending', $3, $4, $5, $6::jsonb, $7) RETURNING id`,
|
||||
companyID, userID, len(chunk), processingType, firstStep, progressJSON, origin).Scan(&jobID)
|
||||
company_id, user_id, status, total_products, processing_type, current_step, step_progress, origin, processing_types
|
||||
) VALUES ($1, $2, 'pending', $3, $4, $5, $6::jsonb, $7, $8) RETURNING id`,
|
||||
companyID, userID, len(chunk), processingType, firstStep, progressJSON, origin, outputs).Scan(&jobID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -229,7 +242,7 @@ func (p *Pipeline) startJobWithOrigin(ctx context.Context, companyID, userID uui
|
||||
}
|
||||
|
||||
// assertProcessingGates enforces plan features and credit/SKU caps before starting or retrying work.
|
||||
func (p *Pipeline) assertProcessingGates(ctx context.Context, companyID uuid.UUID, processingType string, batchSize int) error {
|
||||
func (p *Pipeline) assertProcessingGates(ctx context.Context, companyID uuid.UUID, processingType string, batchSize int, outputs ...string) error {
|
||||
if p == nil || p.Billing == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -240,6 +253,11 @@ func (p *Pipeline) assertProcessingGates(ctx context.Context, companyID uuid.UUI
|
||||
RequiresAI: billing.ProcessingTypeRequiresAI(processingType) || billing.ProcessingTypeIsEmailCampaignAI(processingType),
|
||||
RequiresEPREL: billing.ProcessingTypeRequiresEPREL(processingType),
|
||||
}
|
||||
if rates, err := p.Billing.ContentRates(ctx, companyID); err == nil {
|
||||
if units, err := billing.ContentDebitUnits(outputs, processingType, rates); err == nil {
|
||||
opts.CreditsPerProduct = float64(units) / 10000
|
||||
}
|
||||
}
|
||||
return p.Billing.AssertCanStartProcessing(ctx, companyID, batchSize, opts)
|
||||
}
|
||||
|
||||
@@ -545,7 +563,7 @@ func (p *Pipeline) RetryJob(ctx context.Context, companyID, id uuid.UUID) (Job,
|
||||
if p.Limiter != nil && !p.Limiter.Allow(companyID) {
|
||||
return Job{}, ErrRateLimited
|
||||
}
|
||||
if err := p.assertProcessingGates(ctx, companyID, job.ProcessingType, job.TotalProducts); err != nil {
|
||||
if err := p.assertProcessingGates(ctx, companyID, job.ProcessingType, job.TotalProducts, selectedOutputs...); err != nil {
|
||||
return Job{}, err
|
||||
}
|
||||
|
||||
@@ -601,11 +619,12 @@ func (p *Pipeline) ProcessJob(ctx context.Context, jobID uuid.UUID) error {
|
||||
var companyID uuid.UUID
|
||||
var jobUserID *uuid.UUID
|
||||
var status, processingType, origin string
|
||||
var selectedOutputs []string
|
||||
var alreadyProcessed int
|
||||
err := p.Pool.QueryRow(ctx, `
|
||||
SELECT company_id, user_id, status, processing_type, processed_products, origin
|
||||
SELECT company_id, user_id, status, processing_type, processed_products, origin, processing_types
|
||||
FROM processing_jobs WHERE id = $1`, jobID).
|
||||
Scan(&companyID, &jobUserID, &status, &processingType, &alreadyProcessed, &origin)
|
||||
Scan(&companyID, &jobUserID, &status, &processingType, &alreadyProcessed, &origin, &selectedOutputs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -765,7 +784,7 @@ func (p *Pipeline) ProcessJob(ctx context.Context, jobID uuid.UUID) error {
|
||||
return fmt.Errorf("processing: check cancel job=%s: %w", jobID, err)
|
||||
}
|
||||
}
|
||||
ok, tokens, result, itemErr := p.processOne(ctx, companyID, jobID, &items[i], processingType, origin, jobEngine, modeLabel, usingBYOK, &jobCache)
|
||||
ok, tokens, result, itemErr := p.processOne(ctx, companyID, jobID, &items[i], processingType, selectedOutputs, origin, jobEngine, modeLabel, usingBYOK, &jobCache)
|
||||
if itemErr != nil {
|
||||
failed++
|
||||
if _, err := p.Pool.Exec(ctx, `
|
||||
@@ -1454,7 +1473,7 @@ func (p *Pipeline) reclaimOrphanedProcessingItems(ctx context.Context, jobID uui
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *Pipeline) processOne(ctx context.Context, companyID, jobID uuid.UUID, it *jobItem, processingType, origin string, engine *Engine, modeLabel string, usingBYOK bool, cache *jobScopedCache) (bool, int, StepResult, error) {
|
||||
func (p *Pipeline) processOne(ctx context.Context, companyID, jobID uuid.UUID, it *jobItem, processingType string, selectedOutputs []string, origin string, engine *Engine, modeLabel string, usingBYOK bool, cache *jobScopedCache) (bool, int, StepResult, error) {
|
||||
if it == nil {
|
||||
return false, 0, StepResult{}, fmt.Errorf("processing: nil job item")
|
||||
}
|
||||
@@ -1706,11 +1725,24 @@ func (p *Pipeline) processOne(ctx context.Context, companyID, jobID uuid.UUID, i
|
||||
// Debit before persisting the deliverable so insufficient credits cannot yield free AI output.
|
||||
// BYOK (company key): skip managed credit burn for inference.
|
||||
// Hash-skip (ai_enhance_unchanged): skip flat product_processing debit — no LLM/rework.
|
||||
if p.Billing != nil && shouldDebitProductProcessing(usingBYOK, result) {
|
||||
if err := p.Billing.ConsumeCreditsTx(ctx, tx, companyID, result.TotalTokens, "product_processing"); err != nil {
|
||||
billable := p.Billing != nil && shouldDebitProductProcessing(usingBYOK, result)
|
||||
if billable && result.TotalTokens == 0 {
|
||||
// Normalization-only jobs on Free cannot consume managed AI credits.
|
||||
if ent, entErr := p.Billing.EntitlementsForCompany(ctx, companyID); entErr != nil {
|
||||
return false, 0, result, entErr
|
||||
} else if !ent.CanUseAI {
|
||||
billable = false
|
||||
}
|
||||
}
|
||||
if billable {
|
||||
debit, err := p.Billing.ConsumeContentCreditsTx(ctx, tx, companyID, selectedOutputs, processingType)
|
||||
if err != nil {
|
||||
return false, 0, result, err
|
||||
}
|
||||
cache.noteCreditDebit(p.Billing.EstimateDebitForCompany(ctx, companyID, "product_processing", result.TotalTokens))
|
||||
// Cache tracks whole-credit safety only; the database debit checks every fraction.
|
||||
if debit >= 1 {
|
||||
cache.noteCreditDebit(int(debit))
|
||||
}
|
||||
}
|
||||
|
||||
processedID, err := p.upsertProcessedProduct(ctx, tx, companyID, it.RawID, gtin, result, attrsJSON, procAttrsJSON, gptJSON, sourcesJSON, providerMode, language, origin)
|
||||
|
||||
Reference in new issue
Block a user