Exemplo n.º 1
0
        public override async Task Invoke(IStepExecutionContext context = null)
        {
            var indexerManager = ResolverFactory.Resolve <IndexerManager>();
            var logger         = ResolverFactory.Resolve <ILogger>("SyncService");
            var errorLogger    = ResolverFactory.Resolve <ILogger>("Error");

            indexerManager.OnReport(s => logger.Information(s));
            try
            {
                logger.Information($@"Updating index changes of {IndexModel.Name}/{IndexModel.Id}...");
                indexerManager.SetIndex(IndexModel as IIndexModel);
                await indexerManager.PullNext();

                await Task.Run(() => { });

                logger.Information($@"Updated index changes of {IndexModel.Name}/{IndexModel.Id}");
                Counter += 1;
            }
            catch (Exception ex)
            {
                errorLogger.Error(ex, ex.Message);
                throw;
            }
            finally
            {
                indexerManager?.Dispose();
                ResolverFactory.Release(logger);
                ResolverFactory.Release(errorLogger);
                logger      = null;
                errorLogger = null;
            }
        }
Exemplo n.º 2
0
        public override async Task Invoke(IStepExecutionContext context = null)
        {
            var logger      = ResolverFactory.Resolve <ILogger>("SyncService");
            var errorLogger = ResolverFactory.Resolve <ILogger>("Error");

            try
            {
                logger.Information($@"Queueing index changes of {IndexModel.Name}/{IndexModel.Id}...");
                queueChangesManager.SetIndex(IndexModel);
                queueChangesManager.OnReport(s => logger.Information(s));
                await queueChangesManager.QueueChanges();

                logger.Information($@"Queued index changes of {IndexModel.Name}/{IndexModel.Id}");
            }
            catch (Exception ex)
            {
                errorLogger.Error(ex, ex.Message);
                throw;
            }
            finally
            {
                Counter += 1;
                ResolverFactory.Release(logger);
                ResolverFactory.Release(errorLogger);
                logger      = null;
                errorLogger = null;
            }
        }
Exemplo n.º 3
0
        public override async Task Invoke(IStepExecutionContext context = null)
        {
            var reporterRepository = ResolverFactory.Resolve <ReporterRepository>();
            var logger             = ResolverFactory.Resolve <ILogger>("SyncService");
            var errorLogger        = ResolverFactory.Resolve <ILogger>("Error");

            try
            {
                var reportModels = reporterRepository.GetAll();
                var loopResult   = Parallel.ForEach(reportModels, async(r, i) =>
                {
                    var options  = reporterRepository.LoadOptions(r.Id.ToString(), r.EntityType);
                    var reporter = reporters.FirstOrDefault(rt => rt.Id == r.ReporterId);
                    reporter.SetOptions(options.Select(o => new OptionItem {
                        Name = o.Key, Value = o.Value
                    }));
                    reporter.SetReportModel(r);
                    await reporter.Queue();
                });
                await Task.Run(() => 1);
            }
            catch (Exception ex)
            {
                errorLogger.Error(ex, ex.Message);
                throw;
            }
            finally
            {
                reporterRepository?.Dispose();
                ResolverFactory.Release(logger);
                ResolverFactory.Release(errorLogger);
                logger      = null;
                errorLogger = null;
            }
        }
Exemplo n.º 4
0
        public override async Task Invoke(IStepExecutionContext context = null)
        {
            var entityRepository = ResolverFactory.Resolve <EntityRepository>();
            var logger           = ResolverFactory.Resolve <ILogger>("SyncService");
            var errorLogger      = ResolverFactory.Resolve <ILogger>("Error");

            try
            {
                logger.Information($@"Requeuing items of {IndexModel.Name}/{IndexModel.Id}...");
                await Task.Run(() => entityRepository.ChangeStateOfIndexedItems(IndexModel, ItemState.None, ItemState.Invalid, null));

                logger.Information($@"Requeued items of {IndexModel.Name}/{IndexModel.Id}");
            }
            catch (Exception ex)
            {
                errorLogger.Error(ex, ex.Message);
                throw;
            }
            finally
            {
                Counter += 1;
                entityRepository?.Dispose();
                ResolverFactory.Release(logger);
                ResolverFactory.Release(errorLogger);
                logger      = null;
                errorLogger = null;
            }
        }
