예제 #1
0
        /// <summary>
        /// Waits for an SMS activity to begin, then handles it.
        /// </summary>
        public async Task WaitSmsAsync(ILambdaContext context)
        {
            var startTime     = DateTime.Now;
            var timeAvailable = TimeSpan.FromMinutes(4);

            using (var client = StepFunctionClientFactory.GetClient())
            {
                TimeSpan timeLeft;
                do
                {
                    context.Logger.LogLine("Polling GetActivityTask");
                    var arn = ServerlessConfig.WaitSmsResponseActivityArn;
                    var req = new GetActivityTaskRequest {
                        ActivityArn = arn
                    };
                    var result = await client.GetActivityTaskAsync(req).ConfigureAwait(false);

                    if (result.Input != null && result.TaskToken != null)
                    {
                        context.Logger.LogLine("Received token");

                        // get the current state for the activity
                        var state = JsonConvert.DeserializeObject <State>(result.Input);

                        // save the task token to the survey response and persist the current state
                        // so we can restore it later.
                        var surveyResponse = await _DataAccess.GetSurveyResponseAsync(Sanitizer.HashPhoneNumber(state.PhoneNumber))
                                             .ConfigureAwait(false);

                        surveyResponse.TaskToken  = result.TaskToken;
                        surveyResponse.SavedState = state;
                        await _DataAccess.SaveSurveyResponeAsync(surveyResponse).ConfigureAwait(false);

                        // ask the question -- do this last so that the current state is persisted with the task token
                        await AskQuestionAsync(state.PhoneNumber, Survey.GetQuestion(state.Question)).ConfigureAwait(false);
                    }

                    timeLeft = timeAvailable - (DateTime.Now - startTime);
                } while (timeLeft.TotalMilliseconds >= 65000);

                context.Logger.LogLine("Polling loop terminated");
            }
        }