public IIndexer CreateIndexer(IIndexModel model) { if (model == null) { return(null); } using (var connectionRepository = ResolverFactory.Resolve <ConnectionRepository>()) using (var entityRepository = ResolverFactory.Resolve <EntityRepository>()) { var sourceConnection = connectionRepository.GetById(model.SourceConnectionId.ToString()); IIndexer result = null; if (model.EntityType == EntityType.Attribute) { var attrModel = model as AttributeModel; var entityModel = entityRepository.GetById(attrModel.EntityId.ToString()); result = indexers.Where(p => typeof(IAttributeIndexer).IsAssignableFrom(p.GetType())) .Select(p => p as IAttributeIndexer) .FirstOrDefault(p => p.IsImplemented(model.SourceProcessorId, entityModel.SourceProcessorId, sourceConnection.ProviderId)); } else { result = indexers.Where(p => typeof(IEntityIndexer).IsAssignableFrom(p.GetType())) .Select(p => p as IEntityIndexer) .FirstOrDefault(p => p.IsImplemented(model.SourceProcessorId, sourceConnection.ProviderId)); } var options = entityRepository.LoadOptions(model.Id.ToString(), model.EntityType) .Select(o => new OptionItem { Name = o.Key, Value = o.Value }); result.SetIndex(model); result.SetOptions(options); return(result); } }
public override async Task Invoke(IStepExecutionContext context = null) { var executeAt = DateTime.Now.ToUnixTimestamp(); var firstQueuedItem = entityRepository.GetCurrentQueuedItems(); if (firstQueuedItem == null) { return; } IIndexModel indexModel = null; IndexItemModel itemModel = null; IIndexer indexer = null; IPusher pusher = null; IEnumerable <OptionItem> options = null; if (firstQueuedItem.TargetEntityType == EntityType.Entity) { indexModel = entityRepository.GetById(firstQueuedItem.TargetEntityId.ToString()); options = entityRepository.LoadOptions(indexModel.Id.ToString()).Select(o => new OptionItem { Name = o.Key, Value = o.Value }); var sourceConnection = connectionRepository.GetById(indexModel.SourceConnectionId.ToString()); var destinationConnection = connectionRepository.GetById(indexModel.DestinationConnectionId.ToString()); indexer = entityIndexers.FirstOrDefault(i => i.IsImplemented(indexModel.SourceProcessorId, sourceConnection.ProviderId)); pusher = entityPushers.FirstOrDefault(p => p.IsImplemented(indexModel.DestinationProcessorId, destinationConnection.ProviderId)); } else { var attributeModel = attributeRepository.GetById(firstQueuedItem.TargetEntityId.ToString()); indexModel = attributeModel; var entityModel = entityRepository.GetById(attributeModel.EntityId.ToString()); options = attributeRepository.LoadOptions(attributeModel.Id.ToString()).Select(o => new OptionItem { Name = o.Key, Value = o.Value }); var sourceConnection = connectionRepository.GetById(attributeModel.SourceConnectionId.ToString()); var destinationConnection = connectionRepository.GetById(attributeModel.DestinationConnectionId.ToString()); indexer = attributeIndexers.FirstOrDefault(i => i.IsImplemented(attributeModel.SourceProcessorId, entityModel.SourceProcessorId, sourceConnection.ProviderId)); pusher = attributePushers.FirstOrDefault(p => p.IsImplemented(attributeModel.DestinationProcessorId, entityModel.DestinationProcessorId, destinationConnection.ProviderId)); } indexer.SetIndex(indexModel); indexer.SetOptions(options); pusher.SetIndex(indexModel); pusher.SetOptions(options); pusherManager.SetIndex(indexModel); pusherManager.OnReport(s => Logger.Information(s)); pusherManager.SetIndexer(indexer); pusherManager.SetPusher(pusher); try { itemModel = entityRepository.GetIndexedItemById(indexModel, firstQueuedItem.TargetItemId.ToString()); var pushState = await pusherManager.PushItem(itemModel); var queueItemStatus = firstQueuedItem.Status == PushState.None ? PushState.Success : firstQueuedItem.Status; var messageId = messageRepository.Create(new { Message = string.Join("\n", pusherManager.GetReportMessages()), CreatedAt = DateTime.Now.ToUnixTimestamp(), MessageType = MessageType.Information, Status = MessageStatus.None }); queueItemStatus = queueItemStatus & pushState; if ((pushState & PushState.Success) <= 0) { queueItemStatus = (queueItemStatus | PushState.Success) ^ (PushState.Success); } queueItemRepository.Update(firstQueuedItem.Id.ToString(), new { UpdatedAt = DateTime.Now.ToUnixTimestamp(), ExecuteAt = executeAt, ExecutedAt = DateTime.Now.ToUnixTimestamp(), MessageId = messageId, Status = queueItemStatus }); } 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 = (firstQueuedItem.Status | PushState.UnexpectedError | PushState.Failed | PushState.Success) ^ PushState.Success, // remove success }); throw; } }
public override async Task Invoke(IStepExecutionContext context = null) { try { Logger.Information($@"Updating index changes of {IndexModel.Name}/{IndexModel.Id}..."); var sourceConnection = connectionRepository.GetById(IndexModel.SourceConnectionId.ToString()); //var destConnection = connectionRepository.GetById(IndexModel.DestinationConnectionId.ToString()); IPuller puller = null; IIndexer indexer = null; if (IndexModel.EntityType == EntityType.Entity) { puller = pullers.Where(p => typeof(IEntityPuller).IsAssignableFrom(p.GetType())) .Select(p => (IEntityPuller)p) .FirstOrDefault(p => p.IsImplemented( IndexModel?.SourceProcessorId, sourceConnection?.ProviderId)); indexer = indexers.Where(p => typeof(IEntityIndexer).IsAssignableFrom(p.GetType())) .Select(p => (IEntityIndexer)p) .FirstOrDefault(p => p.IsImplemented( IndexModel?.SourceProcessorId, sourceConnection?.ProviderId)); } else { var entity = entityRepository.GetById((IndexModel as AttributeModel).EntityId.ToString()); puller = pullers.Where(p => typeof(IAttributePuller).IsAssignableFrom(p.GetType())) .Select(p => (IAttributePuller)p) .FirstOrDefault(p => p.IsImplemented( IndexModel.SourceProcessorId, entity?.SourceProcessorId, sourceConnection?.ProviderId)); indexer = indexers.Where(p => typeof(IAttributeIndexer).IsAssignableFrom(p.GetType())) .Select(p => (IAttributeIndexer)p) .FirstOrDefault(p => p.IsImplemented( IndexModel.SourceProcessorId, entity?.SourceProcessorId, sourceConnection?.ProviderId)); } var options = entityRepository.LoadOptions(IndexModel.Id.ToString(), IndexModel.EntityType) .Select(o => new OptionItem { Name = o.Key, Value = o.Value }); puller.SetIndex(IndexModel); puller.SetOptions(options); indexer.SetIndex(IndexModel); indexer.SetOptions(options); indexerManager.SetIndex(IndexModel); indexerManager.SetPuller(puller); indexerManager.SetIndexer(indexer); indexerManager.OnReport(s => { Logger.Information(s); }); await indexerManager.PullNext(); Logger.Information($@"Updated index changes of {IndexModel.Name}/{IndexModel.Id}"); } catch (Exception ex) { ErrorLogger.Error(ex, ex.Message); throw; } }