-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds remaining content item triggers #82
- Loading branch information
Showing
8 changed files
with
180 additions
and
78 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/Raytha.Application/Common/Shared/RaythaFunctionAsBackgroundTask.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using Raytha.Application.Common.Exceptions; | ||
using Raytha.Application.Common.Interfaces; | ||
using Raytha.Domain.Entities; | ||
using System.Text.Json; | ||
|
||
namespace Raytha.Application.Common.Shared; | ||
|
||
public class RaythaFunctionAsBackgroundTaskPayload | ||
{ | ||
public dynamic Target { get; init; } | ||
public RaythaFunction RaythaFunction { get; init; } | ||
} | ||
|
||
public class RaythaFunctionAsBackgroundTask : IExecuteBackgroundTask | ||
{ | ||
private readonly IRaythaDbContext _entityFrameworkDb; | ||
private readonly IRaythaFunctionConfiguration _raythaFunctionConfiguration; | ||
private readonly IRaythaFunctionScriptEngine _raythaFunctionScriptEngine; | ||
private readonly IRaythaFunctionSemaphore _raythaFunctionSemaphore; | ||
|
||
public RaythaFunctionAsBackgroundTask( | ||
IRaythaFunctionConfiguration raythaFunctionConfiguration, | ||
IRaythaFunctionSemaphore raythaFunctionSemaphore, | ||
IRaythaFunctionScriptEngine raythaFunctionScriptEngine, | ||
IRaythaDbContext entityFrameworkDb) | ||
{ | ||
_raythaFunctionConfiguration = raythaFunctionConfiguration; | ||
_raythaFunctionSemaphore = raythaFunctionSemaphore; | ||
_raythaFunctionScriptEngine = raythaFunctionScriptEngine; | ||
_entityFrameworkDb = entityFrameworkDb; | ||
} | ||
|
||
public async Task Execute(Guid jobId, JsonElement args, CancellationToken cancellationToken) | ||
{ | ||
string? raythaFunctionName = args.GetProperty("RaythaFunction").GetProperty("Name").GetString(); | ||
string? code = args.GetProperty("RaythaFunction").GetProperty("Code").GetString(); | ||
|
||
var job = _entityFrameworkDb.BackgroundTasks.First(p => p.Id == jobId); | ||
job.TaskStep = 1; | ||
job.StatusInfo = $"Running Raytha Function"; | ||
job.PercentComplete = 0; | ||
_entityFrameworkDb.BackgroundTasks.Update(job); | ||
await _entityFrameworkDb.SaveChangesAsync(cancellationToken); | ||
|
||
if (await _raythaFunctionSemaphore.WaitAsync(_raythaFunctionConfiguration.QueueTimeout, cancellationToken)) | ||
{ | ||
try | ||
{ | ||
string payload = JsonSerializer.Serialize(args.GetProperty("Target")); | ||
await _raythaFunctionScriptEngine.EvaluateRun(code, payload, _raythaFunctionConfiguration.ExecuteTimeout, cancellationToken); | ||
job.TaskStep = 2; | ||
job.StatusInfo = $"Completed Raytha Function: {raythaFunctionName}"; | ||
job.PercentComplete = 100; | ||
} | ||
catch (Exception exception) when(exception is RaythaFunctionExecuteTimeoutException or RaythaFunctionScriptException) | ||
{ | ||
job.StatusInfo = $"Error running Raytha Function {raythaFunctionName} - {exception.Message}"; | ||
} | ||
finally | ||
{ | ||
job.TaskStep = 2; | ||
job.PercentComplete = 100; | ||
_entityFrameworkDb.BackgroundTasks.Update(job); | ||
await _entityFrameworkDb.SaveChangesAsync(cancellationToken); | ||
_raythaFunctionSemaphore.Release(); | ||
} | ||
} | ||
else | ||
{ | ||
job.TaskStep = 2; | ||
job.StatusInfo = $"Raytha Function {raythaFunctionName} failed to run because too many background tasks are running"; | ||
job.PercentComplete = 100; | ||
_entityFrameworkDb.BackgroundTasks.Update(job); | ||
await _entityFrameworkDb.SaveChangesAsync(cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Raytha.Application/ContentItems/EventHandlers/ContentItemDeletedEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using MediatR; | ||
using Raytha.Application.Common.Interfaces; | ||
using Raytha.Application.Common.Shared; | ||
using Raytha.Domain.Events; | ||
using Raytha.Domain.ValueObjects; | ||
|
||
namespace Raytha.Application.ContentItems.EventHandlers; | ||
|
||
public class ContentItemDeletedEventHandler : INotificationHandler<ContentItemDeletedEvent> | ||
{ | ||
private readonly IBackgroundTaskQueue _taskQueue; | ||
private readonly IRaythaDbContext _db; | ||
|
||
public ContentItemDeletedEventHandler( | ||
IBackgroundTaskQueue taskQueue, | ||
IRaythaDbContext db) | ||
{ | ||
_taskQueue = taskQueue; | ||
_db = db; | ||
} | ||
|
||
public async Task Handle(ContentItemDeletedEvent notification, CancellationToken cancellationToken) | ||
{ | ||
var activeFunctions = _db.RaythaFunctions.Where(p => p.IsActive && p.TriggerType == RaythaFunctionTriggerType.ContentItemDeleted.DeveloperName); | ||
if (activeFunctions.Any()) | ||
{ | ||
foreach (var activeFunction in activeFunctions) | ||
{ | ||
await _taskQueue.EnqueueAsync<RaythaFunctionAsBackgroundTask>(new RaythaFunctionAsBackgroundTaskPayload | ||
{ | ||
Target = ContentItemDto.GetProjection(notification.ContentItem), | ||
RaythaFunction = activeFunction | ||
}, cancellationToken); | ||
} | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Raytha.Application/ContentItems/EventHandlers/ContentItemUpdatedEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using MediatR; | ||
using Raytha.Application.Common.Interfaces; | ||
using Raytha.Application.Common.Shared; | ||
using Raytha.Domain.Events; | ||
using Raytha.Domain.ValueObjects; | ||
|
||
namespace Raytha.Application.ContentItems.EventHandlers; | ||
|
||
public class ContentItemUpdatedEventHandler : INotificationHandler<ContentItemUpdatedEvent> | ||
{ | ||
private readonly IBackgroundTaskQueue _taskQueue; | ||
private readonly IRaythaDbContext _db; | ||
|
||
public ContentItemUpdatedEventHandler( | ||
IBackgroundTaskQueue taskQueue, | ||
IRaythaDbContext db) | ||
{ | ||
_taskQueue = taskQueue; | ||
_db = db; | ||
} | ||
|
||
public async Task Handle(ContentItemUpdatedEvent notification, CancellationToken cancellationToken) | ||
{ | ||
var activeFunctions = _db.RaythaFunctions.Where(p => p.IsActive && p.TriggerType == RaythaFunctionTriggerType.ContentItemUpdated.DeveloperName); | ||
if (activeFunctions.Any()) | ||
{ | ||
foreach (var activeFunction in activeFunctions) | ||
{ | ||
await _taskQueue.EnqueueAsync<RaythaFunctionAsBackgroundTask>(new RaythaFunctionAsBackgroundTaskPayload | ||
{ | ||
Target = ContentItemDto.GetProjection(notification.ContentItem), | ||
RaythaFunction = activeFunction | ||
}, cancellationToken); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters