Browse Source

MIgration improved.

pull/261/head
Sebastian Stehle 8 years ago
parent
commit
b164795983
  1. 25
      src/Squidex.Domain.Apps.Entities/Backup/BackupGrain.cs
  2. 8
      src/Squidex/app/features/settings/pages/backups/backups-page.component.ts
  3. 31
      tools/Migrate_01/Migrations/ConvertEventStore.cs
  4. 51
      tools/Migrate_01/Migrations/ConvertEventStoreAppId.cs

25
src/Squidex.Domain.Apps.Entities/Backup/BackupGrain.cs

@ -100,9 +100,11 @@ namespace Squidex.Domain.Apps.Entities.Backup
{
if (!job.Stopped.HasValue)
{
await CleanupAsync(job);
job.Stopped = clock.GetCurrentInstant();
await CleanupArchiveAsync(job);
await CleanupBackupAsync(job);
job.IsFailed = true;
await WriteAsync();
@ -110,28 +112,31 @@ namespace Squidex.Domain.Apps.Entities.Backup
}
}
private async Task CleanupAsync(BackupStateJob job)
private async Task CleanupBackupAsync(BackupStateJob job)
{
try
{
await backupArchiveLocation.DeleteArchiveAsync(job.Id);
await assetStore.DeleteAsync(job.Id.ToString(), 0, null);
}
catch (Exception ex)
{
log.LogError(ex, w => w
.WriteProperty("action", "deleteArchive")
.WriteProperty("action", "deleteBackup")
.WriteProperty("status", "failed")
.WriteProperty("backupId", job.Id.ToString()));
}
}
private async Task CleanupArchiveAsync(BackupStateJob job)
{
try
{
await assetStore.DeleteAsync(job.Id.ToString(), 0, null);
await backupArchiveLocation.DeleteArchiveAsync(job.Id);
}
catch (Exception ex)
{
log.LogError(ex, w => w
.WriteProperty("action", "deleteBackup")
.WriteProperty("action", "deleteArchive")
.WriteProperty("status", "failed")
.WriteProperty("backupId", job.Id.ToString()));
}
@ -233,7 +238,7 @@ namespace Squidex.Domain.Apps.Entities.Backup
}
finally
{
await CleanupAsync(job);
await CleanupArchiveAsync(job);
job.Stopped = clock.GetCurrentInstant();
@ -259,10 +264,12 @@ namespace Squidex.Domain.Apps.Entities.Backup
}
else
{
await CleanupArchiveAsync(job);
await CleanupBackupAsync(job);
state.Jobs.Remove(job);
await WriteAsync();
await CleanupAsync(job);
}
}

8
src/Squidex/app/features/settings/pages/backups/backups-page.component.ts

