コード例 #1
0
ファイル: RecipesAgent.cs プロジェクト: CardonLabs/Nutriko
        private async Task ProcessEventHandler(ProcessEventArgs eventArgs)
        {
            var recipeItem      = JsonSerializer.Deserialize <Recipe>(Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray()));
            var recipeOperation = eventArgs.Data.Properties["operation"].ToString();
            var recipeEventType = eventArgs.Data.Properties["type"].ToString();

            RecipesBusMessage message = new RecipesBusMessage {
                type      = recipeEventType,
                operation = recipeOperation,
                recipe    = recipeItem
            };

            _recipesSubscription = _recipesBusService.recipesObservable
                                   .Where(e => string.Equals(e.type, "nutriko/type/recipe"))
                                   .Subscribe <RecipesBusMessage>(async e => {
                _logger.LogInformation("Received recipe event: " + e.operation + " -- " + e.recipe.id);

                try
                {
                    /**
                     *  This needs a lot of work - this is too ewwww
                     **/
                    if (e.operation == "nutriko/operation/post")
                    {
                        var operation = await _recipesCosmosClient.AddRecipeAsync(e.recipe);
                        _logger.LogInformation("Recipe committed to store: " + e.recipe.id ?? "no insert" + " with Etag: " + operation.ETag.Value);
                    }

                    if (e.operation == "nutriko/operation/upsert")
                    {
                        var operation = await _recipesCosmosClient.UpsertRecipeAsync(e.recipe);
                        _logger.LogInformation("Recipe upsert to store: " + e.recipe.id ?? "no upsert" + " with Etag: " + operation.ETag.Value);
                    }

                    if (e.operation == "nutriko/operation/delete")
                    {
                        var operation = await _recipesCosmosClient.DeleteRecipeAsync(e.recipe);
                        _logger.LogInformation("Recipe deleted: " + e.recipe.id ?? "no delete" + " with Etag: " + operation.ETag.Value);
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogInformation("Recipe error: " + ex.Message);
                    throw;
                }
            }
                                                                  );

            _recipesBusService.PublishMessage(message);

            _logger.LogInformation("Check-pointing event... ");
            await eventArgs.UpdateCheckpointAsync(eventArgs.CancellationToken);

            _recipesSubscription.Dispose();
        }
コード例 #2
0
 public void PublishMessage(RecipesBusMessage message)
 {
     try
     {
         _recipesBusMessage.OnNext(message);
     }
     catch (Exception e)
     {
         _recipesBusMessage.OnError(e);
     }
 }