Пример #1
0
 private async Task ScheduleStateCheckMessageAsync(List <StateCheckConfig> configs, long jobId)
 {
     if (configs?.Any() == true)
     {
         foreach (var config in configs)
         {
             var stateCheckDto = new StateCheckMessageDto()
             {
                 StateCheckConfig = config,
                 JobId            = jobId
             };
             var msg = new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(stateCheckDto)));
             await Policy.Handle <Exception>()
             .WaitAndRetryAsync(Constants.GlobalRetryTimes,
                                _ => TimeSpan.FromSeconds(Constants.GlobalRetryWaitSec))
             .ExecuteAsync(async() =>
                           await _wrapper.GetRandomStateCheckQueueClient()
                           .ScheduleMessageAsync(msg, config.CheckTime));
         }
     }
 }
        private async Task <bool> SendStateCheckEmailAsync(ActionConfigWrapper wrapper, StateCheckMessageDto msg, JobEntityState jobEntity)
        {
            var dto = wrapper.EmailConfig;

            if (dto.Recipients?.Any() != true)
            {
                return(true);
            }
            var message = new MailMessage {
                From = new MailAddress(_emailConfig.Account)
            };

            foreach (var recipient in dto.Recipients)
            {
                message.To.Add(recipient);
            }
            if (dto.Ccs?.Any() == true)
            {
                foreach (var cc in dto.Ccs)
                {
                    message.CC.Add(cc);
                }
            }
            message.Subject =
                string.IsNullOrWhiteSpace(dto.Subject)
                ? $"[{Constants.BrandName}]: {jobEntity.JobId}'s State Check Notification" : dto.Subject;
            message.Body = @$ "
JobId:   {jobEntity.JobId}
Expect:  [{string.Join(',', msg.StateCheckConfig.TargetStateList.Select(s => s.ToString()))}]
        private async Task <bool> SendStateCheckPostRequestAsync(ActionConfigWrapper actionWrapper, StateCheckMessageDto msg, JobEntityState jobEntity)
        {
            try
            {
                var dto = actionWrapper.HttpConfig;
                if (string.IsNullOrEmpty(dto.Url))
                {
                    return(true);
                }
                var client = _httpFactory.CreateClient();

                var req = new HttpRequestMessage(HttpMethod.Post, dto.Url);
                if (dto.Headers?.Any() == true)
                {
                    foreach (var header in dto.Headers)
                    {
                        req.Headers.TryAddWithoutValidation(header.Key, header.Value);
                    }
                }
                req.Headers.TryAddWithoutValidation(Constants.StateCheckerHeaderKey, "true");
                var body = new StateCheckActionBody()
                {
                    Config             = dto,
                    JobId              = msg.JobId,
                    JobState           = jobEntity.CurrentJobState,
                    Payload            = dto.Payload,
                    TargetJobStateList = msg.StateCheckConfig.TargetStateList
                };
                req.Content = new StringContent(JsonConvert.SerializeObject(body, _serializerSettings), Encoding.UTF8, "application/json");
                var resp = await client.SendAsync(req);

                resp.EnsureSuccessStatusCode();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"exception in {nameof(SendStateCheckPostRequestAsync)}");
                throw;
            }
            return(true);
        }
        public async Task <bool> HandleStateCheckerMessageAsync(ActionConfigWrapper actionWrapper, StateCheckMessageDto msg, JobEntityState jobEntity)
        {
            if (actionWrapper == null)
            {
                return(true);
            }
            var flag = false;

            if (actionWrapper.EmailConfig != null)
            {
                flag = await Policy.Handle <Exception>()
                       .WaitAndRetryAsync(Constants.GlobalRetryTimes, _ => TimeSpan.FromSeconds(Constants.GlobalRetryWaitSec))
                       .ExecuteAsync(async() => await SendStateCheckEmailAsync(actionWrapper, msg, jobEntity));

                if (!flag)
                {
                    return(false);
                }
            }

            if (actionWrapper.HttpConfig != null)
            {
                flag = await Policy.Handle <Exception>()
                       .WaitAndRetryAsync(Constants.GlobalRetryTimes, _ => TimeSpan.FromSeconds(Constants.GlobalRetryWaitSec))
                       .ExecuteAsync(async() => await SendStateCheckPostRequestAsync(actionWrapper, msg, jobEntity));

                if (!flag)
                {
                    return(false);
                }
            }
            return(flag);
        }