@ -44,7 +44,7 @@ export class BackupsPageComponent implements OnInit, OnDestroy {
public ngOnInit() {
this.loadSubscription =
Observable.timer(0, 5000)
Observable.timer(0, 2000)
.switchMap(t => this.backupsService.getBackups(this.ctx.appName))
.subscribe(dtos => {
this.backups = ImmutableArray.of(dtos);
@ -54,10 +54,6 @@ export class BackupsPageComponent implements OnInit, OnDestroy {
public startBackup() {
this.backupsService.postBackup(this.ctx.appName)
.subscribe(() => {
const backup = new BackupDto('', DateTime.now(), null, 0, 0, false);
this.backups = this.backups.pushFront(backup);
this.ctx.notifyInfo('Backup started.');
}, error => {
this.ctx.notifyError(error);
@ -67,8 +63,6 @@ export class BackupsPageComponent implements OnInit, OnDestroy {
public deleteBackup(backup: BackupDto) {
this.backupsService.deleteBackup(this.ctx.appName, backup.id)
.subscribe(() => {
this.backups = this.backups.filter(x => x.id !== backup.id);
this.ctx.notifyInfo('Backup deleting.');
}, error => {
this.ctx.notifyError(error);

31
tools/Migrate_01/Migrations/ConvertEventStore.cs

@ -5,13 +5,11 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using Newtonsoft.Json.Linq;
using Squidex.Domain.Apps.Events;
using Squidex.Infrastructure;
using Squidex.Infrastructure.EventSourcing;
using Squidex.Infrastructure.Migrations;
using Squidex.Infrastructure.MongoDb;
@ -35,24 +33,37 @@ namespace Migrate_01.Migrations
var filter = Builders<BsonDocument>.Filter;
var writesBatches = new List<WriteModel<BsonDocument>>();
async Task WriteAsync(WriteModel<BsonDocument> model, bool force)
{
if (model != null)
{
writesBatches.Add(model);
}
if (writesBatches.Count == 1000 || (force && writesBatches.Count > 0))
{
await collection.BulkWriteAsync(writesBatches);
writesBatches.Clear();
}
}
await collection.Find(new BsonDocument()).ForEachAsync(async commit =>
{
foreach (BsonDocument @event in commit["Events"].AsBsonArray)
{
var meta = JObject.Parse(@event["Metadata"].AsString);
var data = JObject.Parse(@event["Payload"].AsString);
if (data.TryGetValue("appId", out var appId))
{
meta[SquidexHeaders.AppId] = NamedId<Guid>.Parse(appId.ToString(), Guid.TryParse).Id;
}
@event.Remove("EventId");
@event["Metadata"] = meta.ToBson();
}
await collection.ReplaceOneAsync(filter.Eq("_id", commit["_id"].AsString), commit);
await WriteAsync(new ReplaceOneModel<BsonDocument>(filter.Eq("_id", commit["_id"].AsString), commit), false);
});
await WriteAsync(null, true);
}
}
}

51
tools/Migrate_01/Migrations/ConvertEventStoreAppId.cs

@ -6,6 +6,7 @@
// ==========================================================================
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
@ -32,22 +33,64 @@ namespace Migrate_01.Migrations
{
var collection = mongoEventStore.RawCollection;
var filter = Builders<BsonDocument>.Filter;
var filterer = Builders<BsonDocument>.Filter;
var updater = Builders<BsonDocument>.Update;
var writesBatches = new List<WriteModel<BsonDocument>>();
async Task WriteAsync(WriteModel<BsonDocument> model, bool force)
{
if (model != null)
{
writesBatches.Add(model);
}
if (writesBatches.Count == 1000 || (force && writesBatches.Count > 0))
{
await collection.BulkWriteAsync(writesBatches);
writesBatches.Clear();
}
}
await collection.Find(new BsonDocument()).ForEachAsync(async commit =>
{
UpdateDefinition<BsonDocument> update = null;
var index = 0;
foreach (BsonDocument @event in commit["Events"].AsBsonArray)
{
var data = JObject.Parse(@event["Payload"].AsString);
if (data.TryGetValue("appId", out var appId))
if (data.TryGetValue("appId", out var appIdValue))
{
@event["Metadata"][SquidexHeaders.AppId] = NamedId<Guid>.Parse(appId.ToString(), Guid.TryParse).Id.ToString();
var appId = NamedId<Guid>.Parse(appIdValue.ToString(), Guid.TryParse).Id.ToString();
var eventUpdate = updater.Set($"Events.{index}.Metadata.{SquidexHeaders.AppId}", appId);
if (update != null)
{
update = updater.Combine(update, eventUpdate);
}
else
{
update = eventUpdate;
}
}
index++;
}
await collection.ReplaceOneAsync(filter.Eq("_id", commit["_id"].AsString), commit);
if (update != null)
{
var write = new UpdateOneModel<BsonDocument>(filterer.Eq("_id", commit["_id"].AsString), update);
await WriteAsync(write, false);
}
});
await WriteAsync(null, true);
}
}
}

Loading…
Cancel
Save