@@ -1,20 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Text.Json;
|
||||
using System.Formats.Tar;
|
||||
using System.IO.Compression;
|
||||
using Tiku.Application.Assets;
|
||||
using Tiku.Application.Content;
|
||||
using Tiku.Application.Jobs;
|
||||
using Tiku.Application.Security;
|
||||
using Tiku.Application.Tenancy;
|
||||
using Tiku.Domain.Commerce;
|
||||
using Tiku.Domain.Common;
|
||||
using Tiku.Domain.Content;
|
||||
using Tiku.Domain.Operations;
|
||||
using Tiku.Infrastructure.Persistence;
|
||||
using Tiku.Infrastructure.Observability;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Tiku.Infrastructure.Jobs;
|
||||
|
||||
@@ -32,10 +19,7 @@ internal sealed partial class BackgroundJobService
|
||||
item => item.TenantId == command.TenantId && item.JobType == normalizedJobType &&
|
||||
item.IdempotencyKey == idempotencyKey,
|
||||
cancellationToken);
|
||||
if (existing is not null)
|
||||
{
|
||||
return ToItem(existing);
|
||||
}
|
||||
if (existing is not null) return ToItem(existing);
|
||||
}
|
||||
|
||||
if (!command.IsSystemJob && !(await featureAccessService.EvaluateAsync(
|
||||
@@ -43,9 +27,7 @@ internal sealed partial class BackgroundJobService
|
||||
ResolveRequiredFeature(normalizedJobType, command.Payload),
|
||||
FeatureAccessOperation.Write,
|
||||
cancellationToken)).Allowed)
|
||||
{
|
||||
throw new InvalidOperationException("Tenant feature entitlement does not allow this background job.");
|
||||
}
|
||||
var quotaMetric = command.IsSystemJob ? null : ResolveQuotaMetric(normalizedJobType);
|
||||
var quotaConsumed = false;
|
||||
if (quotaMetric is not null)
|
||||
@@ -57,11 +39,11 @@ internal sealed partial class BackgroundJobService
|
||||
quotaConsumed = await featureAccessService.TryConsumeQuotaAsync(
|
||||
command.TenantId, quotaMetric, 1, cancellationToken);
|
||||
if (!quotaConsumed)
|
||||
{
|
||||
throw new FeatureAccessException("The background job quota has been exhausted.", "feature_quota_exhausted");
|
||||
}
|
||||
throw new FeatureAccessException("The background job quota has been exhausted.",
|
||||
"feature_quota_exhausted");
|
||||
}
|
||||
}
|
||||
|
||||
var job = new BackgroundJob
|
||||
{
|
||||
TenantId = command.TenantId,
|
||||
@@ -86,30 +68,30 @@ internal sealed partial class BackgroundJobService
|
||||
if (existing is not null)
|
||||
{
|
||||
if (quotaConsumed && quotaMetric is not null)
|
||||
{
|
||||
await featureAccessService.ReleaseQuotaAsync(command.TenantId, quotaMetric, 1, CancellationToken.None);
|
||||
}
|
||||
await featureAccessService.ReleaseQuotaAsync(command.TenantId, quotaMetric, 1,
|
||||
CancellationToken.None);
|
||||
return ToItem(existing);
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (quotaConsumed && quotaMetric is not null)
|
||||
{
|
||||
await featureAccessService.ReleaseQuotaAsync(command.TenantId, quotaMetric, 1, CancellationToken.None);
|
||||
}
|
||||
throw;
|
||||
}
|
||||
|
||||
return ToItem(job);
|
||||
}
|
||||
|
||||
private static string? ResolveQuotaMetric(string jobType) => jobType switch
|
||||
private static string? ResolveQuotaMetric(string jobType)
|
||||
{
|
||||
"content_import" => SaasQuotaMetricCatalog.ImportCount,
|
||||
"content_export" => SaasQuotaMetricCatalog.ExportCount,
|
||||
_ => null
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
return jobType switch
|
||||
{
|
||||
"content_import" => SaasQuotaMetricCatalog.ImportCount,
|
||||
"content_export" => SaasQuotaMetricCatalog.ExportCount,
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user