Example #1
0
        public async Task Should_backup_users_but_no_clients()
        {
            sut.Backup("user1");
            sut.Backup(Subject("user2"));

            sut.Backup(Client("client"));

            var user1 = CreateUser("user1", "*****@*****.**");
            var user2 = CreateUser("user2", "*****@*****.**");

            var users = new Dictionary <string, IUser>
            {
                [user1.Id] = user1,
                [user2.Id] = user2
            };

            var userResolver = A.Fake <IUserResolver>();

            A.CallTo(() => userResolver.QueryManyAsync(A <string[]> .That.Is(user1.Id, user2.Id)))
            .Returns(users);

            var writer = A.Fake <IBackupWriter>();

            Dictionary <string, string>?storedUsers = null;

            A.CallTo(() => writer.WriteJsonAsync(A <string> ._, A <object> ._))
            .Invokes((string _, object json) => storedUsers = (Dictionary <string, string>)json);

            await sut.StoreAsync(writer, userResolver);

            Assert.Equal(new Dictionary <string, string>
            {
                [user1.Id] = user1.Email,
                [user2.Id] = user2.Email
            }, storedUsers);
        }
Example #2
0
        public async Task Should_backup_users_but_no_clients()
        {
            sut.Backup("1");
            sut.Backup(Subject("2"));

            sut.Backup(Client("client"));

            var user1 = UserMocks.User("1", "*****@*****.**");
            var user2 = UserMocks.User("2", "*****@*****.**");

            var users = new Dictionary <string, IUser>
            {
                [user1.Id] = user1,
                [user2.Id] = user2
            };

            var userResolver = A.Fake <IUserResolver>();

            A.CallTo(() => userResolver.QueryManyAsync(A <string[]> .That.Is(user1.Id, user2.Id), ct))
            .Returns(users);

            var writer = A.Fake <IBackupWriter>();

            Dictionary <string, string>?storedUsers = null;

            A.CallTo(() => writer.WriteJsonAsync(A <string> ._, A <object> ._, ct))
            .Invokes(x => storedUsers = x.GetArgument <Dictionary <string, string> >(1));

            await sut.StoreAsync(writer, userResolver, ct);

            Assert.Equal(new Dictionary <string, string>
            {
                [user1.Id] = user1.Email,
                [user2.Id] = user2.Email
            }, storedUsers);
        }
Example #3
0
        private async Task ProcessAsync(BackupJob job, RefToken actor, CancellationToken ct)
        {
            var handlers = CreateHandlers();

            var lastTimestamp = job.Started;

            try
            {
                var appId = DomainId.Create(Key);

                using (var stream = backupArchiveLocation.OpenStream(job.Id))
                {
                    using (var writer = await backupArchiveLocation.OpenWriterAsync(stream))
                    {
                        await writer.WriteVersionAsync();

                        var userMapping = new UserMapping(actor);

                        var context = new BackupContext(appId, userMapping, writer);

                        await eventStore.QueryAsync(async storedEvent =>
                        {
                            var @event = eventDataFormatter.Parse(storedEvent);

                            if (@event.Payload is SquidexEvent squidexEvent && squidexEvent.Actor != null)
                            {
                                context.UserMapping.Backup(squidexEvent.Actor);
                            }

                            foreach (var handler in handlers)
                            {
                                await handler.BackupEventAsync(@event, context);
                            }

                            writer.WriteEvent(storedEvent);

                            job.HandledEvents = writer.WrittenEvents;
                            job.HandledAssets = writer.WrittenAttachments;

                            lastTimestamp = await WritePeriodically(lastTimestamp);
                        }, GetFilter(), null, ct);

                        foreach (var handler in handlers)
                        {
                            ct.ThrowIfCancellationRequested();

                            await handler.BackupAsync(context);
                        }

                        foreach (var handler in handlers)
                        {
                            ct.ThrowIfCancellationRequested();

                            await handler.CompleteBackupAsync(context);
                        }

                        await userMapping.StoreAsync(writer, userResolver);
                    }

                    stream.Position = 0;

                    ct.ThrowIfCancellationRequested();

                    await backupArchiveStore.UploadAsync(job.Id, stream, ct);
                }

                job.Status = JobStatus.Completed;
            }
            catch (OperationCanceledException)
            {
                await RemoveAsync(job);
            }
            catch (Exception ex)
            {
                log.LogError(ex, job.Id.ToString(), (ctx, w) => w
                             .WriteProperty("action", "makeBackup")
                             .WriteProperty("status", "failed")
                             .WriteProperty("backupId", ctx));

                job.Status = JobStatus.Failed;
            }
            finally
            {
                job.Stopped = clock.GetCurrentInstant();

                await state.WriteAsync();

                currentJobToken?.Dispose();
                currentJobToken = null;
                currentJob      = null;
            }
        }