📄
SendSmsJob.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.Diagnostics; using BfiMonitor; using HuaweiWifiSms.Grpc; using Microsoft.Extensions.Logging; using Quartz; internal sealed class SendSmsJob(SmsSender.SmsSenderClient smsSender, ILogger<SendSmsJob> logger) : IJob { public static JobKey Key { get; } = JobKey.Create(nameof(SendSmsJob)); public async Task Execute(IJobExecutionContext context) { var phoneNumber = context.MergedJobDataMap.GetString("phoneNumber")!; var message = context.MergedJobDataMap.GetString("message")!; using var activity = Tracing.StartSendSms(phoneNumber); try { var response = await smsSender.SendSmsAsync( new SmsRequest { RecipientPhoneNumber = phoneNumber, Content = message }, cancellationToken: context.CancellationToken ); if (response.Status is SmsStatus.Success) { logger.LogSmsSent(phoneNumber); activity?.SetStatus(ActivityStatusCode.Ok); } else { logger.LogSmsFailed(phoneNumber, response.Status); activity?.SetStatus(ActivityStatusCode.Error, $"Failure response {response.Status}"); } } catch (Exception ex) { activity?.SetStatus(ActivityStatusCode.Error, ex.Message); activity?.AddException(ex); throw; } } } internal static partial class SendSmsJobLoggerExtensions { [LoggerMessage(LogLevel.Information, "Successfully sent SMS to {PhoneNumber}")] public static partial void LogSmsSent(this ILogger logger, string phoneNumber); [LoggerMessage(LogLevel.Error, "Failed to send SMS to {PhoneNumber} with status {SmsStatus}")] public static partial void LogSmsFailed(this ILogger logger, string phoneNumber, SmsStatus smsStatus); }