Example #1
0
 internal async Task OnSaved(ReplicateDataMessage message, IEntity entity, SaveMode mode)
 {
     if (Saved.IsHandled())
     {
         await Saved.Raise(new ReplicationSaveMessageProcessedEventArgs(message, entity, mode));
     }
 }
Example #2
0
 internal async Task OnDeleted(ReplicateDataMessage message, IEntity entity)
 {
     if (Deleted.IsHandled())
     {
         await Deleted.Raise(new ReplicationDeleteMessageProcessedEventArgs(message, entity));
     }
 }
Example #3
0
        internal async Task Import(ReplicateDataMessage message)
        {
            if (message.CreationUtc < RefreshRequestUtc)
            {
                // Ignore this. We will receive a full table after this anyway.
                Log.Info("Ignoring importing expired ReplicateDataMessage " + message.DeduplicationId);
                return;
            }

            Log.Debug($"Beginning to import ReplicateDataMessage for {message.TypeFullName}:\n{message.Entity}\n\n");

            IEntity entity;

            try { entity = await Deserialize(message.Entity); }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to deserialize.");
                throw;
            }

            try
            {
                await Database.Save(entity, SaveBehaviour.BypassAll);

                Log.Debug("Saved the " + entity.GetType().FullName + " " + entity.GetId());
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to import.");
                throw;
            }
        }
        internal async Task Import(ReplicateDataMessage message)
        {
            if (message.CreationUtc < RefreshRequestUtc?.Subtract(TimeSyncTolerance))
            {
                // Ignore this. We will receive a full table after this anyway.
                Log.Info("Ignored importing expired ReplicateDataMessage " + message.DeduplicationId + " because it's older the last refresh request.");
                return;
            }

            Log.Debug($"Beginning to import ReplicateDataMessage for {message.TypeFullName}:\n{message.Entity}\n\n");

            IEntity entity;

            try { entity = await Deserialize(message.Entity); }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to deserialize.");
                throw;
            }

            try
            {
                if (message.ToDelete)
                {
                    if (await Endpoint.OnDeleting(message, entity))
                    {
                        await Database.Delete(entity);

                        await Endpoint.OnDeleted(message, entity);
                    }
                }
                else
                {
                    var mode = entity.IsNew ? SaveMode.Insert : SaveMode.Update;

                    if (mode == SaveMode.Update && !await IsActuallyChanged(entity))
                    {
                        return;
                    }

                    if (!await Endpoint.OnSaving(message, entity, mode))
                    {
                        return;
                    }

                    await Database.Save(entity, SaveBehaviour.BypassAll);

                    await GlobalEntityEvents.InstanceSaved.Raise(new GlobalSaveEventArgs(entity, mode));

                    await Endpoint.OnSaved(message, entity, mode);

                    Log.Debug("Saved the " + entity.GetType().FullName + " " + entity.GetId());
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to import.");
                throw;
            }
        }
Example #5
0
        internal async Task <bool> OnSaving(ReplicateDataMessage message, IEntity entity, SaveMode mode)
        {
            if (!Saving.IsHandled())
            {
                return(true);
            }
            var args = new ReplicationSaveMessageReceivedEventArgs(message, entity, mode);
            await Saving.Raise(args);

            return(!args.Cancel);
        }
Example #6
0
        internal async Task <bool> OnDeleting(ReplicateDataMessage message, IEntity entity)
        {
            if (!Deleting.IsHandled())
            {
                return(true);
            }
            var args = new ReplicationDeleteMessageReceivedEventArgs(message, entity);
            await Deleting.Raise(args);

            return(!args.Cancel);
        }
Example #7
0
        async Task Import(ReplicateDataMessage message)
        {
            if (message == null)
            {
                return;
            }

            try
            {
                await Subscribers[message.TypeFullName].Import(message);
            }
            catch (Exception ex)
            {
                Log.For(this).Error(ex, "Failed to import ReplicateDataMessage " + message.Entity);
            }
        }
Example #8
0
 internal ReplicationSaveMessageProcessedEventArgs(ReplicateDataMessage message, IEntity instance, SaveMode mode) : base(message, instance)
 {
     Mode = mode;
 }
Example #9
0
 internal ReplicationDeleteMessageProcessedEventArgs(ReplicateDataMessage message, IEntity instance) : base(message, instance)
 {
 }
Example #10
0
 protected ReplicationEventArgs(ReplicateDataMessage message, IEntity instance)
 {
     Message  = message;
     Instance = instance;
 }
Example #11
0
 Task Import(ReplicateDataMessage message) => Subscribers[message.TypeFullName].Import(message);
Example #12
0
        internal async Task Import(ReplicateDataMessage message)
        {
            if (message.CreationUtc < RefreshRequestUtc?.Subtract(TimeSyncTolerance))
            {
                // Ignore this. We will receive a full table after this anyway.
                Log.Info("Ignored importing expired ReplicateDataMessage " + message.DeduplicationId + " because it's older the last refresh request.");
                return;
            }

            if (message.IsClearSignal)
            {
                Log.Debug($"Received Clear Signal for {message.TypeFullName}");

                await Database.GetAccess(DomainType).ExecuteNonQuery($"truncate table {SchemaAttribute.GetSchema(DomainType).WithSuffix(".")}{TableNameAttribute.GetTableName(DomainType)}");

                return;
            }

            Log.Debug($"Beginning to import ReplicateDataMessage for {message.TypeFullName}:\n{message.Entity}\n\n");

            IEntity entity;

            try { entity = await Deserialize(message.Entity); }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to deserialize a message of :> " + message.TypeFullName + " message : " + message.Entity);
                throw;
            }

            try
            {
                if (message.ToDelete)
                {
                    if (await Endpoint.OnDeleting(message, entity))
                    {
                        await Database.Delete(entity);

                        await Endpoint.OnDeleted(message, entity);
                    }
                }
                else
                {
                    var mode = entity.IsNew ? SaveMode.Insert : SaveMode.Update;

                    if (mode == SaveMode.Update && !await IsActuallyChanged(entity))
                    {
                        return;
                    }

                    if (!await Endpoint.OnSaving(message, entity, mode))
                    {
                        return;
                    }

                    var bypass = SaveBehaviour.BypassAll;

                    if (Config.Get("Database:Audit:EnableForEndpointDataReplication", defaultValue: false))
                    {
                        bypass &= ~SaveBehaviour.BypassLogging;
                    }

                    await Database.Save(entity, bypass);

                    await GlobalEntityEvents.OnInstanceSaved(new GlobalSaveEventArgs(entity, mode));

                    await Endpoint.OnSaved(message, entity, mode);

                    Log.Debug("Saved the " + entity.GetType().FullName + " " + entity.GetId());
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to import.");
                throw;
            }
        }