Example #1
0
 public Task <LandmarkBookingResponseModel> TriggerRunAsync(LandmarkBookingRequest bookingRequest, ScheduledRunSettingsModel scheduledRunModel = null)
 {
     return(Task.FromResult(new LandmarkBookingResponseModel
     {
         ProcessingId = Guid.NewGuid()
     }));
 }
Example #2
0
        /// <inheritdoc />
        public async Task <LandmarkBookingResponseModel> TriggerRunAsync(LandmarkBookingRequest bookingRequest, ScheduledRunSettingsModel scheduledRunModel = null)
        {
            if (bookingRequest is null)
            {
                throw new ArgumentNullException(nameof(bookingRequest));
            }

            Uri UriBuildAction(LandmarkInstanceConfig config)
            {
                var timezone = config.Timezone;

                if (scheduledRunModel != null && !string.IsNullOrWhiteSpace(timezone) && TimeZoneInfo.GetSystemTimeZones().Any(x => x.StandardName == timezone))
                {
                    var timezoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timezone);
                    scheduledRunModel.DateTime = TimeZoneInfo.ConvertTimeFromUtc(scheduledRunModel.DateTime, timezoneInfo);
                }

                return(MakeRunBookingRequestUri(new Uri(config.BaseUri, ServiceRestEndpointAddress), scheduledRunModel));
            }

            HttpContent ContentBuildAction(LandmarkInstanceConfig config)
            {
                bookingRequest.OrganizationCode ??= config.OrganizationCode;
                bookingRequest.PositionCode ??= config.PositionCode;

                // content must be sent as plain json string
                return(new ObjectContent <string>(JsonConvert.SerializeObject(bookingRequest), new JsonMediaTypeFormatter()));
            }

            var jsonString = ContentBuildAction(_instanceConfiguration);

            using var request = new HttpRequestMessage(HttpMethod.Post, UriBuildAction(_instanceConfiguration))
                  {
                      Content = jsonString
                  };

            request.SetCustomUriBuildAction(UriBuildAction);
            request.SetCustomContentBuildAction(ContentBuildAction);

            using var response = await _policyHttpClient.SendAsync(request).ConfigureAwait(false);

            RaiseInfo($"Payload to Landmark: {jsonString}");

            var result = new LandmarkBookingResponseModel();

            if (response.StatusCode == HttpStatusCode.Accepted)
            {
                var responseContent = response.Content != null ? await response.Content.ReadAsStringAsync().ConfigureAwait(false) : default;

                var processingId = responseContent != null?JsonConvert.DeserializeObject <Guid>(responseContent) : default;

                if (processingId == Guid.Empty)
                {
                    throw new InvalidOperationException("Landmark run processing id was not found in response to the booking request");
                }

                result.ProcessingId = processingId;
            }
            else
            {
                var message = response.StatusCode == HttpStatusCode.InternalServerError
                    ? "Error occurred while processing Landmark booking request"
                    : "Unexpected Landmark run booking response status code returned";
                var exception = new InvalidOperationException($"{message}. StatusCode: {response.StatusCode}");
                exception.Data.Add("ResponseContent", await ReadErrorResponseContent(response).ConfigureAwait(false));
                throw exception;
            }

            return(result);
        }
Example #3
0
        /// <inheritdoc />
        public async Task <LandmarkTriggerRunResult> TriggerRunAsync(LandmarkRunTriggerModel command, ScheduledRunSettingsModel scheduledRunSettings = null)
        {
            if (command is null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            RaiseInfo($"Starting Landmark run trigger process for ScenarioId: {command.ScenarioId}");

            var run = command.ScenarioId != Guid.Empty ? _runRepository.FindByScenarioId(command.ScenarioId) : default;

            if (run is null)
            {
                throw new InvalidOperationException($"Run for scenario {command.ScenarioId} was not found");
            }

            var scenario = run.Scenarios.SingleOrDefault(s => s.Id == command.ScenarioId);

            if (scenario is null)
            {
                throw new InvalidOperationException($"Scenario {command.ScenarioId} was not found");
            }

            try
            {
                var request = new LandmarkBookingRequest
                {
                    InputFiles = _landmarkAutoBookPayloadProvider.GetFiles(run.Id, command.ScenarioId).ToList()
                };

                var autoBookTriggerResult = await _landmarkApi.TriggerRunAsync(request, scheduledRunSettings).ConfigureAwait(false);

                scenario.ExternalRunInfo = new ExternalRunInfo
                {
                    ExternalRunId  = autoBookTriggerResult.ProcessingId,
                    ExternalStatus = scheduledRunSettings is null
                        ? ExternalScenarioStatus.Accepted
                        : ExternalScenarioStatus.Scheduled,
                    ExternalStatusModifiedDate = _clock.GetCurrentInstant().ToDateTimeUtc(),

                    QueueName         = scheduledRunSettings?.QueueName,
                    Priority          = scheduledRunSettings?.Priority,
                    ScheduledDateTime = scheduledRunSettings?.DateTime,
                    Comment           = scheduledRunSettings?.Comment,

                    CreatorId       = scheduledRunSettings?.CreatorId,
                    CreatedDateTime = _clock.GetCurrentInstant().ToDateTimeUtc()
                };

                _runRepository.Update(run);
                _runRepository.SaveChanges();

                RaiseInfo($"Landmark run triggered for Run: {run.Id} and Scenario: {command.ScenarioId}. Processing id: {autoBookTriggerResult.ProcessingId}");

                return(new LandmarkTriggerRunResult
                {
                    RunId = run.Id,
                    ExternalRunId = autoBookTriggerResult.ProcessingId,
                    Status = scheduledRunSettings is null
                        ? ExternalScenarioStatus.Accepted
                        : ExternalScenarioStatus.Scheduled
                });
            }