JobExecution
This commit is contained in:
@@ -17,6 +17,8 @@ namespace Musify.Application.Contracts.Infrastructure
|
|||||||
|
|
||||||
DbSet<Job> Jobs { get; }
|
DbSet<Job> Jobs { get; }
|
||||||
|
|
||||||
|
DbSet<JobExecution> JobExecutions { get; }
|
||||||
|
|
||||||
Task<int> SaveChangesAsync(CancellationToken cancellationToken);
|
Task<int> SaveChangesAsync(CancellationToken cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,5 +2,6 @@
|
|||||||
{
|
{
|
||||||
public record RemoveFileEvent(
|
public record RemoveFileEvent(
|
||||||
string BucketName,
|
string BucketName,
|
||||||
string KeyName);
|
string KeyName,
|
||||||
|
Guid JobId);
|
||||||
}
|
}
|
||||||
@@ -3,5 +3,6 @@
|
|||||||
public record ResizePictureEvent(
|
public record ResizePictureEvent(
|
||||||
string BucketName,
|
string BucketName,
|
||||||
string KeyName,
|
string KeyName,
|
||||||
ResizePictureItems[] Items);
|
ResizePictureItems[] Items,
|
||||||
|
Guid JobId);
|
||||||
}
|
}
|
||||||
@@ -77,7 +77,8 @@ namespace Musify.Application.PlayLists.Commands.CreatePlayList
|
|||||||
playListConfiguration.PicturesSizes.LargePictureWidth,
|
playListConfiguration.PicturesSizes.LargePictureWidth,
|
||||||
playListConfiguration.PicturesSizes.LargePictureHeight,
|
playListConfiguration.PicturesSizes.LargePictureHeight,
|
||||||
playListConfiguration.Routes.LargePictures)
|
playListConfiguration.Routes.LargePictures)
|
||||||
] ), cancellationToken);
|
],
|
||||||
|
Guid.NewGuid()), cancellationToken);
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -37,7 +37,8 @@ namespace Musify.Application.PlayLists.Commands.DeletePlayList
|
|||||||
{
|
{
|
||||||
await publishEndpoint.Publish(new RemoveFileEvent(
|
await publishEndpoint.Publish(new RemoveFileEvent(
|
||||||
storageConfiguration.BucketName,
|
storageConfiguration.BucketName,
|
||||||
originalImageStorageKey), cancellationToken);
|
originalImageStorageKey,
|
||||||
|
Guid.NewGuid()), cancellationToken);
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
@@ -51,7 +52,8 @@ namespace Musify.Application.PlayLists.Commands.DeletePlayList
|
|||||||
{
|
{
|
||||||
await publishEndpoint.Publish(new RemoveFileEvent(
|
await publishEndpoint.Publish(new RemoveFileEvent(
|
||||||
storageConfiguration.BucketName,
|
storageConfiguration.BucketName,
|
||||||
playList.SmallPictureKeyName), cancellationToken);
|
playList.SmallPictureKeyName,
|
||||||
|
Guid.NewGuid()), cancellationToken);
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
@@ -66,7 +68,8 @@ namespace Musify.Application.PlayLists.Commands.DeletePlayList
|
|||||||
{
|
{
|
||||||
await publishEndpoint.Publish(new RemoveFileEvent(
|
await publishEndpoint.Publish(new RemoveFileEvent(
|
||||||
storageConfiguration.BucketName,
|
storageConfiguration.BucketName,
|
||||||
playList.MediumPictureKeyName), cancellationToken);
|
playList.MediumPictureKeyName,
|
||||||
|
Guid.NewGuid()), cancellationToken);
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
@@ -81,7 +84,8 @@ namespace Musify.Application.PlayLists.Commands.DeletePlayList
|
|||||||
{
|
{
|
||||||
await publishEndpoint.Publish(new RemoveFileEvent(
|
await publishEndpoint.Publish(new RemoveFileEvent(
|
||||||
storageConfiguration.BucketName,
|
storageConfiguration.BucketName,
|
||||||
playList.LargePictureKeyName), cancellationToken);
|
playList.LargePictureKeyName,
|
||||||
|
Guid.NewGuid()), cancellationToken);
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -55,7 +55,8 @@ namespace Musify.Application.PlayLists.Commands.UpdatePlayList
|
|||||||
{
|
{
|
||||||
await publishEndpoint.Publish(new RemoveFileEvent(
|
await publishEndpoint.Publish(new RemoveFileEvent(
|
||||||
storageConfiguration.BucketName,
|
storageConfiguration.BucketName,
|
||||||
originalImageStorageKey), cancellationToken);
|
originalImageStorageKey,
|
||||||
|
Guid.NewGuid()), cancellationToken);
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
@@ -72,7 +73,8 @@ namespace Musify.Application.PlayLists.Commands.UpdatePlayList
|
|||||||
{
|
{
|
||||||
await publishEndpoint.Publish(new RemoveFileEvent(
|
await publishEndpoint.Publish(new RemoveFileEvent(
|
||||||
storageConfiguration.BucketName,
|
storageConfiguration.BucketName,
|
||||||
playList.SmallPictureKeyName), cancellationToken);
|
playList.SmallPictureKeyName,
|
||||||
|
Guid.NewGuid()), cancellationToken);
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
@@ -87,7 +89,8 @@ namespace Musify.Application.PlayLists.Commands.UpdatePlayList
|
|||||||
{
|
{
|
||||||
await publishEndpoint.Publish(new RemoveFileEvent(
|
await publishEndpoint.Publish(new RemoveFileEvent(
|
||||||
storageConfiguration.BucketName,
|
storageConfiguration.BucketName,
|
||||||
playList.MediumPictureKeyName), cancellationToken);
|
playList.MediumPictureKeyName,
|
||||||
|
Guid.NewGuid()), cancellationToken);
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
@@ -102,7 +105,8 @@ namespace Musify.Application.PlayLists.Commands.UpdatePlayList
|
|||||||
{
|
{
|
||||||
await publishEndpoint.Publish(new RemoveFileEvent(
|
await publishEndpoint.Publish(new RemoveFileEvent(
|
||||||
storageConfiguration.BucketName,
|
storageConfiguration.BucketName,
|
||||||
playList.LargePictureKeyName), cancellationToken);
|
playList.LargePictureKeyName,
|
||||||
|
Guid.NewGuid()), cancellationToken);
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
@@ -150,7 +154,8 @@ namespace Musify.Application.PlayLists.Commands.UpdatePlayList
|
|||||||
playListConfiguration.PicturesSizes.LargePictureWidth,
|
playListConfiguration.PicturesSizes.LargePictureWidth,
|
||||||
playListConfiguration.PicturesSizes.LargePictureHeight,
|
playListConfiguration.PicturesSizes.LargePictureHeight,
|
||||||
playListConfiguration.Routes.LargePictures)
|
playListConfiguration.Routes.LargePictures)
|
||||||
]
|
],
|
||||||
|
Guid.NewGuid()
|
||||||
), cancellationToken);
|
), cancellationToken);
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
|
|||||||
@@ -12,27 +12,10 @@ namespace Musify.Domain.Entities
|
|||||||
[Required]
|
[Required]
|
||||||
public required JobType JobType { get; set; }
|
public required JobType JobType { get; set; }
|
||||||
|
|
||||||
[Required]
|
|
||||||
public JobState JobState { get; set; } = JobState.Pending;
|
|
||||||
|
|
||||||
[NotMapped]
|
|
||||||
public bool IsCompleted => JobState == JobState.Completed;
|
|
||||||
|
|
||||||
[NotMapped]
|
|
||||||
public bool IsFailed => JobState == JobState.Failed;
|
|
||||||
|
|
||||||
[NotMapped]
|
|
||||||
public bool IsPending => JobState == JobState.Pending;
|
|
||||||
|
|
||||||
[Required]
|
[Required]
|
||||||
public required string Payload { get; set; }
|
public required string Payload { get; set; }
|
||||||
|
|
||||||
[Required]
|
[Required]
|
||||||
public DateTime CreateAt { get; set; } = DateTime.UtcNow;
|
public DateTime CreateAt { get; set; } = DateTime.UtcNow;
|
||||||
|
|
||||||
[Required]
|
|
||||||
public int RetryCount { get; set; } = 0;
|
|
||||||
|
|
||||||
public DateTime CompletedAt { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
|
||||||
|
namespace Musify.Domain.Entities
|
||||||
|
{
|
||||||
|
#pragma warning disable CS8618
|
||||||
|
[Table("JobExecution")]
|
||||||
|
public class JobExecution
|
||||||
|
{
|
||||||
|
[Key]
|
||||||
|
public Guid Id { get; set; } = Guid.NewGuid();
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public required Guid JobId { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public JobState JobState { get; set; } = JobState.Pending;
|
||||||
|
|
||||||
|
[NotMapped]
|
||||||
|
public bool IsCompleted => JobState == JobState.Completed;
|
||||||
|
|
||||||
|
[NotMapped]
|
||||||
|
public bool IsFailed => JobState == JobState.Failed;
|
||||||
|
|
||||||
|
[NotMapped]
|
||||||
|
public bool IsPending => JobState == JobState.Pending;
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public DateTime FinishedAt { get; set; }
|
||||||
|
|
||||||
|
[ForeignKey(nameof(JobId)) ]
|
||||||
|
public Job Job { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,7 +18,7 @@ namespace Musify.Infrastructure.Messaging.Consumers
|
|||||||
var payload = JsonSerializer.Serialize(consumeContext.Message);
|
var payload = JsonSerializer.Serialize(consumeContext.Message);
|
||||||
|
|
||||||
var job = await database.Jobs
|
var job = await database.Jobs
|
||||||
.SingleOrDefaultAsync(j => j.JobState == JobState.Failed && j.Payload == payload);
|
.SingleOrDefaultAsync(j => j.Id == consumeContext.Message.JobId);
|
||||||
|
|
||||||
if (job is null)
|
if (job is null)
|
||||||
{
|
{
|
||||||
@@ -30,13 +30,12 @@ namespace Musify.Infrastructure.Messaging.Consumers
|
|||||||
await database.Jobs.AddAsync(job, consumeContext.CancellationToken);
|
await database.Jobs.AddAsync(job, consumeContext.CancellationToken);
|
||||||
logger.LogInformation("Created new job with id {JobId} for removing file {BucketName}/{KeyName}", job.Id, consumeContext.Message.BucketName, consumeContext.Message.KeyName);
|
logger.LogInformation("Created new job with id {JobId} for removing file {BucketName}/{KeyName}", job.Id, consumeContext.Message.BucketName, consumeContext.Message.KeyName);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
var jobExecution = new JobExecution
|
||||||
{
|
{
|
||||||
job.RetryCount++;
|
JobId = job.Id
|
||||||
job.JobState = JobState.Pending;
|
};
|
||||||
database.Jobs.Update(job);
|
await database.JobExecutions.AddAsync(jobExecution, consumeContext.CancellationToken);
|
||||||
logger.LogInformation("Retrying job with id {JobId} for removing file {BucketName}/{KeyName}. Retry count: {RetryCount}", job.Id, consumeContext.Message.BucketName, consumeContext.Message.KeyName, job.RetryCount);
|
|
||||||
}
|
|
||||||
|
|
||||||
var removeFile = await storageHandler.RemoveFileAsync(
|
var removeFile = await storageHandler.RemoveFileAsync(
|
||||||
consumeContext.Message.BucketName,
|
consumeContext.Message.BucketName,
|
||||||
@@ -45,15 +44,17 @@ namespace Musify.Infrastructure.Messaging.Consumers
|
|||||||
|
|
||||||
if (!removeFile.IsSuccess)
|
if (!removeFile.IsSuccess)
|
||||||
{
|
{
|
||||||
job.JobState = JobState.Failed;
|
jobExecution.JobState = JobState.Failed;
|
||||||
logger.LogInformation("Failed to remove file {BucketName}/{KeyName} for job with id {JobId}. Error: {ErrorMessage}", consumeContext.Message.BucketName, consumeContext.Message.KeyName, job.Id, string.Join("; ", removeFile.Errors));
|
logger.LogInformation("Failed to remove file {BucketName}/{KeyName} for jobId={JobId}, JobExecutionId={jobExecutionId}. Error: {ErrorMessage}",
|
||||||
|
consumeContext.Message.BucketName, consumeContext.Message.KeyName, job.Id, jobExecution.Id, string.Join("; ", removeFile.Errors));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
job.CompletedAt = DateTime.UtcNow;
|
logger.LogInformation("Successfully removed file {BucketName}/{KeyName} for jobId={JobId}, JobExecutionId={jobExecutionId}.",
|
||||||
logger.LogInformation("Successfully removed file {BucketName}/{KeyName} for job with id {JobId}.", consumeContext.Message.BucketName, consumeContext.Message.KeyName, job.Id);
|
consumeContext.Message.BucketName, consumeContext.Message.KeyName, job.Id, jobExecution.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
jobExecution.FinishedAt = DateTime.UtcNow;
|
||||||
await database.SaveChangesAsync(consumeContext.CancellationToken);
|
await database.SaveChangesAsync(consumeContext.CancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,14 +18,14 @@ namespace Musify.Infrastructure.Messaging.Consumers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Dictionary<ResizePictureItems, Job> jobs = new Dictionary<ResizePictureItems, Job>();
|
Dictionary<ResizePictureItems, JobExecution> jobsExecutions = new Dictionary<ResizePictureItems, JobExecution>();
|
||||||
|
|
||||||
foreach (var item in consumeContext.Message.Items)
|
foreach (var item in consumeContext.Message.Items)
|
||||||
{
|
{
|
||||||
var payloadJson = JsonSerializer.Serialize(item);
|
var payloadJson = JsonSerializer.Serialize(item);
|
||||||
|
|
||||||
var job = await database.Jobs
|
var job = await database.Jobs
|
||||||
.SingleOrDefaultAsync(job => job.JobType == JobType.ResizePicture && job.Payload == payloadJson, consumeContext.CancellationToken);
|
.SingleOrDefaultAsync(job => job.Id == consumeContext.Message.JobId);
|
||||||
|
|
||||||
if (job is null)
|
if (job is null)
|
||||||
{
|
{
|
||||||
@@ -36,14 +36,14 @@ namespace Musify.Infrastructure.Messaging.Consumers
|
|||||||
};
|
};
|
||||||
await database.Jobs.AddAsync(job, consumeContext.CancellationToken);
|
await database.Jobs.AddAsync(job, consumeContext.CancellationToken);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
job.RetryCount++;
|
|
||||||
job.JobState = JobState.InProgress;
|
|
||||||
database.Jobs.Update(job);
|
|
||||||
}
|
|
||||||
|
|
||||||
jobs.Add(item, job);
|
var jobExecution = new JobExecution
|
||||||
|
{
|
||||||
|
JobId = consumeContext.Message.JobId
|
||||||
|
};
|
||||||
|
await database.JobExecutions.AddAsync(jobExecution, consumeContext.CancellationToken);
|
||||||
|
|
||||||
|
jobsExecutions.Add(item, jobExecution);
|
||||||
}
|
}
|
||||||
|
|
||||||
var fileResult = await storageHandler.GetFileAsync(
|
var fileResult = await storageHandler.GetFileAsync(
|
||||||
@@ -56,7 +56,7 @@ namespace Musify.Infrastructure.Messaging.Consumers
|
|||||||
logger.LogWarning("File not found in storage: BucketName={BucketName}, KeyName={KeyName}",
|
logger.LogWarning("File not found in storage: BucketName={BucketName}, KeyName={KeyName}",
|
||||||
consumeContext.Message.BucketName, consumeContext.Message.KeyName);
|
consumeContext.Message.BucketName, consumeContext.Message.KeyName);
|
||||||
|
|
||||||
foreach (var job in jobs)
|
foreach (var job in jobsExecutions)
|
||||||
{
|
{
|
||||||
job.Value.JobState = JobState.Failed;
|
job.Value.JobState = JobState.Failed;
|
||||||
}
|
}
|
||||||
@@ -70,12 +70,12 @@ namespace Musify.Infrastructure.Messaging.Consumers
|
|||||||
|
|
||||||
foreach (var item in consumeContext.Message.Items)
|
foreach (var item in consumeContext.Message.Items)
|
||||||
{
|
{
|
||||||
var job = jobs[item];
|
var jobExecution = jobsExecutions[item];
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await ResizePicture(
|
await ResizePicture(
|
||||||
job,
|
jobExecution,
|
||||||
consumeContext.Message.BucketName,
|
consumeContext.Message.BucketName,
|
||||||
memoryStream,
|
memoryStream,
|
||||||
item,
|
item,
|
||||||
@@ -83,7 +83,7 @@ namespace Musify.Infrastructure.Messaging.Consumers
|
|||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
logger.LogError(exception, "An error occurred while resizing picture for UploadId={UploadId} and Resize={Resize}", job.Id, item);
|
logger.LogError(exception, "An error occurred while resizing picture for UploadId={UploadId} and Resize={Resize}", jobExecution.Id, item);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -100,10 +100,10 @@ namespace Musify.Infrastructure.Messaging.Consumers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async Task ResizePicture(Job job, string bucketName, MemoryStream memoryStream, ResizePictureItems items, CancellationToken cancellationToken)
|
async Task ResizePicture(JobExecution jobExecution, string bucketName, MemoryStream memoryStream, ResizePictureItems items, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
memoryStream.Position = 0;
|
memoryStream.Position = 0;
|
||||||
job.JobState = JobState.InProgress;
|
jobExecution.JobState = JobState.InProgress;
|
||||||
|
|
||||||
var pictureResult = await pictureHandler.ResizePictureAsync(
|
var pictureResult = await pictureHandler.ResizePictureAsync(
|
||||||
memoryStream,
|
memoryStream,
|
||||||
@@ -114,7 +114,7 @@ namespace Musify.Infrastructure.Messaging.Consumers
|
|||||||
if (!pictureResult.IsSuccess)
|
if (!pictureResult.IsSuccess)
|
||||||
{
|
{
|
||||||
logger.LogError("Failed to resize picture: {ErrorMessage}", string.Join("; ", pictureResult.Errors));
|
logger.LogError("Failed to resize picture: {ErrorMessage}", string.Join("; ", pictureResult.Errors));
|
||||||
job.JobState = JobState.Failed;
|
jobExecution.JobState = JobState.Failed;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,11 +128,12 @@ namespace Musify.Infrastructure.Messaging.Consumers
|
|||||||
if (!saveResult.IsSuccess)
|
if (!saveResult.IsSuccess)
|
||||||
{
|
{
|
||||||
logger.LogError("Failed to upload resized picture: {ErrorMessage}", string.Join("; ", saveResult.Errors));
|
logger.LogError("Failed to upload resized picture: {ErrorMessage}", string.Join("; ", saveResult.Errors));
|
||||||
job.JobState = JobState.Failed;
|
jobExecution.JobState = JobState.Failed;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
job.JobState = JobState.Completed;
|
jobExecution.JobState = JobState.Completed;
|
||||||
|
jobExecution.FinishedAt = DateTime.UtcNow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -24,5 +24,7 @@ namespace Musify.Infrastructure.Persistence
|
|||||||
public DbSet<Upload> Uploads => Set<Upload>();
|
public DbSet<Upload> Uploads => Set<Upload>();
|
||||||
|
|
||||||
public DbSet<Job> Jobs => Set<Job>();
|
public DbSet<Job> Jobs => Set<Job>();
|
||||||
|
|
||||||
|
public DbSet<JobExecution> JobExecutions => Set<JobExecution>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user