Exemplo n.º 5
0
        public async Task <bool> Apply()
        {
            if (!File.Exists(applicationManager.SettingFile) || !(await indexDatabaseSettingProvider.Validate()))
            {
                Message = indexDatabaseSettingProvider.Message;
                wApplicationSettings.SetProvider(indexDatabaseSettingProvider);
                wApplicationSettings.ShowDialog();
                resolverFactory.Release(wApplicationSettings);
            }
            var result = true;

            indexDatabaseSettingProvider.LoadOptions();
            if (!File.Exists(applicationManager.SettingFile))
            {
                result  = false;
                Message = "Missing database configurations.";
            }
            else if (!await indexDatabaseSettingProvider.Validate())
            {
                result  = false;
                Message = "Could not connect database at the moment.";
            }
            else
            {
                result  = true;
                Message = "Success";
            }

            return(result);
        }
Exemplo n.º 6
0
        private void OnManageIndex(OpenManageIndexPageEventArgument obj)
        {
            if (obj.IndexModel.EntityType != _indexType)
            {
                return;
            }
            var window = resolverFactory.Resolve <WManageIndex>();

            window.Owner   = Application.Current.MainWindow;
            window.Closed += (s, e) => resolverFactory.Release(window);
            window.SetPuller(obj.Puller);
            window.SetIndexer(obj.Indexer);
            window.SetPusher(obj.Pusher);
            window.SetIndex(obj.IndexModel);
            window.SetMapper(obj.Mapper);
            window.ShowDialog();
        }
Exemplo n.º 7
0
        public void Release(object owner)
        {
            lock (lockObject)
            {
                if (!_registeredInstances.ContainsKey(owner))
                {
                    return;
                }

                var repos = _registeredInstances[owner];
                if (repos != null && repos.Count > 0)
                {
                    foreach (var r in repos)
                    {
                        _resolverFactory.Release(r);
                    }
                }

                _registeredInstances.Remove(owner);
            }
        }
        public override async Task Invoke(IStepExecutionContext context = null)
        {
            var scheduleOptionRepository = ResolverFactory.Resolve <ScheduleOptionRepository>();
            var entityRepository         = ResolverFactory.Resolve <EntityRepository>();
            var attributeRepository      = ResolverFactory.Resolve <AttributeRepository>();
            var logger      = ResolverFactory.Resolve <ILogger>("SyncService");
            var errorLogger = ResolverFactory.Resolve <ILogger>("Error");

            try
            {
                Indexes = await Task.Run(() =>
                {
                    var scheduleOptions = workingSchedules.GetWorkingSchedules() ?? scheduleOptionRepository
                                          .GetByWorkflow(WorkflowId)
                                          .Where(o => !o.IsParallel && o.Enabled);
                    var entities = entityRepository
                                   .GetAll()
                                   .Where(e => e.Enabled && scheduleOptions.Any(o => o.TargetEntityId == e.Id && o.TargetEntityType == e.EntityType));
                    var attributes = attributeRepository
                                     .GetAll()
                                     .Where(e => e.Enabled && scheduleOptions.Any(o => o.TargetEntityId == e.Id && o.TargetEntityType == e.EntityType));
                    return(entities.Union(attributes.Select(a => a as IIndexModel)).ToList());
                });
            }
            catch (Exception ex)
            {
                errorLogger.Error(ex, ex.Message);
                throw;
            }
            finally
            {
                entityRepository?.Dispose();
                attributeRepository?.Dispose();
                scheduleOptionRepository?.Dispose();
                ResolverFactory.Release(logger);
                ResolverFactory.Release(errorLogger);
                logger      = null;
                errorLogger = null;
            }
        }
