Skip to content

Commit

Permalink
Added command execution timeout configuration option for EF Core DbCo…
Browse files Browse the repository at this point in the history
…ntext
  • Loading branch information
Valdis Iljuconoks committed Nov 18, 2024
1 parent c230c73 commit e862a62
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 101 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## [3.0.2]

* Added command execution timeout configuration option for EF Core DbContext

## [3.0.1]

* Fixed issue with internationalized domain name (https://github.com/Geta/geta-optimizely-productfeed/issues/14)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public void ConfigureServices(IServiceCollection services)
.AddProductFeed<MyCommerceProductRecord>(options =>
{
options.ConnectionString = _configuration.GetConnectionString("EPiServerDB");
options.CommandTimeout = TimeSpan.FromMinutes(1);

options.SetEntityMapper<EntityMapper>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Version>3.0.1</Version>
<PackageVersion>3.0.1</PackageVersion>
<Version>3.0.2</Version>
<PackageVersion>3.0.2</PackageVersion>
<PackageId>Geta.Optimizely.ProductFeed.Csv</PackageId>
<Title>Geta Optimizely Csv Product Feed ProductFeed</Title>
<IsPackable>true</IsPackable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Version>3.0.1</Version>
<PackageVersion>3.0.1</PackageVersion>
<Version>3.0.2</Version>
<PackageVersion>3.0.2</PackageVersion>
<PackageId>Geta.Optimizely.ProductFeed.Google</PackageId>
<Title>Geta Optimizely Google Product Feed ProductFeed</Title>
<IsPackable>true</IsPackable>
Expand Down
37 changes: 18 additions & 19 deletions src/Geta.Optimizely.ProductFeed/Configuration/FeedDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,33 @@

using System;

namespace Geta.Optimizely.ProductFeed.Configuration
namespace Geta.Optimizely.ProductFeed.Configuration;

public class FeedDescriptor
{
public class FeedDescriptor
public FeedDescriptor(string name, string fileName, string mimeType)
{
public FeedDescriptor(string name, string fileName, string mimeType)
{
Name = name;
FileName = fileName;
MimeType = mimeType;
}
Name = name;
FileName = fileName;
MimeType = mimeType;
}

public string Name { get; set; }
public string Name { get; set; }

public string FileName { get; set; }
public string FileName { get; set; }

public string MimeType { get; set; }
public string MimeType { get; set; }

public Type Exporter { get; set; }
public Type Exporter { get; set; }

public Type Converter { get; set; }
public Type Converter { get; set; }

public Type Filter { get; set; }
public Type Filter { get; set; }

public Type SiteUrlBuilder { get; set; }
public Type SiteUrlBuilder { get; set; }

public void SetSiteUrlBuilder<TBuilder>() where TBuilder : ISiteUrlBuilder
{
SiteUrlBuilder = typeof(TBuilder);
}
public void SetSiteUrlBuilder<TBuilder>() where TBuilder : ISiteUrlBuilder
{
SiteUrlBuilder = typeof(TBuilder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class ProductFeedOptions<TEntity> : ProductFeedOptions

public Type EntityMapper { get; set; }

public TimeSpan CommandTimeout { get; set; } = TimeSpan.FromSeconds(30);

public void Add(FeedDescriptor feedDescriptor)
{
Descriptors.Add(feedDescriptor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Version>3.0.1</Version>
<PackageVersion>3.0.1</PackageVersion>
<Version>3.0.2</Version>
<PackageVersion>3.0.2</PackageVersion>
<PackageId>Geta.Optimizely.ProductFeed</PackageId>
<Title>Geta Optimizely Product Feed ProductFeed</Title>
<IsPackable>true</IsPackable>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
// Copyright (c) Geta Digital. All rights reserved.
// Licensed under Apache-2.0. See the LICENSE file in the project root for more information

using System;
using Geta.Optimizely.ProductFeed.Models;
using Microsoft.EntityFrameworkCore;

// TODO: test namespace change
namespace Geta.Optimizely.GoogleProductFeed.Repositories
{
public class FeedApplicationDbContext : DbContext
{
private readonly string _connectionString;
namespace Geta.Optimizely.GoogleProductFeed.Repositories;

public FeedApplicationDbContext(string connectionString)
{
_connectionString = connectionString;
}
public sealed class FeedApplicationDbContext : DbContext
{
private readonly string _connectionString;

public FeedApplicationDbContext(DbContextOptions options) : base(options)
{
}
public FeedApplicationDbContext(string connectionString, TimeSpan commandTimeout)
{
_connectionString = connectionString;
Database.SetCommandTimeout(commandTimeout);
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured) return;
public FeedApplicationDbContext(DbContextOptions options) : base(options)
{
}

optionsBuilder.UseSqlServer(_connectionString);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured) return;

public DbSet<FeedEntity> FeedData { get; set; }
optionsBuilder.UseSqlServer(_connectionString);
}

public DbSet<FeedEntity> FeedData { get; set; }
}
123 changes: 73 additions & 50 deletions src/Geta.Optimizely.ProductFeed/Repositories/FeedRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,75 +4,98 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Geta.Optimizely.GoogleProductFeed.Repositories;
using Geta.Optimizely.ProductFeed.Configuration;
using Geta.Optimizely.ProductFeed.Models;
using Microsoft.EntityFrameworkCore;

namespace Geta.Optimizely.ProductFeed.Repositories
namespace Geta.Optimizely.ProductFeed.Repositories;

public class FeedRepository : IFeedRepository
{
public class FeedRepository : IFeedRepository
private readonly FeedApplicationDbContext _applicationDbContext;
private readonly IEnumerable<FeedDescriptor> _descriptors;

public FeedRepository(FeedApplicationDbContext applicationDbContext, IEnumerable<FeedDescriptor> descriptors)
{
private readonly FeedApplicationDbContext _applicationDbContext;
private readonly IEnumerable<FeedDescriptor> _descriptors;
_applicationDbContext = applicationDbContext;
_descriptors = descriptors;
}

public FeedRepository(FeedApplicationDbContext applicationDbContext, IEnumerable<FeedDescriptor> descriptors)
public FeedEntity GetLatestFeed(Uri siteUri)
{
if (siteUri == null)
{
_applicationDbContext = applicationDbContext;
_descriptors = descriptors;
throw new ArgumentNullException(nameof(siteUri));
}

public FeedEntity GetLatestFeed(Uri siteUri)
// we need to do client-side eval because string.Equals with comparison is not supported
var feedContent = _applicationDbContext
.FeedData
.ToList();

return feedContent.FirstOrDefault(f => f.Link.Equals(GetAbsoluteUrlWithoutQuery(siteUri).AbsoluteUri.TrimEnd('/'),
StringComparison.InvariantCultureIgnoreCase));
}

public void Save(ICollection<FeedEntity> feedData)
{
if (feedData == null)
{
if (siteUri == null)
{
throw new ArgumentNullException(nameof(siteUri));
}

// we need to do client-side eval because string.Equals with comparison is not supported
var feedContent = _applicationDbContext
.FeedData
.ToList();

return feedContent.FirstOrDefault(f => f.Link.Equals(GetAbsoluteUrlWithoutQuery(siteUri).AbsoluteUri.TrimEnd('/'),
StringComparison.InvariantCultureIgnoreCase));
return;
}

public void Save(ICollection<FeedEntity> feedData)
var feeds = _applicationDbContext.FeedData.ToList();

foreach (var data in feedData)
{
if (feedData == null)
{
return;
}

var feeds = _applicationDbContext.FeedData.ToList();

foreach (var data in feedData)
{
var found = feeds.FirstOrDefault(f => f.Link.Equals(data.Link, StringComparison.InvariantCultureIgnoreCase));

if (found != null)
{
found.CreatedUtc = DateTime.UtcNow;
found.FeedBytes = data.FeedBytes;
}
else
{
data.CreatedUtc = DateTime.UtcNow;
_applicationDbContext.FeedData.Add(data);
}

_applicationDbContext.SaveChanges();
}
PrepareFeedData(feeds, data);
_applicationDbContext.SaveChanges();
}
}

public FeedDescriptor FindDescriptorByUri(Uri siteUri)
public async Task SaveAsync(ICollection<FeedEntity> feedData, CancellationToken cancellationToken)
{
if (feedData == null)
{
var path = GetAbsoluteUrlWithoutQuery(siteUri).AbsolutePath.Trim('/');
return;
}

return _descriptors.FirstOrDefault(d => d.FileName.Trim('/').Equals(path, StringComparison.InvariantCultureIgnoreCase));
var feeds = await AsyncEnumerable.ToListAsync(_applicationDbContext.FeedData, cancellationToken);
foreach (var data in feedData)
{
PrepareFeedData(feeds, data);
await _applicationDbContext.SaveChangesAsync(cancellationToken);
}
}

private Uri GetAbsoluteUrlWithoutQuery(Uri siteUri)
=> new UriBuilder(siteUri) { Query = string.Empty }.Uri;
private void PrepareFeedData(List<FeedEntity> feeds, FeedEntity data)
{
var found = feeds.FirstOrDefault(f => f.Link.Equals(data.Link, StringComparison.InvariantCultureIgnoreCase));

if (found != null)
{
found.CreatedUtc = DateTime.UtcNow;
found.FeedBytes = data.FeedBytes;
}
else
{
data.CreatedUtc = DateTime.UtcNow;
_applicationDbContext.FeedData.Add(data);
}
}

public FeedDescriptor FindDescriptorByUri(Uri siteUri)
{
var path = GetAbsoluteUrlWithoutQuery(siteUri).AbsolutePath.Trim('/');

return _descriptors.FirstOrDefault(d => d.FileName.Trim('/').Equals(path, StringComparison.InvariantCultureIgnoreCase));
}

private Uri GetAbsoluteUrlWithoutQuery(Uri siteUri)
{
return new UriBuilder(siteUri) { Query = string.Empty }.Uri;
}
}
17 changes: 10 additions & 7 deletions src/Geta.Optimizely.ProductFeed/Repositories/IFeedRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Geta.Optimizely.ProductFeed.Configuration;
using Geta.Optimizely.ProductFeed.Models;

namespace Geta.Optimizely.ProductFeed.Repositories
namespace Geta.Optimizely.ProductFeed.Repositories;

public interface IFeedRepository
{
public interface IFeedRepository
{
FeedEntity GetLatestFeed(Uri siteUri);
FeedEntity GetLatestFeed(Uri siteUri);

void Save(ICollection<FeedEntity> feedData);

void Save(ICollection<FeedEntity> feedData);
Task SaveAsync(ICollection<FeedEntity> feedData, CancellationToken cancellationToken);

FeedDescriptor FindDescriptorByUri(Uri siteUri);
}
FeedDescriptor FindDescriptorByUri(Uri siteUri);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static IServiceCollection AddProductFeed<TEntity>(
services.AddTransient(provider =>
{
var options = provider.GetRequiredService<IOptions<ProductFeedOptions<TEntity>>>();
return new FeedApplicationDbContext(options.Value.ConnectionString);
return new FeedApplicationDbContext(options.Value.ConnectionString, options.Value.CommandTimeout);
});

services.AddTransient<FeedBuilderCreateJob>();
Expand Down

0 comments on commit e862a62

Please sign in to comment.