Skip to content

Commit

Permalink
Log response
Browse files Browse the repository at this point in the history
  • Loading branch information
SeriaWei committed Mar 19, 2023
1 parent 017e6f7 commit e95aefe
Show file tree
Hide file tree
Showing 18 changed files with 51 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Database/MySql/Dump.sql
Expand Up @@ -1494,7 +1494,7 @@ INSERT INTO `EA_EventAction` VALUES
DROP TABLE IF EXISTS `EA_PendingTask`;
CREATE TABLE `EA_PendingTask` (
`ID` INT AUTO_INCREMENT NOT NULL,
`Identifier` VARCHAR(100) CHARACTER SET utf8mb4 NOT NULL,
`Identifier` VARCHAR(200) CHARACTER SET utf8mb4 NOT NULL,
`HandlerName` VARCHAR(100) CHARACTER SET utf8mb4 NOT NULL,
`Data` LONGTEXT CHARACTER SET utf8mb4 NULL,
`LogMessage` LONGTEXT CHARACTER SET utf8mb4 NULL,
Expand Down
2 changes: 1 addition & 1 deletion Database/SQLite/ZKEACMS.sqlite.sql
Expand Up @@ -503,7 +503,7 @@ CREATE TABLE [ExtendField] (
);
CREATE TABLE [EA_PendingTask] (
[ID] INTEGER NOT NULL
, [Identifier] nvarchar(100) NOT NULL
, [Identifier] nvarchar(200) NOT NULL
, [HandlerName] nvarchar(100) NOT NULL
, [Data] ntext NULL
, [LogMessage] ntext NULL
Expand Down
2 changes: 1 addition & 1 deletion Database/Update/3.7/MsSql.sql
Expand Up @@ -52,7 +52,7 @@ SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[EA_PendingTask](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Identifier] [nvarchar](100) NOT NULL,
[Identifier] [nvarchar](200) NOT NULL,
[HandlerName] [nvarchar](100) NOT NULL,
[Data] [nvarchar](max) NULL,
[LogMessage] [nvarchar](max) NULL,
Expand Down
2 changes: 1 addition & 1 deletion Database/Update/3.7/MySql.sql
Expand Up @@ -151,7 +151,7 @@ INSERT INTO `EA_EventAction` VALUES
DROP TABLE IF EXISTS `EA_PendingTask`;
CREATE TABLE `EA_PendingTask` (
`ID` INT AUTO_INCREMENT NOT NULL,
`Identifier` VARCHAR(100) CHARACTER SET utf8mb4 NOT NULL,
`Identifier` VARCHAR(200) CHARACTER SET utf8mb4 NOT NULL,
`HandlerName` VARCHAR(100) CHARACTER SET utf8mb4 NOT NULL,
`Data` LONGTEXT CHARACTER SET utf8mb4 NULL,
`LogMessage` LONGTEXT CHARACTER SET utf8mb4 NULL,
Expand Down
2 changes: 1 addition & 1 deletion Database/Update/3.7/Sqlite.sql
@@ -1,6 +1,6 @@
CREATE TABLE [EA_PendingTask] (
[ID] INTEGER NOT NULL
, [Identifier] nvarchar(100) NOT NULL
, [Identifier] nvarchar(200) NOT NULL
, [HandlerName] nvarchar(100) NOT NULL
, [Data] ntext NULL
, [LogMessage] ntext NULL
Expand Down
Binary file modified Database/Update/3.7/package.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion Database/script.sql
Expand Up @@ -700,7 +700,7 @@ SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[EA_PendingTask](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Identifier] [nvarchar](100) NOT NULL,
[Identifier] [nvarchar](200) NOT NULL,
[HandlerName] [nvarchar](100) NOT NULL,
[Data] [nvarchar](max) NULL,
[LogMessage] [nvarchar](max) NULL,
Expand Down
Expand Up @@ -13,20 +13,23 @@
using ZKEACMS.Event;
using ZKEACMS.EventAction.Service;
using Easy.RuleEngine;
using Microsoft.Extensions.Logging;

namespace ZKEACMS.EventAction.ActionExecutor
{
public sealed class EventHandler : IEventHandler
public sealed class EventActionEventHandler : IEventHandler
{
private readonly IEventActionService _eventActionService;
private readonly IExecutorManager _executorManager;
private readonly IRuleManager _ruleManager;
private readonly ILogger<EventActionEventHandler> _logger;

public EventHandler(IEventActionService eventActionService, IExecutorManager executorManager, IRuleManager ruleManager)
public EventActionEventHandler(IEventActionService eventActionService,
IExecutorManager executorManager,
ILogger<EventActionEventHandler> logger)
{
_eventActionService = eventActionService;
_executorManager = executorManager;
_ruleManager = ruleManager;
_logger = logger;
}

public void Handle(object entity, EventArg e)
Expand All @@ -45,7 +48,14 @@ public void Handle(object entity, EventArg e)
var executor = _executorManager.CreateExecutor(parsedAction.Uses);
if (executor == null) continue;

executor.Execute(new Arguments(parsedAction.GetRendedWith(entity)), entity, e);
try
{
executor.Execute(new Arguments(parsedAction.GetRendedWith(entity)), entity, e);
}
catch(Exception ex)
{
_logger.LogError(ex, ex.Message);
}
}
}
}
Expand Down
Expand Up @@ -44,7 +44,7 @@ public ServiceResult Execute(Arguments args, object model, EventArg e)
}
private void PushRequestInQueue(HttpRequestContent httpRequest)
{
_pendingTaskService.Add(httpRequest.Url, HttpRequesetTaskHandler.Name, httpRequest);
_pendingTaskService.Add(httpRequest.Method + " " + httpRequest.Url.Split('?')[0], HttpRequesetTaskHandler.Name, httpRequest);
}
}
}
2 changes: 1 addition & 1 deletion src/ZKEACMS.EventAction/EventActionPlug.cs
Expand Up @@ -101,7 +101,7 @@ public override void ConfigureServices(IServiceCollection serviceCollection)
serviceCollection.AddTransient<IActionBodyService, ActionBodyService>();
serviceCollection.ConfigureMetaData<Models.ActionBody, Models.ActionBodyMetaData>();

