private async Task TryTriggerActionAsync() { if (State.ActionConfigs?.Any() == true) { var state = State.CurrentJobState; var targets = State.ActionConfigs.Where( s => s.JobStateFilters?.Any() == true && s.JobStateFilters.Contains(state) && s.ActionWrapper != null); foreach (var target in targets) { var actionDto = new ActionMessageDto() { ActionConfig = target, JobId = State.JobId, JobState = State.CurrentJobState }; var msg = new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(actionDto))); await Policy.Handle <Exception>() .WaitAndRetryAsync(Constants.GlobalRetryTimes, _ => TimeSpan.FromSeconds(Constants.GlobalRetryWaitSec)) .ExecuteAsync(async() => await _wrapper.GetRandomActionQueueClient().SendAsync(msg)); } } }
public async Task <bool> HandleMessageAsync(ActionMessageDto msg) { var flag = false; if (msg.ActionConfig.ActionWrapper.EmailConfig != null) { flag = await Policy.Handle <Exception>() .WaitAndRetryAsync(Constants.GlobalRetryTimes, _ => TimeSpan.FromSeconds(Constants.GlobalRetryWaitSec)) .ExecuteAsync(async() => await SendEmailAsync(msg)); if (!flag) { return(false); } } if (msg.ActionConfig.ActionWrapper.HttpConfig != null) { flag = await Policy.Handle <Exception>() .WaitAndRetryAsync(Constants.GlobalRetryTimes, _ => TimeSpan.FromSeconds(Constants.GlobalRetryWaitSec)) .ExecuteAsync(async() => await SendPostRequestAsync(msg)); if (!flag) { return(false); } } return(flag); }
private async Task <bool> SendEmailAsync(ActionMessageDto config) { var dto = config.ActionConfig.ActionWrapper.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}]-{config.JobId}-{config.JobState}" : dto.Subject; message.Body = $"[{Constants.BrandName}]-{config.JobId}-{config.JobState}"; try { using var smtp = new SmtpClient(_emailConfig.SmtpHost, _emailConfig.SmtpPort) { UseDefaultCredentials = false, Credentials = new NetworkCredential(_emailConfig.Account, _emailConfig.Password), EnableSsl = _emailConfig.EnableSsl }; await smtp.SendMailAsync(message); } catch (Exception ex) { _logger.LogError(ex, $"exception in {nameof(SendEmailAsync)}"); throw; } return(true); }
private async Task <bool> SendPostRequestAsync(ActionMessageDto config) { try { var dto = config.ActionConfig.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); } } var body = new HttpActionBody() { Config = dto, JobId = config.JobId, JobState = config.JobState, Payload = dto.Payload }; 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(SendPostRequestAsync)}"); throw; } return(true); }