Exemple #1
0
 static Task FireAndForgetJourneyWithLowMemoryStateDelegste(Func <TJourney, Task <TJourneyResult> > journeyTaker,
                                                            LambdaRequest <TJourney> command,
                                                            CloudWatchLogReporter <TJourney, TJourneyResult> cloudWatchLogReporter,
                                                            int journeyIndex,
                                                            CancellationToken token)
 {
     return(Task.Factory.StartNew(
                iteration => InvokeJourney(
                    journeyTaker,
                    command.Journeys[(int)iteration % command.Journeys.Length],
                    cloudWatchLogReporter),
                journeyIndex,
                token).Unwrap());
 }
Exemple #2
0
        static async Task InvokeJourney(Func <TJourney, Task <TJourneyResult> > journeyTaker,
                                        TJourney journey,
                                        CloudWatchLogReporter <TJourney, TJourneyResult> cloudWatchLogReporter)
        {
            var sw = Stopwatch.StartNew();

            try
            {
                var result = await journeyTaker(journey);

                sw.Stop();
                cloudWatchLogReporter.ReportSuccess(journey, sw.Elapsed, result);
            }
            catch (Exception e)
            {
                cloudWatchLogReporter.ReportError(journey, sw.Elapsed, e);
            }
        }
Exemple #3
0
        public async Task Handle(SNSEvent snsEvent, ILambdaContext context)
        {
            var command = JsonConvert.DeserializeObject <LambdaRequest <TJourney> >(snsEvent.Records[0].Sns.Message);

            var journeyReporter = new CloudWatchLogReporter <TJourney, TJourneyResult>(context.Logger);

            var executionInterval = command.Duration / command.RequestCount;

            var scheduler = new TaskSchedulingInterval();

            var endOfStep         = new CancellationTokenSource();
            var endOfStepDuration = command.Duration + (command.Duration / 10);

            endOfStep.CancelAfter(endOfStepDuration);
            var endOfStepTask = Task.Delay(endOfStepDuration, endOfStep.Token);

            try
            {
                var pendingTasks = new List <Task>();
                for (var i = 0; i < command.RequestCount; i++)
                {
                    scheduler.Start();
                    pendingTasks.Add(FireAndForgetJourneyWithLowMemoryStateDelegste(_journeyTaker, command, journeyReporter, i,
                                                                                    endOfStep.Token));

                    await scheduler.WaitFor(executionInterval, endOfStep.Token);
                }

                var firstTask = await Task.WhenAny(Task.WhenAll(pendingTasks), endOfStepTask);

                journeyReporter.ReportFinished(firstTask == endOfStepTask ? CompletionState.Timeout : CompletionState.Success);
            }
            catch (Exception e)
            {
                journeyReporter.ReportFinished(CompletionState.Error, e);
            }
        }