Beispiel #1
0
        private async Task PollAsync(CancellationToken cancellationToken)
        {
            var translatorName = typeof(TTranslator).FullName;

            using (var context = _scope.ServiceProvider.GetRequiredService <SIOTranslatorDbContext>())
            {
                _translatorState = context.TranslatorStates.FirstOrDefault(ps => ps.Name == translatorName);

                if (_translatorState == null)
                {
                    _translatorState = new TranslatorState
                    {
                        Name        = translatorName,
                        CreatedDate = DateTimeOffset.UtcNow,
                        Position    = 0
                    };

                    context.TranslatorStates.Add(_translatorState);

                    await context.SaveChangesAsync();
                }

                while (!cancellationToken.IsCancellationRequested)
                {
                    try
                    {
                        await context.Entry(_translatorState).ReloadAsync();

                        var page = await _eventStore.TryGetEventsAsync(_translatorState.Position);

                        foreach (var @event in page.Events)
                        {
                            await _translator.HandleAsync(@event);
                        }

                        if (_translatorState.Position == page.Offset)
                        {
                            await Task.Delay(500);
                        }
                        else
                        {
                            _translatorState.Position         = page.Offset;
                            _translatorState.LastModifiedDate = DateTimeOffset.UtcNow;

                            await context.SaveChangesAsync();
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogCritical(ex, $"Projection '{typeof(TTranslator).Name}' failed at postion '{_translatorState.Position}' due to an unexpected error. See exception details for more information.");
                        break;
                    }
                }
            }
        }
Beispiel #2
0
        private static void TranslateHTMLLineToTXTLine(string line, out string[] outputLines)
        {
            var             outputList      = new List <string>();
            string          tag             = "";
            string          outputStr       = "";
            TranslatorState translatorState = TranslatorState.Neutral;

            foreach (var character in line)
            {
                switch (translatorState)
                {
                case TranslatorState.Neutral:
                {
                    if (character == '<')
                    {
                        tag             = "";
                        translatorState = TranslatorState.ReadingTag;
                    }
                    else if (character == '&')
                    {
                        translatorState = TranslatorState.ReadingHTMLSymbol;
                    }
                    else
                    {
                        outputStr += character;
                    }
                    break;
                }

                case TranslatorState.ReadingHTMLSymbol:
                {
                    if (character == ';')
                    {
                        translatorState = TranslatorState.Neutral;
                    }
                    break;
                }

                case TranslatorState.ReadingTag:
                {
                    if (character == ' ' && tag == "img")
                    {
                        tag = "";
                        if (!string.IsNullOrEmpty(outputStr))
                        {
                            outputList.Add(outputStr);
                            outputStr = "";
                        }
                        translatorState = TranslatorState.ExpectImagePath;
                    }
                    else if (character == '>')
                    {
                        translatorState = TranslatorState.Neutral;
                    }
                    else
                    {
                        tag += character;
                    }
                    break;
                }

                case TranslatorState.ExpectImagePath:
                {
                    if (character == '"')
                    {
                        translatorState = TranslatorState.ReadingImagePath;
                    }
                    else if (character == '>')
                    {
                        translatorState = TranslatorState.Neutral;
                    }
                    break;
                }

                case TranslatorState.ReadingImagePath:
                {
                    if (character == '"')
                    {
                        if (!string.IsNullOrEmpty(outputStr))
                        {
                            outputList.Add($"<{outputStr}>");
                            outputStr = "";
                        }
                        translatorState = TranslatorState.ReadingTag;
                    }
                    else
                    {
                        outputStr += character;
                    }
                    break;
                }

                default:
                    Debug.Fail("This case doesn't exist yet.");
                    break;
                }
            }
            outputList.Add(outputStr);
            outputLines = outputList.ToArray();
        }