Exemplo n.º 9
0
 public virtual void Dispose()
 {
     resolverFactory.Release(Logger);
 }
Exemplo n.º 10
0
 public void Dispose()
 {
     resolverFactory.Release(_container);
     //resolverFactory.Release(_logger);
 }
Exemplo n.º 11
0
        public override async Task Invoke(IStepExecutionContext context = null)
        {
            var messageRepository  = ResolverFactory.Resolve <MessageRepository>();
            var reporterRepository = ResolverFactory.Resolve <ReporterRepository>();
            var messageDeliveryChannelRepository = ResolverFactory.Resolve <MessageDeliveryChannelRepository>();
            var logger      = ResolverFactory.Resolve <ILogger>("SyncService");
            var errorLogger = ResolverFactory.Resolve <ILogger>("Error");

            try
            {
                var undeliverMessages      = messageRepository.GetUndeliveredMessages(100, 0);
                var linkedReportModels     = messageRepository.GetLinkedReports(undeliverMessages.Select(m => m.Id));
                var reportModels           = reporterRepository.GetByIds(linkedReportModels.Select(r => r.ReporterId.ToString()).Distinct());
                var linkedDeliveryChannels = reporterRepository.GetLinkedDeliveryChannels(reportModels.Select(r => r.Id.ToString()));
                var deliveryChannelModels  = messageDeliveryChannelRepository.GetByIds(linkedDeliveryChannels.Select(c => c.DeliveryChannelId.ToString()).Distinct());
                // Order same same channels, these one should be run in sequence, e.g: slack cannot be send in parallel
                var channelDict = new Dictionary <string, List <MessageDeliveryChannelModel> >();
                foreach (var channel in deliveryChannelModels)
                {
                    if (!channelDict.ContainsKey(channel.ChannelId))
                    {
                        channelDict.Add(channel.ChannelId, new List <MessageDeliveryChannelModel> {
                        });
                    }

                    channelDict[channel.ChannelId].Add(channel);
                }

                await Task.Run(() => Parallel.ForEach(channelDict, async(c, i) =>
                {
                    foreach (var channel in c.Value)
                    {
                        var relatedReports  = reportModels.Where(rp => linkedDeliveryChannels.Any(dc => dc.DeliveryChannelId == channel.Id));
                        var relatedMessages = undeliverMessages.Where(m => linkedReportModels.Any(rm => rm.MessageId == m.Id && relatedReports.Any(r => r.Id == rm.ReporterId)));
                        var messageDic      = new Dictionary <MessageType, List <MessageModel> >();
                        foreach (var relatedMessage in relatedMessages)
                        {
                            if (!messageDic.ContainsKey(relatedMessage.MessageType))
                            {
                                messageDic.Add(relatedMessage.MessageType, new List <MessageModel>());
                            }

                            messageDic[relatedMessage.MessageType].Add(relatedMessage);
                        }
                        var options          = messageDeliveryChannelRepository.LoadOptions(channel.Id.ToString(), channel.EntityType);
                        var delieveryChannel = channels.FirstOrDefault(cc => cc.Id == channel.ChannelId);
                        delieveryChannel.SetOptions(options.Select(o => new OptionItem {
                            Name = o.Key, Value = o.Value
                        }));
                        delieveryChannel.OnReport(s => logger.Information(s));
                        foreach (var dict in messageDic)
                        {
                            if (dict.Value == null || dict.Value.Count == 0)
                            {
                                continue;
                            }
                            try
                            {
                                // send bulk messages in sequence because of MessageType is different
                                await delieveryChannel.DeliverMessage(string.Join("\n", dict.Value.Select(v => v.Message)), dict.Key);
                            }
                            finally
                            {
                                // Update the message as repotered no matter what
                                messageRepository.SetMessagesAsReported(dict.Value.Select(v => v.Id));
                            }
                        }
                    }
                }));
            }
            catch (Exception ex)
            {
                errorLogger.Error(ex, ex.Message);
                throw;
            }
            finally
            {
                ResolverFactory.Release(logger);
                ResolverFactory.Release(errorLogger);
                logger      = null;
                errorLogger = null;
            }
        }
