Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug 69620 #470

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 22 additions & 29 deletions products/ASC.Files/Core/Helpers/DocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,10 @@

namespace ASC.Files.Core.Helpers;

/// <summary>
/// Class service connector
/// </summary>
public static class DocumentService
{
/// <summary>
/// Timeout to request conversion
/// </summary>
public static readonly int Timeout = 120000;
//public static int Timeout = Convert.ToInt32(ConfigurationManagerExtension.AppSettings["files.docservice.timeout"] ?? "120000");

/// <summary>
/// Number of tries request conversion
/// </summary>
public static readonly int MaxTry = 3;
private const int Timeout = 120000;


private static readonly JsonSerializerOptions _bodySettings = new()
{
Expand Down Expand Up @@ -246,8 +235,7 @@ public static async Task<CommandResponse> CommandRequestAsync(FileUtility fileUt
Method = HttpMethod.Post
};

var httpClient = clientFactory.CreateClient();
httpClient.Timeout = TimeSpan.FromMilliseconds(commandTimeout);
var httpClient = clientFactory.CreateClient(nameof(DocumentService));

var body = new CommandBody
{
Expand Down Expand Up @@ -284,12 +272,20 @@ public static async Task<CommandResponse> CommandRequestAsync(FileUtility fileUt
var bodyString = JsonSerializer.Serialize(body, _bodySettings);

request.Content = new StringContent(bodyString, Encoding.UTF8, "application/json");

string dataResponse;
using (var response = await httpClient.SendAsync(request, cancellationTokenSource.Token))
try
{
using var response = await httpClient.SendAsync(request, cancellationTokenSource.Token);
dataResponse = await response.Content.ReadAsStringAsync(cancellationTokenSource.Token);
}
catch (HttpRequestException e) when (e.HttpRequestError == HttpRequestError.NameResolutionError)
{
return new CommandResponse
{
Error = ErrorTypes.UnknownError,
ErrorString = e.Message
};
}

try
{
Expand Down Expand Up @@ -340,8 +336,7 @@ public static async Task<CommandResponse> CommandRequestAsync(FileUtility fileUt
Method = HttpMethod.Post
};

var httpClient = clientFactory.CreateClient();
httpClient.Timeout = TimeSpan.FromMilliseconds(Timeout);
var httpClient = clientFactory.CreateClient(nameof(DocumentService));

var body = new BuilderBody
{
Expand Down Expand Up @@ -722,19 +717,17 @@ private static (int ResultPercent, string responseuri, string convertedFileType)

public static class DocumentServiceHttpClientExtension
{
public static void AddDocumentServiceHttpClient(this IServiceCollection services)
public static void AddDocumentServiceHttpClient(this IServiceCollection services, IConfiguration configuration)
{

services.AddHttpClient(nameof(DocumentService))
.SetHandlerLifetime(TimeSpan.FromMilliseconds(DocumentService.Timeout))
.SetHandlerLifetime(TimeSpan.FromMilliseconds(Convert.ToInt32(configuration["files:docservice:timeout"] ?? "5000")))
.AddPolicyHandler((_, _) =>
HttpPolicyExtensions
.HandleTransientHttpError()
.OrResult(response =>
{
return response.IsSuccessStatusCode
? false
: throw new HttpRequestException($"Response status code: {response.StatusCode}", null, response.StatusCode);
})
.WaitAndRetryAsync(MaxTry, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))));
.HandleTransientHttpError()
.OrResult(response => response.IsSuccessStatusCode
? false
: throw new HttpRequestException($"Response status code: {response.StatusCode}", null, response.StatusCode))
.WaitAndRetryAsync(Convert.ToInt32(configuration["files:docservice:try"] ?? "3"), retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))));
}
}
2 changes: 1 addition & 1 deletion products/ASC.Files/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override async Task ConfigureServices(IServiceCollection services)
services.AddBaseDbContextPool<FilesDbContext>();
services.RegisterQuotaFeature();
services.AddScoped<IWebItem, ProductEntryPoint>();
services.AddDocumentServiceHttpClient();
services.AddDocumentServiceHttpClient(_configuration);

services.AddStartupTask<CheckPdfStartupTask>()
.TryAddSingleton(services);
Expand Down
2 changes: 1 addition & 1 deletion products/ASC.Files/Service/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ public override async Task ConfigureServices(IServiceCollection services)
services.AddSingleton(Channel.CreateUnbounded<FileData<int>>());
services.AddSingleton(svc => svc.GetRequiredService<Channel<FileData<int>>>().Reader);
services.AddSingleton(svc => svc.GetRequiredService<Channel<FileData<int>>>().Writer);
services.AddDocumentServiceHttpClient();
services.AddDocumentServiceHttpClient(Configuration);
}
}
1 change: 1 addition & 0 deletions web/ASC.Web.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public override async Task ConfigureServices(IServiceCollection services)
.TryAddSingleton(services);

services.AddActivePassiveHostedService<NotifySchedulerService>(_configuration, "WebApiNotifySchedulerService");
services.AddDocumentServiceHttpClient(_configuration);
}

public override void Configure(IApplicationBuilder app, IWebHostEnvironment env)
Expand Down
Loading