Skip to content

Commit

Permalink
move install app to queue
Browse files Browse the repository at this point in the history
  • Loading branch information
nhathoang989 committed Oct 19, 2023
1 parent cbf0f2a commit 46ef8ac
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/modules/mix.portal/Controllers/MixApplicationController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Microsoft.AspNetCore.Mvc;
using FirebaseAdmin.Auth.Multitenancy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.Elfie.Diagnostics;
using Mix.Database.Entities.AuditLog;
using Mix.Lib.Interfaces;
using Mix.Portal.Domain.Interfaces;
using Mix.SignalR.Interfaces;

namespace Mix.Portal.Controllers
Expand Down Expand Up @@ -36,15 +37,14 @@ public MixApplicationController(

[HttpPost]
[Route("install")]
public async Task<ActionResult<MixApplicationViewModel>> Install([FromBody] MixApplicationViewModel app, CancellationToken cancellationToken = default)
public ActionResult Install([FromBody] MixApplicationViewModel app, CancellationToken cancellationToken = default)
{
if (_cmsContext.MixApplication.Any(m => m.MixTenantId == CurrentTenant.Id && m.BaseHref == app.BaseHref && m.Id != app.Id))
{
return BadRequest($"BaseHref: \"{app.BaseHref}\" existed");
}

await _applicationService.Install(app, cancellationToken);
return base.Ok(app);
QueueService.PushQueue(CurrentTenant.Id, MixQueueTopics.MixBackgroundTasks, MixQueueActions.InstallMixApplication, app);
return NoContent();
}

[HttpPost]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ private async Task ModifyFilesAndFolders(string deployUrl, string topFolder, str
MixTenantId = CurrentTenant.Id,
Scripts = string.Empty,
Styles = string.Empty,
CreatedBy = _mixIdentityService.GetClaim(HttpContextAccessor.HttpContext.User, MixClaims.Username)
};
template.Content = indexFile.Content.Replace("@", "@@")
.Replace("<body>", "<body><pre id=\"app-settings-container\" style=\"display:none\">@Model.AppSettings.ToString()</pre>");
Expand Down Expand Up @@ -298,11 +297,11 @@ public async Task AlertAsync<T>(IClientProxy clients, string action, int status,
var logMsg = new JObject()
{
new JProperty("created_at", DateTime.UtcNow),
new JProperty("id", HttpContextAccessor.HttpContext?.Request.HttpContext.Connection.Id.ToString()),
//new JProperty("id", HttpContextAccessor.HttpContext?.Request.HttpContext.Connection.Id.ToString()),
new JProperty("address", address),
new JProperty("ip_address", HttpContextAccessor.HttpContext.Request.HttpContext.Connection.RemoteIpAddress.ToString()),
new JProperty("user", _mixIdentityService.GetClaim(HttpContextAccessor.HttpContext.User, MixClaims.Username)),
new JProperty("request_url", HttpContextAccessor.HttpContext.Request.Path.Value),
//new JProperty("ip_address", HttpContextAccessor.HttpContext.Request.HttpContext.Connection.RemoteIpAddress.ToString()),
//new JProperty("user", _mixIdentityService.GetClaim(HttpContextAccessor.HttpContext.User, MixClaims.Username)),
//new JProperty("request_url", HttpContextAccessor.HttpContext.Request.Path.Value),
new JProperty("action", action),
new JProperty("status", status),
new JProperty("message", message)
Expand Down
1 change: 1 addition & 0 deletions src/platform/mix.constant/Constants/MixQueueActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public class MixQueueActions
{
public const string InstallMixApplication = "InstallMixApplication";
public const string AuditLog = "AuditLog";
public const string EnqueueLog = "QueueLog";
public const string AckLog = "AckLog";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Mix.Portal.Domain.Dtos
namespace Mix.Lib.Dtos
{
public sealed class RestoreMixApplicationPackageDto
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.SignalR;
using Mix.Lib.Dtos;

namespace Mix.Portal.Domain.Interfaces
namespace Mix.Lib.Interfaces
{
public interface IMixApplicationService
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Mix.Communicator.Models;
using Mix.Communicator.Services;
using Mix.Database.Entities.Account;
using Mix.Lib.Interfaces;
using Mix.Mixdb.Event.Services;
using Mix.Queue.Engines;
using Mix.Queue.Engines.MixQueue;
Expand All @@ -23,7 +24,8 @@ public class MixBackgroundTaskSubscriber : SubscriberBase
private static string[] allowActions =
{
MixQueueActions.SendMail,
MixQueueActions.MixDbEvent
MixQueueActions.MixDbEvent,
MixQueueActions.InstallMixApplication
};
public MixBackgroundTaskSubscriber(
IServiceProvider serviceProvider,
Expand Down Expand Up @@ -59,6 +61,10 @@ public override async Task Handler(MessageQueueModel model)

switch (model.Action)
{
case MixQueueActions.InstallMixApplication:
await InstallMixApplication(model);
break;

case MixQueueActions.SendMail:
await SendMail(model);
break;
Expand All @@ -73,6 +79,18 @@ public override async Task Handler(MessageQueueModel model)
}
}

private async Task InstallMixApplication(MessageQueueModel model)
{
using (ServiceScope = ServicesProvider.CreateScope())
{
var cmsUow = GetRequiredService<UnitOfWorkInfo<MixCmsContext>>();
IMixApplicationService _applicationService = GetRequiredService<IMixApplicationService>();
var app = model.ParseData<MixApplicationViewModel>();
await _applicationService.Install(app);
await cmsUow.CompleteAsync();
}
}

public override Task HandleException(MessageQueueModel model, Exception ex)
{
return MixLogService.LogExceptionAsync(ex);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Mix.Portal.Domain.ViewModels
namespace Mix.Lib.ViewModels
{
public sealed class MixApplicationViewModel
: TenantDataViewModelBase<MixCmsContext, MixApplication, int, MixApplicationViewModel>
Expand Down

0 comments on commit 46ef8ac

Please sign in to comment.