Add a internal retry when we have a locked db

This commit is contained in:
Jamie Rees 2019-06-04 21:07:20 +01:00
commit 08b7e8f540
3 changed files with 16 additions and 3 deletions

View file

@ -196,7 +196,7 @@ namespace Ombi.Schedule.Jobs.Plex
} }
contentToAdd.Clear(); contentToAdd.Clear();
} }
if (count > 200) if (count > 30)
{ {
await Repo.SaveChangesAsync(); await Repo.SaveChangesAsync();
@ -233,7 +233,7 @@ namespace Ombi.Schedule.Jobs.Plex
} }
contentToAdd.Clear(); contentToAdd.Clear();
} }
if (count > 200) if (count > 30)
{ {
await Repo.SaveChangesAsync(); await Repo.SaveChangesAsync();
} }

View file

@ -16,6 +16,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.2" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Nito.AsyncEx" Version="5.0.0-pre-05" /> <PackageReference Include="Nito.AsyncEx" Version="5.0.0-pre-05" />
<PackageReference Include="Polly" Version="7.1.0" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="1.1.9" /> <PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="1.1.9" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -3,11 +3,13 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query; using Microsoft.EntityFrameworkCore.Query;
using Ombi.Helpers; using Ombi.Helpers;
using Ombi.Store.Context; using Ombi.Store.Context;
using Ombi.Store.Entities; using Ombi.Store.Entities;
using Polly;
namespace Ombi.Store.Repository namespace Ombi.Store.Repository
{ {
@ -83,7 +85,17 @@ namespace Ombi.Store.Repository
protected async Task<int> InternalSaveChanges() protected async Task<int> InternalSaveChanges()
{ {
return await _ctx.SaveChangesAsync(); var policy = Policy
.Handle<SqliteException>()
.WaitAndRetryAsync(new[]
{
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(2),
TimeSpan.FromSeconds(3)
});
var result = await policy.ExecuteAndCaptureAsync(async () => await _ctx.SaveChangesAsync());
return result.Result;
} }