Commit 8c098830 authored by Alessio Parma's avatar Alessio Parma

locks with cache

parent 7e3c523f
......@@ -22,10 +22,9 @@
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.3" PrivateAssets="All" />
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.76" PrivateAssets="All" />
<PackageReference Include="NWebsec.AspNetCore.Middleware" Version="3.0.0" />
<PackageReference Include="Polly.Contrib.WaitAndRetry" Version="1.1.1" />
<PackageReference Include="PommaLabs.KVLite.AspNetCore" Version="10.0.1" />
<PackageReference Include="PommaLabs.KVLite.Memory" Version="10.0.1" />
<PackageReference Include="PommaLabs.KVLite.PostgreSql" Version="10.0.1" />
<PackageReference Include="PommaLabs.KVLite.SQLite" Version="10.0.1" />
<PackageReference Include="PommaLabs.KVLite.SqlServer" Version="10.0.1" />
<PackageReference Include="reCAPTCHA.AspNetCore" Version="3.0.3" />
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
......
......@@ -21,14 +21,10 @@
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Polly;
using Polly.Contrib.WaitAndRetry;
using Polly.Retry;
using PommaLabs.MimeTypes;
using PommaLabs.Thumbnailer.Models.DTO;
using PommaLabs.Thumbnailer.Models.Exceptions;
......@@ -42,13 +38,16 @@ namespace PommaLabs.Thumbnailer.Services.Managers.Thumbnail
/// </summary>
public sealed class ConcreteThumbnailManager : IThumbnailManager
{
private const int MaxUnoconvListenerPort = 3000;
private const int MinUnoconvListenerPort = 2000;
private const string WkhtmlCommonFlags = "--quiet --disable-local-file-access --load-media-error-handling ignore --stop-slow-scripts";
private readonly ICommandManager _commandManager;
private readonly ILogger<ConcreteThumbnailManager> _logger;
private readonly AsyncRetryPolicy _retryPolicy;
private readonly ITempFileStore _tempFileStore;
private int _lastUnoconvListenerPort = MinUnoconvListenerPort;
/// <summary>
/// Constructor.
/// </summary>
......@@ -63,16 +62,6 @@ public sealed class ConcreteThumbnailManager : IThumbnailManager
_commandManager = commandManager;
_logger = logger;
_tempFileStore = tempFileStore;
_retryPolicy = Policy
.Handle<CommandException>(ex =>
{
if (ex.CommandName == "unoconv") return true;
return false;
})
.WaitAndRetryAsync(Backoff.DecorrelatedJitterBackoffV2(
medianFirstRetryDelay: TimeSpan.FromSeconds(1),
retryCount: 5));
}
/// <inheritdoc/>
......@@ -159,11 +148,16 @@ private async Task ConvertOfficeToPdfAsync(TempFileDTO file, TempFileDTO pdf, Ca
{
// See: https://ask.libreoffice.org/en/question/7284/multiple-instances-for-c-automation/
var tempProfileDirectory = Path.Combine(_tempFileStore.RootDirectory, Path.GetRandomFileName());
// Each unoconv listener needs a dedicated port.
Interlocked.CompareExchange(ref _lastUnoconvListenerPort, MinUnoconvListenerPort, MaxUnoconvListenerPort);
var listenerPort = Interlocked.Increment(ref _lastUnoconvListenerPort);
try
{
await _retryPolicy.ExecuteAsync(async (_) => await _commandManager.RunCommandAsync(
"unoconv", $"--export=PageRange=1-1 --format=pdf --output={pdf.Path} --user-profile={tempProfileDirectory} {file.Path}",
cancellationToken).ConfigureAwait(false), cancellationToken).ConfigureAwait(false);
await _commandManager.RunCommandAsync(
"unoconv", $"--export=PageRange=1-1 --format=pdf --output={pdf.Path} --user-profile={tempProfileDirectory} --port={listenerPort} {file.Path}",
cancellationToken).ConfigureAwait(false);
}
finally
{
......
......@@ -89,12 +89,12 @@ public async Task<TempFileDTO> HandleFileUploadAsync(IFormFile formFile, Cancell
{
var contentType = formFile.ContentType;
var extension = Path.GetExtension(formFile.FileName);
_logger.LogInformation("Handling upload of file with content type {ContentType} and file extension {Extension}", contentType, extension);
_logger.LogInformation("Handling upload of file with content type '{ContentType}', extension '{Extension}' and file name '{FileName}'", contentType, extension, formFile.FileName);
var file = await GetTempFileAsync(contentType, extension, cancellationToken).ConfigureAwait(false);
if (file.ContentType != contentType)
{
_logger.LogInformation("Content type of uploaded file has been overridden with {ContentType}", file.ContentType);
_logger.LogInformation("Content type of uploaded file has been overridden with '{ContentType}'", contentType);
}
using var fileStream = File.OpenWrite(file.Path);
......@@ -110,12 +110,12 @@ public async Task<TempFileDTO> HandleFileUploadAsync(UploadedFileDTO externalFil
{
var contentType = externalFile.ContentType;
var extension = Path.GetExtension(externalFile.FileName);
_logger.LogInformation("Handling upload of file with content type {ContentType} and extension {Extension}", contentType, extension);
_logger.LogInformation("Handling upload of file with content type '{ContentType}', extension '{Extension}' and file name '{FileName}'", contentType, extension, externalFile.FileName);
var file = await GetTempFileAsync(contentType, extension, cancellationToken).ConfigureAwait(false);
if (file.ContentType != contentType)
{
_logger.LogInformation("Content type of uploaded file has been overridden with {ContentType}", file.ContentType);
_logger.LogInformation("Content type of uploaded file has been overridden with '{ContentType}'", contentType);
}
using var fileStream = File.OpenWrite(file.Path);
......
......@@ -209,7 +209,7 @@ public void ConfigureContainer(ServiceRegistry services)
break;
default:
services.AddKVLiteVolatileSQLiteCache();
services.AddKVLiteMemoryCache();
break;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment