62 lines
2.2 KiB
C#
62 lines
2.2 KiB
C#
using System.Text.Json;
|
||
using Microsoft.Extensions.Logging;
|
||
using Volo.Abp.BackgroundJobs;
|
||
using Volo.Abp.DependencyInjection;
|
||
using Yi.Framework.AiHub.Domain.Entities.Chat;
|
||
using Yi.Framework.AiHub.Domain.Managers;
|
||
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||
|
||
namespace Yi.Framework.AiHub.Application.Jobs;
|
||
|
||
/// <summary>
|
||
/// 图片生成后台任务
|
||
/// </summary>
|
||
public class ImageGenerationJob : AsyncBackgroundJob<ImageGenerationJobArgs>, ITransientDependency
|
||
{
|
||
private readonly ILogger<ImageGenerationJob> _logger;
|
||
private readonly AiGateWayManager _aiGateWayManager;
|
||
private readonly ISqlSugarRepository<ImageStoreTaskAggregateRoot> _imageStoreTaskRepository;
|
||
|
||
public ImageGenerationJob(
|
||
ILogger<ImageGenerationJob> logger,
|
||
AiGateWayManager aiGateWayManager,
|
||
ISqlSugarRepository<ImageStoreTaskAggregateRoot> imageStoreTaskRepository)
|
||
{
|
||
_logger = logger;
|
||
_aiGateWayManager = aiGateWayManager;
|
||
_imageStoreTaskRepository = imageStoreTaskRepository;
|
||
}
|
||
|
||
public override async Task ExecuteAsync(ImageGenerationJobArgs args)
|
||
{
|
||
_logger.LogInformation("开始执行图片生成任务,TaskId: {TaskId}, ModelId: {ModelId}, UserId: {UserId}",
|
||
args.TaskId, args.ModelId, args.UserId);
|
||
|
||
try
|
||
{
|
||
var request = JsonSerializer.Deserialize<JsonElement>(args.RequestJson);
|
||
|
||
await _aiGateWayManager.GeminiGenerateContentImageForStatisticsAsync(
|
||
args.TaskId,
|
||
args.ModelId,
|
||
request,
|
||
args.UserId);
|
||
|
||
_logger.LogInformation("图片生成任务完成,TaskId: {TaskId}", args.TaskId);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "图片生成任务失败,TaskId: {TaskId}, Error: {Error}", args.TaskId, ex.Message);
|
||
|
||
// 更新任务状态为失败
|
||
var task = await _imageStoreTaskRepository.GetFirstAsync(x => x.Id == args.TaskId);
|
||
if (task != null)
|
||
{
|
||
task.TaskStatus = TaskStatusEnum.Fail;
|
||
await _imageStoreTaskRepository.UpdateAsync(task);
|
||
}
|
||
}
|
||
}
|
||
}
|