From 861355d7f534fd8386e963be5b9884f00c951eb3 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 18 May 2018 09:07:55 +0200 Subject: [PATCH] Packages fixed. Close #290 --- src/Squidex.Domain.Apps.Backup/BackupGrain.cs | 242 ------------------ .../EventStreamWriter.cs | 80 ------ .../IBackupArchiveLocation.cs | 13 - .../IBackupGrain.cs | 24 -- src/Squidex.Domain.Apps.Backup/IBackupJob.cs | 25 -- .../Squidex.Domain.Apps.Backup.csproj | 22 -- .../State/BackupState.cs | 18 -- .../State/BackupStateJob.cs | 31 --- .../Squidex.Domain.Apps.Entities.csproj | 4 +- src/Squidex/Squidex.csproj | 4 +- 10 files changed, 4 insertions(+), 459 deletions(-) delete mode 100644 src/Squidex.Domain.Apps.Backup/BackupGrain.cs delete mode 100644 src/Squidex.Domain.Apps.Backup/EventStreamWriter.cs delete mode 100644 src/Squidex.Domain.Apps.Backup/IBackupArchiveLocation.cs delete mode 100644 src/Squidex.Domain.Apps.Backup/IBackupGrain.cs delete mode 100644 src/Squidex.Domain.Apps.Backup/IBackupJob.cs delete mode 100644 src/Squidex.Domain.Apps.Backup/Squidex.Domain.Apps.Backup.csproj delete mode 100644 src/Squidex.Domain.Apps.Backup/State/BackupState.cs delete mode 100644 src/Squidex.Domain.Apps.Backup/State/BackupStateJob.cs diff --git a/src/Squidex.Domain.Apps.Backup/BackupGrain.cs b/src/Squidex.Domain.Apps.Backup/BackupGrain.cs deleted file mode 100644 index 7390cf21f..000000000 --- a/src/Squidex.Domain.Apps.Backup/BackupGrain.cs +++ /dev/null @@ -1,242 +0,0 @@ -// ========================================================================== -// Squidex Headless CMS -// ========================================================================== -// Copyright (c) Squidex UG (haftungsbeschraenkt) -// All rights reserved. Licensed under the MIT license. -// ========================================================================== - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using NodaTime; -using Orleans; -using Orleans.Concurrency; -using Squidex.Domain.Apps.Backup.State; -using Squidex.Domain.Apps.Events.Assets; -using Squidex.Infrastructure; -using Squidex.Infrastructure.Assets; -using Squidex.Infrastructure.EventSourcing; -using Squidex.Infrastructure.Log; -using Squidex.Infrastructure.Orleans; -using Squidex.Infrastructure.States; - -namespace Squidex.Domain.Apps.Backup -{ - [Reentrant] - public sealed class BackupGrain : Grain, IBackupGrain - { - private const int MaxBackups = 10; - private readonly IClock clock; - private readonly IAssetStore assetStore; - private readonly IEventDataFormatter eventDataFormatter; - private readonly ISemanticLog log; - private readonly IEventStore eventStore; - private readonly IBackupArchiveLocation backupArchiveLocation; - private readonly IStore store; - private CancellationTokenSource currentTask; - private BackupStateJob currentJob; - private Guid appId; - private BackupState state; - private IPersistence persistence; - - public BackupGrain( - IAssetStore assetStore, - IBackupArchiveLocation backupArchiveLocation, - IClock clock, - IEventStore eventStore, - IEventDataFormatter eventDataFormatter, - ISemanticLog log, - IStore store) - { - Guard.NotNull(assetStore, nameof(assetStore)); - Guard.NotNull(backupArchiveLocation, nameof(backupArchiveLocation)); - Guard.NotNull(clock, nameof(clock)); - Guard.NotNull(eventStore, nameof(eventStore)); - Guard.NotNull(eventDataFormatter, nameof(eventDataFormatter)); - Guard.NotNull(store, nameof(store)); - Guard.NotNull(log, nameof(log)); - - this.assetStore = assetStore; - this.backupArchiveLocation = backupArchiveLocation; - this.clock = clock; - this.eventStore = eventStore; - this.eventDataFormatter = eventDataFormatter; - this.store = store; - this.log = log; - } - - public override Task OnActivateAsync() - { - return OnActivateAsync(this.GetPrimaryKey()); - } - - public async Task OnActivateAsync(Guid appId) - { - this.appId = appId; - - persistence = store.WithSnapshots(GetType(), appId, s => state = s); - - await ReadAsync(); - await CleanupAsync(); - } - - private async Task ReadAsync() - { - await persistence.ReadAsync(); - } - - private async Task WriteAsync() - { - await persistence.WriteSnapshotAsync(state); - } - - private async Task CleanupAsync() - { - var hasUpdated = false; - - foreach (var job in state.Jobs) - { - if (!job.Stopped.HasValue) - { - await CleanupAsync(job); - - job.Stopped = clock.GetCurrentInstant(); - job.Failed = true; - - hasUpdated = true; - } - } - - if (hasUpdated) - { - await WriteAsync(); - } - } - - private async Task CleanupAsync(BackupStateJob job) - { - await backupArchiveLocation.DeleteArchiveAsync(job.Id); - } - - public async Task StartNewAsync() - { - if (currentTask != null) - { - throw new DomainException("Another backup process is already running."); - } - - if (state.Jobs.Count >= MaxBackups) - { - throw new DomainException($"You cannot have more than {MaxBackups} backups."); - } - - var job = new BackupStateJob { Id = Guid.NewGuid(), Started = clock.GetCurrentInstant() }; - - currentTask = new CancellationTokenSource(); - currentJob = job; - - state.Jobs.Add(job); - - await WriteAsync(); - - try - { - using (var stream = await backupArchiveLocation.OpenStreamAsync(job.Id)) - { - using (var writer = new EventStreamWriter(stream)) - { - await eventStore.QueryAsync(async @event => - { - var eventData = @event.Data; - - if (eventData.Type == nameof(AssetCreated) || - eventData.Type == nameof(AssetUpdated)) - { - var parsedEvent = eventDataFormatter.Parse(eventData); - - var assetVersion = 0L; - var assetId = Guid.Empty; - - if (parsedEvent.Payload is AssetCreated assetCreated) - { - assetId = assetCreated.AssetId; - assetVersion = assetCreated.FileVersion; - } - - if (parsedEvent.Payload is AssetUpdated asetUpdated) - { - assetId = asetUpdated.AssetId; - assetVersion = asetUpdated.FileVersion; - } - - await writer.WriteEventAsync(eventData, async attachmentStream => - { - await assetStore.DownloadAsync(assetId.ToString(), assetVersion, null, attachmentStream); - }); - } - else - { - await writer.WriteEventAsync(eventData); - } - }, "AppId", appId, null, currentTask.Token); - } - - stream.Position = 0; - - currentTask.Token.ThrowIfCancellationRequested(); - - await assetStore.UploadAsync(job.Id.ToString(), 0, null, stream); - - currentTask.Token.ThrowIfCancellationRequested(); - } - } - catch - { - job.Failed = true; - } - finally - { - job.Stopped = clock.GetCurrentInstant(); - - await WriteAsync(); - - currentTask = null; - currentJob = null; - } - } - - public async Task DeleteAsync(Guid id) - { - var job = state.Jobs.FirstOrDefault(x => x.Id == id); - - if (job == null) - { - throw new DomainObjectNotFoundException(id.ToString(), typeof(IBackupJob)); - } - - if (currentJob == job) - { - currentTask?.Cancel(); - } - else - { - state.Jobs.Remove(job); - - await WriteAsync(); - await CleanupAsync(job); - } - } - - public Task>> GetStateAsync() - { - return Task.FromResult(new J>(state.Jobs.OfType().ToList())); - } - - private bool IsRunning() - { - return state.Jobs.Any(x => !x.Stopped.HasValue); - } - } -} diff --git a/src/Squidex.Domain.Apps.Backup/EventStreamWriter.cs b/src/Squidex.Domain.Apps.Backup/EventStreamWriter.cs deleted file mode 100644 index 210132ddc..000000000 --- a/src/Squidex.Domain.Apps.Backup/EventStreamWriter.cs +++ /dev/null @@ -1,80 +0,0 @@ -// ========================================================================== -// Squidex Headless CMS -// ========================================================================== -// Copyright (c) Squidex UG (haftungsbeschraenkt) -// All rights reserved. Licensed under the MIT license. -// ========================================================================== - -using System; -using System.IO; -using System.IO.Compression; -using System.Threading.Tasks; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using Squidex.Infrastructure; -using Squidex.Infrastructure.EventSourcing; - -namespace Squidex.Domain.Apps.Backup -{ - public sealed class EventStreamWriter : DisposableObjectBase - { - private const int MaxItemsPerFolder = 1000; - private readonly StreamWriter streamWriter; - private readonly ZipArchive archive; - private int writtenEvents; - private int writtenAttachments; - - public EventStreamWriter(Stream stream) - { - archive = new ZipArchive(stream, ZipArchiveMode.Update, true); - } - - public async Task WriteEventAsync(EventData eventData, Func attachment = null) - { - var eventObject = - new JObject( - new JProperty("type", eventData.Type), - new JProperty("payload", eventData.Payload), - new JProperty("metadata", eventData.Metadata)); - - var eventFolder = writtenEvents / MaxItemsPerFolder; - var eventPath = $"events/{eventFolder}/{writtenEvents}.json"; - var eventEntry = archive.GetEntry(eventPath) ?? archive.CreateEntry(eventPath); - - using (var stream = eventEntry.Open()) - { - using (var textWriter = new StreamWriter(stream)) - { - using (var jsonWriter = new JsonTextWriter(textWriter)) - { - await eventObject.WriteToAsync(jsonWriter); - } - } - } - - writtenEvents++; - - if (attachment != null) - { - var attachmentFolder = writtenAttachments / MaxItemsPerFolder; - var attachmentPath = $"attachments/{attachmentFolder}/{writtenEvents}.blob"; - var attachmentEntry = archive.GetEntry(attachmentPath) ?? archive.CreateEntry(attachmentPath); - - using (var stream = eventEntry.Open()) - { - await attachment(stream); - } - - writtenAttachments++; - } - } - - protected override void DisposeObject(bool disposing) - { - if (disposing) - { - archive.Dispose(); - } - } - } -} diff --git a/src/Squidex.Domain.Apps.Backup/IBackupArchiveLocation.cs b/src/Squidex.Domain.Apps.Backup/IBackupArchiveLocation.cs deleted file mode 100644 index 300abbbe9..000000000 --- a/src/Squidex.Domain.Apps.Backup/IBackupArchiveLocation.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.IO; -using System.Threading.Tasks; - -namespace Squidex.Domain.Apps.Backup -{ - public interface IBackupArchiveLocation - { - Task OpenStreamAsync(Guid backupId); - - Task DeleteArchiveAsync(Guid backupId); - } -} diff --git a/src/Squidex.Domain.Apps.Backup/IBackupGrain.cs b/src/Squidex.Domain.Apps.Backup/IBackupGrain.cs deleted file mode 100644 index 1c982f853..000000000 --- a/src/Squidex.Domain.Apps.Backup/IBackupGrain.cs +++ /dev/null @@ -1,24 +0,0 @@ -// ========================================================================== -// Squidex Headless CMS -// ========================================================================== -// Copyright (c) Squidex UG (haftungsbeschraenkt) -// All rights reserved. Licensed under the MIT license. -// ========================================================================== - -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Orleans; -using Squidex.Infrastructure.Orleans; - -namespace Squidex.Domain.Apps.Backup -{ - public interface IBackupGrain : IGrainWithGuidKey - { - Task StartNewAsync(); - - Task DeleteAsync(Guid id); - - Task>> GetStateAsync(); - } -} diff --git a/src/Squidex.Domain.Apps.Backup/IBackupJob.cs b/src/Squidex.Domain.Apps.Backup/IBackupJob.cs deleted file mode 100644 index 53a93f9b8..000000000 --- a/src/Squidex.Domain.Apps.Backup/IBackupJob.cs +++ /dev/null @@ -1,25 +0,0 @@ -// ========================================================================== -// Squidex Headless CMS -// ========================================================================== -// Copyright (c) Squidex UG (haftungsbeschraenkt) -// All rights reserved. Licensed under the MIT license. -// ========================================================================== - -using System; -using NodaTime; - -namespace Squidex.Domain.Apps.Backup -{ - public interface IBackupJob - { - Guid Id { get; } - - Instant Started { get; } - - Instant? Stopped { get; } - - bool Failed { get; } - - string DownloadPath { get; } - } -} diff --git a/src/Squidex.Domain.Apps.Backup/Squidex.Domain.Apps.Backup.csproj b/src/Squidex.Domain.Apps.Backup/Squidex.Domain.Apps.Backup.csproj deleted file mode 100644 index cbe70395d..000000000 --- a/src/Squidex.Domain.Apps.Backup/Squidex.Domain.Apps.Backup.csproj +++ /dev/null @@ -1,22 +0,0 @@ - - - netstandard2.0 - - - - - - - - - - - - - - ..\..\Squidex.ruleset - - - - - diff --git a/src/Squidex.Domain.Apps.Backup/State/BackupState.cs b/src/Squidex.Domain.Apps.Backup/State/BackupState.cs deleted file mode 100644 index e6000eea7..000000000 --- a/src/Squidex.Domain.Apps.Backup/State/BackupState.cs +++ /dev/null @@ -1,18 +0,0 @@ -// ========================================================================== -// Squidex Headless CMS -// ========================================================================== -// Copyright (c) Squidex UG (haftungsbeschraenkt) -// All rights reserved. Licensed under the MIT license. -// ========================================================================== - -using System.Collections.Generic; -using Newtonsoft.Json; - -namespace Squidex.Domain.Apps.Backup.State -{ - public sealed class BackupState - { - [JsonProperty] - public List Jobs { get; set; } = new List(); - } -} diff --git a/src/Squidex.Domain.Apps.Backup/State/BackupStateJob.cs b/src/Squidex.Domain.Apps.Backup/State/BackupStateJob.cs deleted file mode 100644 index 410709f13..000000000 --- a/src/Squidex.Domain.Apps.Backup/State/BackupStateJob.cs +++ /dev/null @@ -1,31 +0,0 @@ -// ========================================================================== -// Squidex Headless CMS -// ========================================================================== -// Copyright (c) Squidex UG (haftungsbeschraenkt) -// All rights reserved. Licensed under the MIT license. -// ========================================================================== - -using System; -using Newtonsoft.Json; -using NodaTime; - -namespace Squidex.Domain.Apps.Backup.State -{ - public sealed class BackupStateJob : IBackupJob - { - [JsonProperty] - public Guid Id { get; set; } - - [JsonProperty] - public Instant Started { get; set; } - - [JsonProperty] - public Instant? Stopped { get; set; } - - [JsonProperty] - public string DownloadPath { get; set; } - - [JsonProperty] - public bool Failed { get; set; } - } -} diff --git a/src/Squidex.Domain.Apps.Entities/Squidex.Domain.Apps.Entities.csproj b/src/Squidex.Domain.Apps.Entities/Squidex.Domain.Apps.Entities.csproj index 1912227c2..91803ab9d 100644 --- a/src/Squidex.Domain.Apps.Entities/Squidex.Domain.Apps.Entities.csproj +++ b/src/Squidex.Domain.Apps.Entities/Squidex.Domain.Apps.Entities.csproj @@ -1,4 +1,4 @@ - + netstandard2.0 @@ -15,7 +15,7 @@ - + diff --git a/src/Squidex/Squidex.csproj b/src/Squidex/Squidex.csproj index 16517cf58..edfa2e5f2 100644 --- a/src/Squidex/Squidex.csproj +++ b/src/Squidex/Squidex.csproj @@ -75,8 +75,8 @@ - - + +