Exemplo n.º 1
0
        public void Retry(IIndexModel model, string itemId, PushState pushState)
        {
            ItemState state = ItemState.Failed;

            if ((pushState & PushState.RelatedItemNotFound) > 0)
            {
                state = state & ItemState.RelatedItemNotFound;
            }
            if ((pushState & PushState.RelatedItemNotSync) > 0)
            {
                state = state & ItemState.RelatedItemNotSynced;
            }
            if ((pushState & PushState.ValidationFailed) > 0)
            {
                state = state & ItemState.ValidationFailed;
            }
            var sql = $@"
UPDATE [{model.ValueTableName}]
SET [RetryCount] = [RetryCount] + 1,
    [LastUpdated] = @LastUpdated,
    [State] = CASE WHEN [State] = 0 OR [State] IS NULL THEN @States
                ELSE ([State] | @StatesToExclude) ^ @StatesToExclude
                END
WHERE [Id] = @ItemId
";

            _connection.Execute(sql, param: new
            {
                ItemId          = itemId,
                LastUpdated     = DateTime.Now.ToUnixTimestamp(),
                StatesToExclude = ItemState.Processed,
                States          = state
            }, transaction: _transaction);
        }
Exemplo n.º 2
0
        public void Process()
        {
            IndexConfiguration = ReadConfiguration();

            var markets = Configuration.GetSection("Markets").Get <string[]>();

            markets = markets.Where(x => Markets == null || (Markets.Contains(x))).Select(x => x).ToArray();

            foreach (var market in markets)
            {
                if (IndexConfiguration.DataSources.Length > 0)
                {
                    ReadDataResult = ReadData(market);

                    if (ReadDataResult != null && ReadDataResult.Count > 0)
                    {
                        JArray result = Compare(ReadDataResult);

                        if (result != null)
                        {
                            Model = BuildModel(result);
                            //if (Model != null)
                            //{
                            //    var isIndexBuild = BuildIndex(Model);
                            //    if (isIndexBuild)
                            //    {
                            //        //AddToQueue(QueueObject);
                            //    }
                            //}
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
 public override IIndexer SetIndex(IIndexModel model)
 {
     AttributeModel = model as AttributeModel;
     EntityModel    = EntityRepository.GetById(AttributeModel.EntityId.ToString());
     SpreadOptions();
     return(this);
 }
Exemplo n.º 4
0
        public void StartTest(ScheduleOptionModel option)
        {
            try
            {
                if (_running)
                {
                    _errorLogger.Error("The workflow is running");
                    return;
                }
                _running = true;
                RegisterWorkflows(workflowMode);
                var         workflow   = workflows.FirstOrDefault(w => w.Id == option.WorkflowId) as IBaseWorkflow;
                IIndexModel indexModel = option.TargetEntityType == EntityType.Entity
                        ? entityRepository.GetById(option.TargetEntityId.ToString()) as IIndexModel
                        : attributeRepository.GetById(option.TargetEntityId.ToString());

                if (indexModel == null)
                {
                    throw new Exception($"Could not find Index with ID: {option.TargetEntityId}, Name: {option.TargetEntityName}, Type: {option.TargetEntityType}");
                }

                workflow.SetIndex(indexModel);

                _host.Start();

                _host.StartWorkflow(workflow.Id, workflow.Version, null);
            }
            catch (Exception ex)
            {
                _errorLogger.Error(ex, "Sync Service failed to run.");
                throw;
            }
        }
Exemplo n.º 5
0
        public void Retry(IIndexModel model, string itemId, PushState pushState)
        {
            var state = ItemState.None;

            if ((pushState & (PushState.ValidationFailed | PushState.Failed | PushState.UnexpectedError)) > 0)
            {
                // tells the service to not queue this item again
                // but there will be a workflow to re-queue these type of items => never stop the progress
                state = state | ItemState.Invalid;
            }
            var sql = $@"
UPDATE [{model.ValueTableName}]
SET [RetryCount] = [RetryCount] + 1,
    [LastUpdated] = @LastUpdated,
    [State] = CASE WHEN 
                [State] = 0 OR [State] IS NULL THEN @States
                ELSE ([State] | @StatesToExclude | @States) ^ @StatesToExclude
            END
WHERE [Id] = @ItemId
";

            _connection.Execute(sql, param: new
            {
                ItemId          = itemId,
                LastUpdated     = DateTime.Now.ToUnixTimestamp(),
                StatesToExclude = ItemState.None,
                States          = state
            }, transaction: _transaction);
        }
Exemplo n.º 6
0
        private string GetEntityMessage(QueueItemModel item, out IIndexModel indexModel, out IndexItemModel itemModel)
        {
            indexModel = entityRepository.GetById(item.TargetEntityId.ToString());
            var options = entityRepository.LoadOptions(indexModel.Id.ToString(), new List <string> {
                "Indexer"
            });
            var reporterMappingColumnOption = options.FirstOrDefault(o => o.Key == "indexer_reporter_columns");
            var reporterMappingColumns      = !string.IsNullOrWhiteSpace(reporterMappingColumnOption?.Value)
                ? JsonConvert.DeserializeObject <List <ReporterColumnMapping> >(reporterMappingColumnOption.Value)
                : new List <ReporterColumnMapping> {
                new ReporterColumnMapping {
                    SourceName  = "Value",
                    MappingName = "Name",
                    Key         = true,
                    Value       = true
                }
            };
            var entityModel = entityRepository.GetIndexedItemById(indexModel, item.TargetItemId);

            itemModel = entityModel;
            var keys = reporterMappingColumns.Where(r => r.Key)
                       .Select(r => $@"_{r.MappingName}_: {entityModel.GetValue(r.SourceName)?.ToString() ?? "(empty)"}");
            var vals = reporterMappingColumns.Where(r => r.Value)
                       .Select(r => $@"_{r.MappingName}_: {entityModel.GetValue(r.SourceName)?.ToString() ?? "(empty)"}");

            if (reporterMappingColumns.Count == 1)
            {
                vals = reporterMappingColumns.Where(r => r.Value)
                       .Select(r => $@"{entityModel.GetValue(r.SourceName)?.ToString() ?? "(empty)"}");
            }
            var executed   = item.ExecutedAt.UnixTimeToTime().ToString("G");
            var executedIn = item.ExecutedAt - item.ExecuteAt;

            return($@"*{indexModel.Name}* ({string.Join(", ", keys)}): {vals} {executed} in {executedIn} second(s)");
        }
Exemplo n.º 7
0
        public void ChangeIndexedItemsRange(IIndexModel model, ItemState include, ItemState exclude, string fromId = null, string toId = null)
        {
            fromId = !string.IsNullOrWhiteSpace(fromId) ? fromId : "0";

            var sql = $@"
UPDATE {model.ValueTableName}
SET [State] = 
    CASE  
        WHEN [State] = 0 THEN @State
        WHEN [State] IS NULL THEN @State 
        ELSE ([State] | @State | @StatesToExclude) ^ @StatesToExclude
    END
WHERE Id >= @FromId";

            if (!string.IsNullOrWhiteSpace(toId))
            {
                sql = $" AND Id < @ToId";
            }
            _connection.Execute(sql, new
            {
                FromId          = fromId,
                ToId            = toId,
                State           = include | ItemState.Changed,
                StatesToExclude = exclude,
            }, transaction: _transaction);
        }
Exemplo n.º 8
0
        public IEnumerable <IndexItemModel> GetIndexedItemsBySourceId(IIndexModel model, string id)
        {
            var items = _connection.Query($@"
SELECT * FROM [{model.ValueTableName}]
WHERE [SourceId] = @Id", param: new { Id = id }, transaction: _transaction) as IEnumerable <IDictionary <string, object> >;

            return(items?.Select(i => IndexItemModel.FromJObject(JObject.FromObject(i))));
        }
Exemplo n.º 9
0
        public IndexItemModel GetIndexedItemDestinationId(IIndexModel model, string id)
        {
            var items = _connection.Query($@"
SELECT * FROM [{model.ValueTableName}]
WHERE [DestinationId] = @Id", param: new { Id = id }, transaction: _transaction) as IEnumerable <IDictionary <string, object> >;
            var item  = items?.FirstOrDefault();

            return(item != null?IndexItemModel.FromJObject(JObject.FromObject(item)) : null);
        }
Exemplo n.º 10
0
        public void StartTest(ScheduleOptionModel option)
        {
            var entityRepository    = ResolverFactory.Resolve <EntityRepository>();
            var attributeRepository = ResolverFactory.Resolve <AttributeRepository>();

            try
            {
                if (_running)
                {
                    ErrorLogger.Error("The workflow is running");
                    return;
                }
                _running = true;
                workingSchedules.SetSchedules(new List <ScheduleOptionModel> {
                    option
                });
                RegisterWorkflows();
                var         workflow   = workflows.FirstOrDefault(w => w.Id == option.WorkflowId) as IBaseWorkflow;
                IIndexModel indexModel = option.TargetEntityType == EntityType.Entity
                        ? entityRepository.GetById(option.TargetEntityId.ToString()) as IIndexModel
                        : attributeRepository.GetById(option.TargetEntityId.ToString());

                if (indexModel == null)
                {
                    throw new Exception($"Could not find Index with ID: {option.TargetEntityId}, Name: {option.TargetEntityName}, Type: {option.TargetEntityType}");
                }

                _host.Start();
                if (!_ranWorkflows.Contains(workflow.Id))
                {
                    _ranWorkflows.Add(workflow.Id);
                    if (workflow.IsGeneric)
                    {
                        _host.StartGenericWorkflow(workflow);
                    }
                    else
                    {
                        _host.StartWorkflow(workflow.Id);
                    }
                }
                else
                {
                    _host.ResumeWorkflow(workflow.Id);
                }
            }
            catch (Exception ex)
            {
                ErrorLogger.Error(ex, "Sync Service failed to run.");
                throw;
            }
            finally
            {
                entityRepository?.Dispose();
                attributeRepository?.Dispose();
            }
        }
Exemplo n.º 11
0
        public IEnumerable <IndexItemModel> GetIndexChangedItems(
            IIndexModel model,
            int limit,
            int offset,
            out int totalCount)
        {
            var @params = new DynamicParameters();

            @params.Add("Limit", limit > 0 ? limit : 100);
            @params.Add("Offset", offset);
            @params.Add("ChangedState", ItemState.Changed);

            /**
             * Do not ignore RelatedItemNotFound and RelatedItemNotSync,
             * These items should be checked in code logic
             * RelatedItemNotFound: it might be found later
             * RelatedItemNotSynced: it will be synced later
             */
            @params.Add("ExcludeStates", ItemState.Invalid);
            // TODO: RETRY COUNT?
            var sql      = $@"
SELECT v.* 
FROM {model.ValueTableName} v
WHERE 
    v.[DestinationId] IS NULL
    OR v.[State] IS NULL
    OR v.[State] = 0
    OR (
        (v.[State] & @ChangedState) > 0
        AND (v.[State] & @ExcludeStates) = 0
    )
ORDER BY v.[RetryCount], v.[Id] 
OFFSET @Offset ROWS
FETCH NEXT @Limit ROWS ONLY;
";
            var countSql = $@"
SELECT COUNT(v.Id) 
FROM {model.ValueTableName} v
WHERE 
    v.[DestinationId] IS NULL
    OR v.[State] IS NULL
    OR v.[State] = 0
    OR (
        (v.[State] & @ChangedState) > 0
        AND (v.[State] & @ExcludeStates) = 0
    )
";

            totalCount = _connection
                         .Query <int>(countSql, param: @params, transaction: _transaction)
                         .FirstOrDefault();
            var result = _connection
                         .Query(sql, param: @params, transaction: _transaction) as IEnumerable <IDictionary <string, object> >;

            return(result.Select(d => IndexItemModel.FromJObject(JObject.FromObject(d))));
        }
Exemplo n.º 12
0
 public override IPuller SetIndex(IIndexModel model)
 {
     using (var entityRepository = ResolverFactory.Resolve <EntityRepository>())
     {
         AttributeModel = model as AttributeModel;
         EntityModel    = entityRepository.GetById(AttributeModel.EntityId.ToString());
         SpreadOptions();
         return(this);
     }
 }
Exemplo n.º 13
0
        private void CreateOldValueTable(IIndexModel model, string sourceColumnType, IEnumerable <string> columns)
        {
            var createTableSQL = $@"
CREATE TABLE {model.OldValueTableName} (
	[Id] {sourceColumnType} NOT NULL, -- Although it is a primary key but it should not be UNIQUE, there is multiple options attribute
    {string.Join(",\n\t", columns)}
)
";

            _connection.Execute(createTableSQL, transaction: _transaction);
        }
Exemplo n.º 14
0
        public IEnumerable <IndexItemModel> GetIndexedItems(
            IIndexModel model,
            IEnumerable <FilterArgument> filters,
            int limit,
            int offset,
            out int totalCount)
        {
            var @params = new DynamicParameters();

            @params.Add("Limit", limit > 0 ? limit : 100);
            @params.Add("Offset", offset);
            var filterStrs = new List <string>();
            var @where     = string.Empty;

            if (filters != null && filters.Count() > 0)
            {
                foreach (var filter in filters)
                {
                    var paramName = StringExtensions.StringExtensions.Random(10);
                    filterStrs.Add($@"[{filter.Field}] {filter.Op} @Param_{paramName}");
                    @params.Add($@"Param_{paramName}", filter.Target);
                }
                //var condition = string.Join(" AND ", filters.Select(f => $@"[{f.Field}] {f.Op} {}"));
                @where = string.Join(" AND ", filterStrs);
                if (!string.IsNullOrWhiteSpace(@where))
                {
                    @where = $"WHERE {@where}";
                }
            }
            var sql      = $@"
SELECT * FROM {model.ValueTableName}
{@where}
ORDER BY [Id]
OFFSET @Offset ROWS
FETCH NEXT @Limit ROWS ONLY;
";
            var countSql = $@"
SELECT COUNT(*) FROM {model.ValueTableName}
{@where}
";

            totalCount = _connection
                         .Query <int>(countSql, param: @params, transaction: _transaction)
                         .FirstOrDefault();
            var result = _connection
                         .Query(sql, param: @params, transaction: _transaction) as IEnumerable <IDictionary <string, object> >;

            return(result.Select(d => IndexItemModel.FromJObject(JObject.FromObject(d))));
        }
Exemplo n.º 15
0
        public string QueueItem(IIndexModel model, string itemId, bool force = false)
        {
            if (force) // use for requeue errors
            {
                return(Create <QueueItemModel>(new
                {
                    TargetEntityId = model.Id,
                    TargetEntityType = model.EntityType,
                    TargetItemId = itemId,
                    CreatedAt = DateTime.Now.ToUnixTimestamp(),
                    UpdatedAt = DateTime.Now.ToUnixTimestamp(),
                    RetryCount = 0,
                    Status = PushState.None
                }));
            }
            // Get the newest
            var exists = _connection.Query <QueueItemModel>($@"
SELECT TOP 1 * 
FROM [core_queue_items]
WHERE [TargetEntityId] = @TargetEntityId 
    AND [TargetEntityType] = @TargetEntityType 
    AND [TargetItemId] = @TargetItemId
ORDER BY [CreatedAt] DESC -- always get the newest
",
                                                            param: new
            {
                TargetEntityId   = model.Id,
                TargetEntityType = model.EntityType,
                TargetItemId     = itemId
            },
                                                            transaction: _transaction).FirstOrDefault();

            if (exists != null && (exists.Status == 0 || exists.HasState(PushState.Ignore | PushState.Failed | PushState.UnexpectedError)))
            {
                return(null); // no row inserted
            }

            return(Create <QueueItemModel>(new
            {
                TargetEntityId = model.Id,
                TargetEntityType = model.EntityType,
                TargetItemId = itemId,
                CreatedAt = DateTime.Now.ToUnixTimestamp(),
                UpdatedAt = DateTime.Now.ToUnixTimestamp(),
                RetryCount = 0,
                Status = PushState.None
            }));
        }
Exemplo n.º 16
0
        /// <summary>
        /// Inits the current instance
        /// </summary>
        /// <param name="model">The model with the access to the desired data</param>
        public IndexModel(IIndexModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var all = model.GetCollectionData();

            Data     = all.Select(d => d.GetEntity()).ToArray();
            Message  = model.Message?.Text;
            Selected = all
                       .Where(d => d.GetKeyValue().Equals(model.GetId()))
                       .FirstOrDefault()
                       ?.GetEntity() ?? null;
        }
Exemplo n.º 17
0
        private void CreateValueTable(IIndexModel model, string sourceColumnType, IEnumerable <string> columns)
        {
            var createValueTableSQL = $@"
CREATE TABLE {model.ValueTableName} (
	[Id] INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
    [SourceId] {sourceColumnType} NOT NULL,
    [DestinationId] NVARCHAR(MAX),
    [State] INT NULL,
    [LastUpdated] INT NOT NULL,
    [RetryCount] INT NOT NULL DEFAULT 0,
    {string.Join(",\n\t", columns)}
)
";

            _connection.Execute(createValueTableSQL, transaction: _transaction);
        }
Exemplo n.º 18
0
        protected virtual Dictionary <string, string> GetNormalizedValuesByDependencies(IndexItemModel indexedItem = null)
        {
            using (var entityRepository = ResolverFactory.Resolve <EntityRepository>())
                using (var attributeRepository = ResolverFactory.Resolve <AttributeRepository>())
                {
                    var indexedModel = GetIndexModel();
                    indexedItem = indexedItem ?? IndexedItem;
                    var dependencies     = entityRepository.GetDependencies(indexedModel.Id, indexedModel.EntityType);
                    var normalizedValues = new Dictionary <string, string>();
                    foreach (var d in dependencies)
                    {
                        var         foreignKeys    = d.ForeignKeysArr;
                        var         referenceKeys  = d.ReferenceKeysArr;
                        IIndexModel dependsOnModel = null;
                        if (d.TargetEntityType == EntityType.Attribute)
                        {
                            dependsOnModel = attributeRepository.GetById(d.TargetEntityId.ToString());
                        }
                        else
                        {
                            dependsOnModel = entityRepository.GetById(d.TargetEntityId.ToString());
                        }
                        var hasDependencies = entityRepository.GetDependsOnItem(dependsOnModel.ValueTableName, d, indexedItem, out IndexItemModel referencedItem);
                        if (!hasDependencies)
                        {
                            return(normalizedValues);
                        }
                        for (var i = 0; i < foreignKeys.Length; i++)
                        {
                            var foreignKey   = foreignKeys[i];
                            var referenceKey = referenceKeys[i];

                            if (!IndexedItem.Properties().Select(p => p.Name).Contains(foreignKey))
                            {
                                continue;
                            }
                            if (normalizedValues.ContainsKey(foreignKey))
                            {
                                continue;
                            }

                            normalizedValues.Add(foreignKey, referencedItem?.Value <string>(referenceKey));
                        }
                    }
                    return(normalizedValues);
                }
        }
Exemplo n.º 19
0
        public bool Initialized(IIndexModel model)
        {
            var checkTables = new List <string> {
                model.ValueTableName, model.NewValueTableName, model.OldValueTableName
            };
            var exists = true;

            foreach (var table in checkTables)
            {
                var existsSQL = $@"
SELECT * FROM sys.tables
WHERE [name] = N'{table}' AND [type] = 'U'
";
                var existsObj = _connection.Query <object>(existsSQL, transaction: _transaction).FirstOrDefault();
                exists = exists && (existsObj != null);
            }

            return(exists);
        }
Exemplo n.º 20
0
        public virtual void Init(IIndexModel entity)
        {
            var options     = LoadOptions(entity.Id.ToString(), entity.EntityType);
            var checkTables = new List <string> {
                entity.ValueTableName, entity.NewValueTableName, entity.OldValueTableName
            };

            foreach (var table in checkTables)
            {
                var truncateSQL = $@"
IF EXISTS (
    SELECT * FROM sys.tables
    WHERE name = N'{table}' AND type = 'U'
)
BEGIN
    DROP TABLE [{table}];
END;
";
                _connection.Execute(truncateSQL, transaction: _transaction);
            }
            var mappingOptionStr = options.GetValue("indexer_mapping_columns");
            var columnMappings   = !string.IsNullOrWhiteSpace(mappingOptionStr)
                ? JsonConvert.DeserializeObject <List <IndexColumnMapping> >(mappingOptionStr)
                : new List <IndexColumnMapping>();
            var idColumn     = columnMappings.FirstOrDefault(m => m.Primary);
            var idColumnName = idColumn.MappingName;
            var idColumnType = !string.IsNullOrWhiteSpace(idColumn.DataType)
                ? idColumn.DataType
                : "NVARCHAR(MAX)";
            var sqlColumns = columnMappings.Where(m => !m.Primary)
                             .Select(c => {
                var dType = c.DataType;
                if (string.IsNullOrWhiteSpace(dType))
                {
                    dType = "NVARCHAR(MAX)";
                }
                return($@"[{c.MappingName}] {dType.ToUpper()}");
            });

            CreateValueTable(entity, idColumnType, sqlColumns);
            CreateOldValueTable(entity, idColumnType, sqlColumns);
            CreateNewValueTable(entity, idColumnType, sqlColumns);
        }
Exemplo n.º 21
0
 public IPusher CreatePusher(IIndexModel model)
 {
     if (model == null)
     {
         return(null);
     }
     using (var connectionRepository = ResolverFactory.Resolve <ConnectionRepository>())
         using (var entityRepository = ResolverFactory.Resolve <EntityRepository>())
         {
             var     destinationConnection = connectionRepository.GetById(model.DestinationConnectionId.ToString());
             IPusher result = null;
             if (model.EntityType == EntityType.Attribute)
             {
                 var attrModel   = model as AttributeModel;
                 var entityModel = entityRepository.GetById(attrModel.EntityId.ToString());
                 result = pushers
                          .Where(p => typeof(IAttributePusher).IsAssignableFrom(p.GetType()))
                          .Select(p => p as IAttributePusher)
                          .FirstOrDefault(p => p.IsImplemented(model.DestinationProcessorId, entityModel.DestinationProcessorId, destinationConnection.ProviderId));
             }
             else
             {
                 result = pushers
                          .Where(p => typeof(IEntityPusher).IsAssignableFrom(p.GetType()))
                          .Select(p => p as IEntityPusher)
                          .FirstOrDefault(p => p.IsImplemented(model.DestinationProcessorId, destinationConnection.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);
         }
 }
Exemplo n.º 22
0
        public void UpdateItemDestinationId(IIndexModel model, string sourceId, string destinationId)
        {
            // Only works when sync success
            var sql = $@"
UPDATE [{model.ValueTableName}]
SET [DestinationId] = @DestinationId,
    [RetryCount] = 0,
    [LastUpdated] = @LastUpdated,
    [State] = CASE  
        WHEN [State] = 0 OR [State] IS NULL THEN 0
        ELSE ([State] | @StatesToExclude) ^ @StatesToExclude
    END
WHERE [SourceId] = @SourceId
";

            _connection.Execute(sql, param: new
            {
                SourceId        = sourceId,
                DestinationId   = destinationId,
                LastUpdated     = DateTime.Now.ToUnixTimestamp(),
                StatesToExclude = ItemState.Invalid | ItemState.RelatedItemNotFound | ItemState.RelatedItemNotSynced
            }, transaction: _transaction);
        }
Exemplo n.º 23
0
        public void ChangeStateOfIndexedItems(IIndexModel model, ItemState include, ItemState exclude, params string[] ids)
        {
            var sql = $@"
UPDATE {model.ValueTableName}
SET [State] = 
    CASE  
        WHEN [State] = 0 THEN @State
        WHEN [State] IS NULL THEN @State 
        ELSE ([State] | @State | @StatesToExclude) ^ @StatesToExclude
    END";

            if (ids != null && ids.Count() > 0)
            {
                sql += $@"
WHERE Id IN @Id";
            }
            _connection.Execute(sql, new
            {
                Id              = ids,
                State           = include,
                StatesToExclude = exclude,
            }, transaction: _transaction);
        }
Exemplo n.º 24
0
 private bool BuildIndex(IIndexModel model)
 {
     // Indexer.UpdateIndex(Type,jobj);
     return(true);
 }
Exemplo n.º 25
0
 public IMapper SetIndex(IIndexModel model)
 {
     EntityModel = model as EntityModel;
     SpreadOptions();
     return(this);
 }
Exemplo n.º 26
0
 public void SetIndex(IIndexModel model)
 {
     _indexerModel = model;
 }
Exemplo n.º 27
0
        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;
            }
        }
Exemplo n.º 28
0
 public void SetIndex(IIndexModel indexModel)
 {
     _indexModel = indexModel;
     viewModel.SetIndex(indexModel);
 }
Exemplo n.º 29
0
 public void SetIndex(IIndexModel model)
 {
     viewModel.SetIndex(model);
 }
Exemplo n.º 30
0
 public abstract IIndexer SetIndex(IIndexModel model);