/// <summary>
        /// Puts the given DataEntity into its corresponding Mongo collection
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entityToStore"></param>
        public void Put <T>(T entityToStore) where T : DataEntityBase
        {
            IMongoCollection <T> collection = GetCollection <T>();

            if (collection.FindOneAndReplace(o => Equals(o.Id, entityToStore.Id), entityToStore) == null)
            {
                collection.InsertOne(entityToStore);


                if (!string.IsNullOrEmpty(_amazonKinesisStreamName))
                {
                    _amazonFirehoseProducer.PutRecord(_amazonKinesisStreamName,
                                                      FormatEntity(entityToStore, OperationType.Insert));
                }

                if (!string.IsNullOrEmpty(_dataPumpLambdaName))
                {
                    Task.Run(() => _lambdaDataPump.InvokeLambdaAsync(FormatEntity(entityToStore, OperationType.Insert)))
                    .ConfigureAwait(false);
                }

                Logger.Trace($"{nameof(MongoStore)}.{nameof(Put)}",
                             new LogItem("Event", "Insert entity"),
                             new LogItem("Type", typeof(T).ToString),
                             new LogItem("Entity", entityToStore.ToString));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Puts the given DataEntity into its corresponding Dynamo table
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entityToStore"></param>
        public void Put <T>(T entityToStore) where T : DataEntity
        {
            var table = GetCachedTable <T>();

            table.InsertOnSubmit(entityToStore);
            Stopwatch timer = Stopwatch.StartNew();

            try
            {
                _context.SubmitChanges();

                if (!string.IsNullOrEmpty(_amazonKinesisStreamName))
                {
                    _amazonFirehoseProducer.PutRecord(_amazonKinesisStreamName,
                                                      FormatEntity(entityToStore, Helpers.OperationType.Insert));
                }

                if (!string.IsNullOrEmpty(_dataPumpLambdaName))
                {
                    Task.Run(() =>
                             _lambdaDataPump.InvokeLambdaAsync(FormatEntity(entityToStore,
                                                                            Helpers.OperationType.Insert)))
                    .ConfigureAwait(false);
                }

                Logger.Trace($"{nameof(DynamoStore)}.{nameof(Put)}",
                             new LogItem("Event", "Insert entity"),
                             new LogItem("Type", typeof(T).ToString),
                             new LogItem("Entity", entityToStore.ToString),
                             new LogItem("DurationMilliseconds", timer.Elapsed.TotalMilliseconds));
            }
            catch (Exception ex)
            {
                Logger.Error($"{nameof(DynamoStore)}.{nameof(Put)}",
                             new LogItem("Event", "Failed to insert entity"),
                             new LogItem("Type", typeof(T).ToString),
                             new LogItem("Entity", entityToStore.ToString),
                             new LogItem("Exception Message", ex.Message),
                             new LogItem("Stack Trace", ex.StackTrace),
                             new LogItem("DurationMilliseconds", timer.Elapsed.TotalMilliseconds));
                throw;
            }
        }
Esempio n. 3
0
        public void PublishMessage <TNeed, TSolution>(Message <TNeed, TSolution> message, Guid uuid = default)
        {
            if (message.Source == null)
            {
                message.Source = ServiceInfo.Name;
            }

            if (uuid == Guid.Empty)
            {
                uuid = Guid.NewGuid();
            }

            message.LastModifiedBy   = ServiceInfo.Name;
            message.LastModifiedTime = _dateTimeProvider.UtcNow;
            message.Uuid             = uuid;
            message.PublishedTime    = _dateTimeProvider.UtcNow;

            string serializedMessage = JsonConvert.SerializeObject(message,
                                                                   new JsonSerializerSettings {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });

            IModel model = _channelCache.GetNextAvailableChannel();

            model.BasicPublish(QueueConstants.ExchangeName, SelectedRoutingKey, true, null,
                               Encoding.UTF8.GetBytes(serializedMessage));


            if (!string.IsNullOrEmpty(_dataPumpLambdaName))
            {
                Task.Run(() => _lambdaDataPump.InvokeLambdaAsync(serializedMessage)).ConfigureAwait(false);
            }

            _channelCache.ReleaseChannel(model);

            Logger.Trace($"{nameof(RabbitMqWrapper)}.{nameof(PublishMessage)}",
                         new LogItem("Event", "Message published"),
                         new LogItem("Message", serializedMessage),
                         new LogItem("Topic", SelectedRoutingKey),
                         new LogItem("MessageId", uuid.ToString));

            _serviceMetrics.RecordPublishedMessage();
        }