Exemplo n.º 12
0
        public override async Task Invoke(IStepExecutionContext context = null)
        {
            var            entityRepository    = ResolverFactory.Resolve <EntityRepository>();
            var            attributeRepository = ResolverFactory.Resolve <AttributeRepository>();
            var            messageRepository   = ResolverFactory.Resolve <MessageRepository>();
            var            queueItemRepository = ResolverFactory.Resolve <QueueItemRepository>();
            IIndexModel    indexModel          = null;
            IndexItemModel itemModel           = null;
            var            logger        = ResolverFactory.Resolve <ILogger>("SyncService");
            var            errorLogger   = ResolverFactory.Resolve <ILogger>("Error");
            var            pusherManager = ResolverFactory.Resolve <PusherManager>();

            try
            {
                var executeAt       = DateTime.Now.ToUnixTimestamp();
                var firstQueuedItem = entityRepository.GetCurrentQueuedItems();
                if (firstQueuedItem == null)
                {
                    return;
                }

                if (firstQueuedItem.TargetEntityType == EntityType.Entity)
                {
                    indexModel = entityRepository.GetById(firstQueuedItem.TargetEntityId.ToString());
                }
                else
                {
                    indexModel = attributeRepository.GetById(firstQueuedItem.TargetEntityId.ToString());
                }

                pusherManager.SetIndex(indexModel);
                pusherManager.OnReport(s => logger.Information(s));

                try
                {
                    itemModel = entityRepository.GetIndexedItemById(indexModel, firstQueuedItem.TargetItemId.ToString());
                    var pushState = await pusherManager.PushItem(itemModel);

                    var messageId = messageRepository.Create(new
                    {
                        Message     = string.Join("\n", pusherManager.GetReportMessages()),
                        CreatedAt   = DateTime.Now.ToUnixTimestamp(),
                        MessageType = MessageType.Information,
                        Status      = MessageStatus.None
                    });
                    queueItemRepository.Update(firstQueuedItem.Id.ToString(), new
                    {
                        UpdatedAt  = DateTime.Now.ToUnixTimestamp(),
                        ExecuteAt  = executeAt,
                        ExecutedAt = DateTime.Now.ToUnixTimestamp(),
                        MessageId  = messageId,
                        Status     = pushState
                    });
                }
                catch (Exception ex)
                {
                    var messages  = $@"Queue item (Id: {firstQueuedItem.Id}) failed to run. 
Addtional information:
```{JsonConvert.SerializeObject(indexModel, Formatting.Indented)}```
Progress: 
```{string.Join("\n - ", pusherManager.GetReportMessages())}```
Exception: 
```{ex}```";
                    var messageId = messageRepository.Create(new
                    {
                        Message     = messages,
                        CreatedAt   = DateTime.Now.ToUnixTimestamp(),
                        MessageType = MessageType.Error,
                        Status      = MessageStatus.None
                    });

                    queueItemRepository.Update(firstQueuedItem.Id.ToString(), new
                    {
                        UpdatedAt  = DateTime.Now.ToUnixTimestamp(),
                        ExecuteAt  = executeAt,
                        ExecutedAt = DateTime.Now.ToUnixTimestamp(),
                        MessageId  = messageId,
                        Status     = PushState.UnexpectedError
                    });
                    throw;
                }
            }
            catch (Exception ex)
            {
                errorLogger.Error(ex, ex.Message);
                throw;
            }
            finally
            {
                entityRepository?.Dispose();
                attributeRepository?.Dispose();
                messageRepository?.Dispose();
                queueItemRepository?.Dispose();
                ResolverFactory.Release(logger);
                ResolverFactory.Release(errorLogger);
                logger      = null;
                errorLogger = null;
            }
        }
Exemplo n.º 13
0
 public virtual void Dispose()
 {
     ResolverFactory.Release(Logger);
     ResolverFactory.Release(ErrorLogger);
 }