public override async Task <bool> ExecuteAsync(CancellationToken cancellationToken = default) { var proc = More.GetValue("proc").ThrowIfBlank("proc"); var data = More.GetValue("data"); var timeout = More.GetValue("timeout", (int?)null); var result_name = More.GetValue("result_name"); await LogHelper.LogDebugAsync(nameof(RunSqlProcAction), new { jobId = JobId, proc, data = ShortStr(data, 200), result_name, timeout }, cancellationToken); if (string.IsNullOrEmpty(result_name)) { _ = await DbHelper.ExecProcAsync($"WJb_{proc}", data, timeout, cancellationToken); await LogHelper.LogInformationAsync(nameof(RunSqlProcAction), new { jobId = JobId }, cancellationToken); } else { var result = await DbHelper.FromProcAsync <string?>($"WJb_{proc}", data, timeout, cancellationToken); More[result_name] = result; await LogHelper.LogInformationAsync(nameof(RunSqlProcAction), new { jobId = JobId, result = ShortStr(result, 200) }, cancellationToken); } return(true); }
public override async Task <bool> ExecuteAsync(CancellationToken cancellationToken = default) { var url = More.GetValue("url").ThrowIfBlank("url"); var result_name = More.GetValue("result_name"); if (string.IsNullOrEmpty(result_name)) { result_name = "next_body"; } await LogHelper.LogDebugAsync(nameof(Url2HtmlAction), new { jobId = JobId, url, result_name }); var html = string.Empty; using var client = new WebClient() { Encoding = Encoding.UTF8 }; html = await client.DownloadStringTaskAsync(url); if (html?.Length > 200) { html = await DbHelper.FromProcAsync("WJbFiles_Ins", new { FileName = "body.html", FileContent = Encoding.UTF8.GetBytes(html) }, cancellationToken : cancellationToken); } More[result_name] = html; await LogHelper.LogInformationAsync(nameof(Url2HtmlAction), new { jobId = JobId, result = "OK", html }); return(true); }
public override async Task <bool> ExecuteAsync(CancellationToken cancellationToken = default) { string template_prefix = "template_", tvalue_prefix = "tvalue_"; // @"[A-Z]{1,}[_]{1,}[A-Z]{1,}[_]{0,}[A-Z]{0,}" var tname_pattern = More.GetValue("tname_pattern"); ArgumentNullException.ThrowIfNull(tname_pattern); var vals = new More(); foreach (var more in More.Where(item => item.Key.StartsWith(tvalue_prefix))) { vals.Add(more.Key[tvalue_prefix.Length..], more.Value);
public override async Task <bool> ExecuteAsync(CancellationToken cancellationToken = default) { var api_settings_name = More.GetValue("api_settings_name").ThrowIfBlank("api_settings_name"); var api_settings = await DbHelper.FromProcAsync <ApiSettings>("WJbSettings_Get", api_settings_name, cancellationToken : cancellationToken); ArgumentNullException.ThrowIfNull(api_settings); var url = api_settings.Url; ArgumentNullException.ThrowIfNull(url); var proc = More.GetValue("proc"); ArgumentNullException.ThrowIfNull(proc); var data = More.GetValue("data"); var body = More.GetValue("body"); var result_name = More.GetValue("result_name"); if (string.IsNullOrEmpty(result_name)) { result_name = "next_data"; } await LogHelper.LogDebugAsync(nameof(RunApiProcAction), new { jobId = JobId, url = api_settings.Url, proc, data = ShortStr(data, 200), body = ShortStr(body, 200), result_name }, cancellationToken); HttpMethod method = HttpMethod.Get; if (!string.IsNullOrEmpty(body)) { method = HttpMethod.Post; data = "body"; } using HttpClient httpClient = new(); httpClient.BaseAddress = new Uri(url); httpClient.DefaultRequestHeaders.Add("ApiHoleKey", api_settings.Key); var requestUri = $"{proc}"; if (!string.IsNullOrEmpty(data)) { requestUri += $"?data={Uri.EscapeDataString(data)}"; } HttpRequestMessage requestMessage = new(method, requestUri); if (!string.IsNullOrEmpty(body)) { requestMessage.Content = new StringContent(body, Encoding.UTF8); } var httpResponse = await httpClient.SendAsync(requestMessage, cancellationToken); httpResponse.EnsureSuccessStatusCode(); var responseBody = await httpResponse.Content.ReadAsStringAsync(cancellationToken); if (responseBody != null && responseBody.StartsWith("Error:")) { throw new Exception(responseBody.Replace("Error:", "").TrimStart()); } await LogHelper.LogInformationAsync(nameof(RunApiProcAction), new { jobId = JobId, result = ShortStr(responseBody, 200) }, cancellationToken); More[result_name] = responseBody; return(true); }
public override async Task <bool> ExecuteAsync(CancellationToken cancellationToken = default) { var smtp_settings_name = More.GetValue("smtp_settings_name").ThrowIfBlank("smtp_settings_name"); var smtp_settings = await DbHelper.FromProcAsync <SmtpSettings>("WJbSettings_Get", smtp_settings_name, cancellationToken : cancellationToken); ArgumentNullException.ThrowIfNull(smtp_settings); var from = More.GetValue("from"); ArgumentNullException.ThrowIfNull(from); var to = More.GetValue("to"); ArgumentNullException.ThrowIfNull(to); var cc = More.GetValue("cc"); var bcc = More.GetValue("bcc"); var subject = More.GetValue("subject"); var body = More.GetValue("body"); var attachment = More.GetValue("attachment"); var attachments = More.GetValue("attachments"); await LogHelper.LogDebugAsync(nameof(SendEmailAction), new { jobId = JobId, to, cc, bcc, subject, body = ShortStr(body, 200), attachment, attachments }, cancellationToken); if (Guid.TryParse(body, out var guidBody)) { var file = await DbHelper.FromProcAsync <Data.File>("WJbFiles_Get", body, cancellationToken : cancellationToken); if (file?.FileContent != null) { body = Encoding.UTF8.GetString(file.FileContent); } } MailMessage message = new(from, to, subject, body) { IsBodyHtml = Utility.IsHtmlBody(body) }; if (!string.IsNullOrEmpty(cc)) { message.CC.Add(cc); } if (!string.IsNullOrEmpty(bcc)) { message.Bcc.Add(bcc); } if (!string.IsNullOrEmpty(attachment)) { if (Guid.TryParse(attachment, out var guidAttach)) { var file = await DbHelper.FromProcAsync <Data.File>("WJbFiles_Get", attachment, cancellationToken : cancellationToken); if (file?.FileContent != null) { message.Attachments.Add(new Attachment(new MemoryStream(file.FileContent), file.FileName)); } } else { message.Attachments.Add(new Attachment(attachment)); } } else if (!string.IsNullOrEmpty(attachments)) { foreach (var fileName in JsonSerializer.Deserialize <object[]>(attachments) ?? Enumerable.Empty <object>()) { var fileNameStr = Convert.ToString(fileName); if (Guid.TryParse(fileNameStr, out var guidAttach)) { var file = await DbHelper.FromProcAsync <Data.File>("WJbFiles_Get", guidAttach, cancellationToken : cancellationToken); if (file?.FileContent != null) { message.Attachments.Add(new Attachment(new MemoryStream(file.FileContent), file.FileName)); } } else if (!string.IsNullOrWhiteSpace(fileNameStr)) { message.Attachments.Add(new Attachment(fileNameStr)); } } } using var smtp = new SmtpClient(smtp_settings.Host, smtp_settings.Port); smtp.EnableSsl = smtp_settings.EnableSsl; smtp.Credentials = new NetworkCredential(smtp_settings.UserName, smtp_settings.Password); await smtp.SendMailAsync(message, cancellationToken); await LogHelper.LogInformationAsync(nameof(SendEmailAction), new { jobId = JobId, result = "OK" }, cancellationToken); return(true); } }
public static int?GetValue(this More more, string name, int?defaultValue) { return((int.TryParse(more.GetValue(name), out int value)) ? value : defaultValue); }
public static bool?GetValue(this More more, string name, bool?defaultValue) { return(bool.TryParse(more.GetValue(name), out bool value) ? value : defaultValue); }
public override async Task <bool> ExecuteAsync(CancellationToken cancellationToken) { const string funcName = nameof(CsvToJsonAction); var jobId = JobId; try { Data.File fileJson = new() { FileName = "file.json" }; var dataSourceId = More.GetValue("dataSourceId", 0); var sourceFileId = More.GetValue("sourceFileId"); var header = await DbHelper.FromProcAsync <string>("api.ClientDataSources_Get_Header", dataSourceId, cancellationToken : cancellationToken); var csv = string.Empty; if (Guid.TryParse(sourceFileId, out var guidCsv)) { var fileCsv = await DbHelper.FromProcAsync <Data.File>("WJbFiles_Get", guidCsv.ToString(), cancellationToken : cancellationToken); ArgumentNullException.ThrowIfNull(fileCsv); ArgumentNullException.ThrowIfNull(fileCsv.FileContent); csv = Encoding.UTF8.GetString(fileCsv.FileContent); if (!string.IsNullOrEmpty(fileCsv.FileName)) { fileJson.FileName = Path.ChangeExtension(fileCsv.FileName, ".json"); } } if (!string.IsNullOrEmpty(header) && !csv.StartsWith(header, StringComparison.CurrentCultureIgnoreCase)) { csv = header + "\r\n" + csv; } Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(csv)); using TextReader txtReader = new StreamReader(stream); using var csvReader = new CsvReader(txtReader, CultureInfo.InvariantCulture); var records = csvReader.GetRecords <dynamic>(); var json = JsonSerializer.Serialize(records); var fileId = await DbHelper.FromProcAsync <Guid?>("WJbFiles_Ins", new { fileJson.FileName, FileContent = Encoding.UTF8.GetBytes(json) }, cancellationToken : cancellationToken); await DbHelper.ExecProcAsync("api.ConnFiles_Ins_Json", new { DataSourceId = dataSourceId, SourceFileId = sourceFileId, JsonFileId = fileId }, cancellationToken : cancellationToken); return(true); } catch (Exception ex) { await LogHelper.LogErrorAsync(funcName, new { jobId, errMsg = ex.Message }, cancellationToken); return(false); } }
public override async Task <bool> ExecuteAsync(CancellationToken cancellationToken) { const string funcName = nameof(SftpGetFilesAction); var jobId = JobId; try { var dataSourceId = More.GetValue("dataSourceId", 0); var sftp_options = await DbHelper.FromProcAsync <SftpOptions>("api.FtpSettings_Get", $"FtpSource{dataSourceId}", cancellationToken : cancellationToken); ArgumentNullException.ThrowIfNull(sftp_options); var remote_path = More.GetValue("remote_path"); ArgumentNullException.ThrowIfNull(remote_path); var remove = More.GetValue("remove", false) ?? false; var timeout = More.GetValue("timeout", 60) ?? 60; using (SftpClient client = new(sftp_options.Host, sftp_options.Port, sftp_options.UserName, sftp_options.Password)) { try { if (!string.IsNullOrEmpty(sftp_options?.FingerPrint)) { // string hexFingerPrint; client.HostKeyReceived += delegate(object sender, HostKeyEventArgs e) { var bytes = sftp_options.FingerPrint.Split(':').Select(s => Convert.ToByte(s, 16)).ToArray(); if (e.FingerPrint.SequenceEqual(bytes)) { e.CanTrust = true; } else { e.CanTrust = false; } //var fingerPrint = new StringBuilder(); //foreach (var b in e.FingerPrint) fingerPrint.AppendFormat("{0:x2}", b); //hexFingerPrint = String.Join(':', fingerPrint); }; } client.Connect(); if (!client.IsConnected) { throw new Exception("Can't connect."); } var files = client.ListDirectory(remote_path); var expired = DateTime.Now.AddSeconds(timeout); foreach (var file in files.Where(e => e.IsRegularFile).OrderBy(o => o.LastWriteTime)) { if (cancellationToken.IsCancellationRequested) { break; } try { string remoteFile = $"{remote_path}/{file.Name}"; //, localFile = Path.Combine(local_path, clientFile.Name); // var content = client.ReadAllBytes(remoteFile); MemoryStream stream = new(); client.DownloadFile(remoteFile, stream); var fileId = await DbHelper.FromProcAsync <Guid?>("WJbFiles_Ins", new { FileName = file.Name, FileContent = stream.ToArray() }, cancellationToken : cancellationToken); // save ConnFile await DbHelper.ExecProcAsync("api.ConnFiles_Ins_Source", new { ClientDataSourceId = dataSourceId, SourceFileId = fileId }, cancellationToken : cancellationToken); if (remove) { client.DeleteFile(remoteFile); } await LogHelper.LogInformationAsync(funcName, new { jobId, errMsg = $"Downloaded: {file.Name}" }, cancellationToken); } catch (Exception ex) { await LogHelper.LogErrorAsync(funcName, new { jobId, errMsg = $"Fail: {file.Name}. Error: {ex.Message}" }, cancellationToken); throw; } if (expired < DateTime.Now) { break; } } } catch (Exception ex) { await LogHelper.LogErrorAsync(funcName, new { jobId, errMsg = $"Error: {ex.Message}" }, cancellationToken); throw; } finally { client.Disconnect(); } } return(true); } catch (Exception ex) { await LogHelper.LogErrorAsync(funcName, new { jobId, errMsg = ex.Message }, cancellationToken); return(false); } }