diff --git a/PlexRequests.Core/PlexRequests.Core.csproj b/PlexRequests.Core/PlexRequests.Core.csproj index 7fdb4b375..4def9282d 100644 --- a/PlexRequests.Core/PlexRequests.Core.csproj +++ b/PlexRequests.Core/PlexRequests.Core.csproj @@ -99,6 +99,7 @@ + diff --git a/PlexRequests.Core/Queue/TransientFaultQueue.cs b/PlexRequests.Core/Queue/TransientFaultQueue.cs new file mode 100644 index 000000000..e3621ee0f --- /dev/null +++ b/PlexRequests.Core/Queue/TransientFaultQueue.cs @@ -0,0 +1,87 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: TransientFaultQueue.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, 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. +// ************************************************************************/ +#endregion + +using System.Collections.Generic; +using System.Threading.Tasks; +using PlexRequests.Helpers; +using PlexRequests.Store; +using PlexRequests.Store.Models; +using PlexRequests.Store.Repository; + +namespace PlexRequests.Core.Queue +{ + public class TransientFaultQueue + { + public TransientFaultQueue(IRepository queue) + { + RequestQueue = queue; + } + + private IRepository RequestQueue { get; } + + + public void QueueItem(RequestedModel request, RequestType type) + { + var queue = new RequestQueue + { + Type = type, + Content = ByteConverterHelper.ReturnBytes(request), + PrimaryIdentifier = request.ProviderId + }; + RequestQueue.Insert(queue); + } + + public async Task QueueItemAsync(RequestedModel request, RequestType type) + { + var queue = new RequestQueue + { + Type = type, + Content = ByteConverterHelper.ReturnBytes(request), + PrimaryIdentifier = request.ProviderId + }; + await RequestQueue.InsertAsync(queue); + } + + public IEnumerable Dequeue() + { + var items = RequestQueue.GetAll(); + + RequestQueue.DeleteAll("RequestQueue"); + + return items; + } + + public async Task> DequeueAsync() + { + var items = RequestQueue.GetAllAsync(); + + await RequestQueue.DeleteAllAsync("RequestQueue"); + + return await items; + } + } +} \ No newline at end of file diff --git a/PlexRequests.Store/Models/RequestQueue.cs b/PlexRequests.Store/Models/RequestQueue.cs new file mode 100644 index 000000000..bdbf25a2f --- /dev/null +++ b/PlexRequests.Store/Models/RequestQueue.cs @@ -0,0 +1,42 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: RequestQueue.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, 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. +// ************************************************************************/ +#endregion + +using Dapper.Contrib.Extensions; + +namespace PlexRequests.Store.Models +{ + [Table("RequestQueue")] + public class RequestQueue : Entity + { + public int PrimaryIdentifier { get; set; } + + public RequestType Type { get; set; } + + public byte[] Content { get; set; } + + } +} \ No newline at end of file diff --git a/PlexRequests.Store/PlexRequests.Store.csproj b/PlexRequests.Store/PlexRequests.Store.csproj index 961b6d4b5..c1310e1ce 100644 --- a/PlexRequests.Store/PlexRequests.Store.csproj +++ b/PlexRequests.Store/PlexRequests.Store.csproj @@ -69,6 +69,7 @@ + diff --git a/PlexRequests.Store/Repository/BaseGenericRepository.cs b/PlexRequests.Store/Repository/BaseGenericRepository.cs index 9cf67a2bb..9cafca7fd 100644 --- a/PlexRequests.Store/Repository/BaseGenericRepository.cs +++ b/PlexRequests.Store/Repository/BaseGenericRepository.cs @@ -327,5 +327,22 @@ namespace PlexRequests.Store.Repository throw; } } + public async Task DeleteAllAsync(string tableName) + { + try + { + ResetCache(); + using (var db = Config.DbConnection()) + { + db.Open(); + await db.ExecuteAsync($"delete from {tableName}"); + } + } + catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt) + { + Log.Fatal(CorruptMessage); + throw; + } + } } } \ No newline at end of file diff --git a/PlexRequests.Store/Repository/IRepository.cs b/PlexRequests.Store/Repository/IRepository.cs index 8ab6037d2..4967d6111 100644 --- a/PlexRequests.Store/Repository/IRepository.cs +++ b/PlexRequests.Store/Repository/IRepository.cs @@ -85,5 +85,6 @@ namespace PlexRequests.Store.Repository IEnumerable Custom(Func> func); Task> CustomAsync(Func>> func); void DeleteAll(string tableName); + Task DeleteAllAsync(string tableName); } } diff --git a/PlexRequests.Store/SqlTables.sql b/PlexRequests.Store/SqlTables.sql index 31b0a73d5..a22258a5d 100644 --- a/PlexRequests.Store/SqlTables.sql +++ b/PlexRequests.Store/SqlTables.sql @@ -131,4 +131,15 @@ CREATE TABLE IF NOT EXISTS PlexEpisodes ); CREATE UNIQUE INDEX IF NOT EXISTS PlexEpisodes_Id ON PlexEpisodes (Id); CREATE INDEX IF NOT EXISTS PlexEpisodes_ProviderId ON PlexEpisodes (ProviderId); + + +CREATE TABLE IF NOT EXISTS RequestQueue +( + Id INTEGER PRIMARY KEY AUTOINCREMENT, + PrimaryIdentifier INTEGER NOT NULL, + Type INTEGER NOT NULL, + Content BLOB NOT NULL +); +CREATE UNIQUE INDEX IF NOT EXISTS PlexUsers_Id ON PlexUsers (Id); + COMMIT; \ No newline at end of file