public override async Task Bind()
        {
            // Register handlers for processing events and handling errors
            EventProcessorClient.ProcessEventAsync += async(args) =>
            {
                Console.WriteLine("ProcessEventAsync");

                if (args.CancellationToken.IsCancellationRequested)
                {
                    return;
                }

                try
                {
                    await _container.CreateItemAsync(
                        await Run(
                            _context,
                            JsonSerializer.Deserialize <TInput>(Encoding.UTF8.GetString(args.Data.Body.ToArray())), args.CancellationToken));

                    await args.UpdateCheckpointAsync(args.CancellationToken);

                    _context.PostHealth(FunHealth.Normal());
                }
                catch (OutOfMemoryException ex)
                {
                    // TODO: Fix Logging
                    //_context.LogError(ex);
                    _context.ScaleUp(ex);
                    _context.PostHealth(FunHealth.Degraded(ex));
                }
                catch (Exception ex)
                {
                    // TODO: retry, etc
                    Console.WriteLine(ex.Message);
                    _context.PostHealth(FunHealth.Failure(ex));
                }
            };

            EventProcessorClient.ProcessErrorAsync += (args) =>
            {
                Console.WriteLine($"ProcessErrorAsync: {args.Exception.Message}");

                //_context.Logger.LogError(eventArgs.Exception);
                // Circuit breaker
                //if (attempts > 3) Health.Error(ex);
                //else Health.Warning(ex);
                _context.PostHealth(FunHealth.Failure(args.Exception));

                return(Task.CompletedTask);
            };

            // Start the processor
            await EventProcessorClient.StartProcessingAsync();

            return;
        }
Esempio n. 2
0
 public override Task <MyDocument> Run(FunContext context, MyEvent input, CancellationToken cancellationToken)
 {
     try
     {
         Console.WriteLine("Run");
         return(Task.FromResult(
                    new MyDocument {
             Id = Guid.NewGuid().ToString("N"),
             MyProperty = input.MyProperty
         }));
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         //context.Logger.LogError(ex);
         context.PostHealth(FunHealth.Failure(ex));
         throw;
     }
 }