serviceCollection.RegistEvent<ActionExecutor.EventHandler>(Event.Events.All);
serviceCollection.RegistEvent<ActionExecutor.EventActionEventHandler>(Event.Events.All);
serviceCollection.RegistPendingTask<HttpRequesetTaskHandler>(HttpRequesetTaskHandler.Name);

serviceCollection.AddScoped<IExecutorManager, ExecutorManager>();
Expand Down
11 changes: 6 additions & 5 deletions src/ZKEACMS.EventAction/HttpParser/HttpRequesetTaskHandler.cs
Expand Up @@ -2,6 +2,7 @@
* Copyright (c) ZKEASOFT. All rights reserved.
* http://www.zkea.net/licenses */

using Easy.RepositoryPattern;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
Expand All @@ -23,19 +24,19 @@ public HttpRequesetTaskHandler(IHttpClientFactory httpClientFactory)
_httpClientFactory = httpClientFactory;
}

public override async Task ExecuteAsync(object context)
public override Task<ServiceResult<string>> ExecuteAsync(object context)
{
await SendAsync(context as HttpRequestContent);
return SendAsync(context as HttpRequestContent);
}

public async Task SendAsync(HttpRequestContent httpRequest)
public async Task<ServiceResult<string>> SendAsync(HttpRequestContent httpRequest)
{
using (var response = await _httpClientFactory.CreateClient().SendAsync(httpRequest.ConvertToHttpRequestMessage()))
{
string responseContent = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode) return;
if (!response.IsSuccessStatusCode) throw new Exception($"Send http request with error.\r\nPayload:\r\n{httpRequest}\r\nResponse:\r\n{responseContent}");

throw new Exception($"Send http request with error.\r\nPayload:\r\n{httpRequest}\r\nResponse:\r\n{responseContent}");
return new ServiceResult<string>(responseContent);
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/ZKEACMS.EventAction/Service/PendingTaskService.cs
Expand Up @@ -47,8 +47,13 @@ public void Add(string identifier, string handlerName, object context)
HandlerName = handlerName,
Data = JsonConverter.Serialize(context),
Status = (int)PendingTaskStatus.Pending,
CreateDate = DateTime.Now
CreateDate = DateTime.Now,
LastUpdateDate = DateTime.Now
};
if (newTask.Identifier.Length > 200)
{
newTask.Identifier = newTask.Identifier.Substring(0, 200);
}
_dbContextProvider.Current.Set<PendingTaskEntity>().Add(newTask);
_dbContextProvider.Current.SaveChanges();
}
Expand Down
4 changes: 3 additions & 1 deletion src/ZKEACMS.EventAction/Views/PendingTask/Index.cshtml
Expand Up @@ -13,7 +13,9 @@
@Html.SearchTerms(Authorizer.Authorize(ZKEACMS.EventAction.PermissionKeys.ManagePendingTask))
<div class="panel panel-default">
<div class="panel-body">
<grid delete-able="Authorizer.Authorize(ZKEACMS.EventAction.PermissionKeys.ManagePendingTask)" edit-able="Authorizer.Authorize(ZKEACMS.EventAction.PermissionKeys.ManagePendingTask)"></grid>
<grid delete-able="Authorizer.Authorize(ZKEACMS.EventAction.PermissionKeys.ManagePendingTask)"
edit-able="Authorizer.Authorize(ZKEACMS.EventAction.PermissionKeys.ManagePendingTask)"
order-desc="CreateDate"></grid>
</div>
</div>
</div>
6 changes: 4 additions & 2 deletions src/ZKEACMS.Mail/EmailPendingTaskHandler.cs
@@ -1,4 +1,5 @@
using Easy.Notification;
using Easy.RepositoryPattern;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -18,9 +19,10 @@ public EmailPendingTaskHandler(IEmailNotification emailNotification)
_emailNotification = emailNotification;
}

public override Task ExecuteAsync(object context)
public override async Task<ServiceResult<string>> ExecuteAsync(object context)
{
return _emailNotification.SendEmailAsync(context as EmailContext);
await _emailNotification.SendEmailAsync(context as EmailContext);
return new ServiceResult<string>();
}
}
}
2 changes: 1 addition & 1 deletion src/ZKEACMS.Mail/Queue/PersistentEmailQueue.cs
Expand Up @@ -28,7 +28,7 @@ public Task<EmailContext> Receive(CancellationToken cancellationToken = default)

public Task Send(EmailContext emailMessage)
{
_pendingTaskService.Add(string.Join(";", emailMessage.EmailMessage.To), EmailPendingTaskHandler.Name, emailMessage);
_pendingTaskService.Add("mailto: " + string.Join(";", emailMessage.EmailMessage.To), EmailPendingTaskHandler.Name, emailMessage);
return Task.CompletedTask;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/ZKEACMS/PendingTask/IPendingTaskHandler.cs
Expand Up @@ -2,6 +2,7 @@
* Copyright (c) ZKEASOFT. All rights reserved.
* http://www.zkea.net/licenses */

using Easy.RepositoryPattern;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -12,7 +13,7 @@ namespace ZKEACMS.PendingTask
{
public interface IPendingTaskHandler<out T>
{
Task ExecuteAsync(object context);
Task<ServiceResult<string>> ExecuteAsync(object context);
T Deserialize(string rowData);
}
}
5 changes: 4 additions & 1 deletion src/ZKEACMS/PendingTask/PendingTaskExecutor.cs
Expand Up @@ -45,7 +45,10 @@ public async Task ProcessAllPendingTaskAsync()
try
{
var context = taskHandler.Deserialize(task.Data);
await taskHandler.ExecuteAsync(context);
var result = await taskHandler.ExecuteAsync(context);
if (result.HasViolation) throw new Exception(result.ErrorMessage);

task.LogMessage = result.Result;
_pendingTaskService.Complete(task);
}
catch (Exception ex)
Expand Down
3 changes: 2 additions & 1 deletion src/ZKEACMS/PendingTask/PendingTaskHandler.cs
Expand Up @@ -2,6 +2,7 @@
* Copyright (c) ZKEASOFT. All rights reserved.
* http://www.zkea.net/licenses */

using Easy.RepositoryPattern;
using Easy.Serializer;
using System;
using System.Collections.Generic;
Expand All @@ -18,6 +19,6 @@ public T Deserialize(string rowData)
return JsonConverter.Deserialize<T>(rowData);
}

public abstract Task ExecuteAsync(object context);
public abstract Task<ServiceResult<string>> ExecuteAsync(object context);
}
}

0 comments on commit e95aefe

Please sign in to comment.