Example #1
0
        public Task SendAsync(WebhookJob job, bool isLastAttempt, CancellationToken ct)
        {
            return(log.ProfileAsync("SendWebhook", async() =>
            {
                var notification = job.Notification;

                try
                {
                    await UpdateAsync(notification, ProcessStatus.Attempt);

                    using (var client = httpClientFactory.CreateClient())
                    {
                        await client.PostAsJsonAsync(job.Url, job.Notification, ct);
                    }

                    await UpdateAsync(notification, ProcessStatus.Handled);
                }
                catch (Exception ex)
                {
                    if (isLastAttempt)
                    {
                        await UpdateAsync(notification, ProcessStatus.Failed);
                    }

                    await logStore.LogAsync(notification.AppId, $"Webhook: {ex.Message}");

                    throw;
                }
            }));
        }
Example #2
0
        public Task SendAsync(WebPushJob job, bool isLastAttempt, CancellationToken ct)
        {
            return(log.ProfileAsync("SendWebPush", async() =>
            {
                try
                {
                    await UpdateAsync(job, ProcessStatus.Attempt);

                    await SendAnyAsync(job, ct);

                    await UpdateAsync(job, ProcessStatus.Handled);
                }
                catch (Exception ex)
                {
                    if (isLastAttempt)
                    {
                        await UpdateAsync(job, ProcessStatus.Failed);
                    }

                    if (ex is DomainException domainException)
                    {
                        await logStore.LogAsync(job.AppId, domainException.Message);
                    }
                    else
                    {
                        throw;
                    }
                }
            }));
Example #3
0
        public Task SendAsync(SmsJob job, bool isLastAttempt, CancellationToken ct)
        {
            return(log.ProfileAsync("SendSms", async() =>
            {
                try
                {
                    await UpdateAsync(job, ProcessStatus.Attempt);

                    var result = await smsSender.SendAsync(job.PhoneNumber, job.Text, job.Id.ToString(), ct);

                    if (result == SmsResult.Delivered)
                    {
                        await UpdateAsync(job, ProcessStatus.Handled);
                    }
                }
                catch (Exception ex)
                {
                    if (isLastAttempt)
                    {
                        await UpdateAsync(job, ProcessStatus.Failed);
                    }

                    if (ex is DomainException domainException)
                    {
                        await logStore.LogAsync(job.AppId, domainException.Message);
                    }
                    else
                    {
                        throw;
                    }
                }
            }));
Example #4
0
        public Task SendAsync(List <UserNotification> notifications, bool isLastAttempt, CancellationToken ct)
        {
            return(log.ProfileAsync("SendEmail", async() =>
            {
                await UpdateAsync(notifications, ProcessStatus.Attempt);

                var first = notifications[0];

                var app = await appStore.GetCachedAsync(first.AppId, ct);

                if (app == null)
                {
                    log.LogWarning(w => w
                                   .WriteProperty("action", "SendEmail")
                                   .WriteProperty("status", "Failed")
                                   .WriteProperty("reason", "App not found"));

                    return;
                }

                try
                {
                    var user = await userStore.GetCachedAsync(first.AppId, first.UserId, ct);

                    if (user == null)
                    {
                        throw new DomainException(Texts.Email_UserNoEmail);
                    }

                    if (string.IsNullOrWhiteSpace(user.EmailAddress))
                    {
                        throw new DomainException(Texts.Email_UserNoEmail);
                    }

                    await SendAsync(notifications, app, user, ct);

                    await UpdateAsync(notifications, ProcessStatus.Handled);
                }
                catch (Exception ex)
                {
                    if (isLastAttempt)
                    {
                        await UpdateAsync(notifications, ProcessStatus.Failed);
                    }

                    if (ex is DomainException domainException)
                    {
                        await logStore.LogAsync(app.Id, domainException.Message);
                    }
                    else
                    {
                        throw;
                    }
                }
            }));
        public Task SendAsync(MobilePushJob job, bool isLastAttempt, CancellationToken ct)
        {
            return(log.ProfileAsync("SendMobilePush", async() =>
            {
                var notification = job.Notification;

                var app = await appStore.GetCachedAsync(notification.AppId, ct);

                if (app == null)
                {
                    log.LogWarning(w => w
                                   .WriteProperty("action", "SendMobilePush")
                                   .WriteProperty("status", "Failed")
                                   .WriteProperty("reason", "App not found"));

                    return;
                }

                try
                {
                    await UpdateAsync(notification, ProcessStatus.Attempt);

                    if (!IsFirebaseConfigured(app))
                    {
                        throw new DomainException(Texts.MobilePush_ConfigReset);
                    }

                    var messaging = pool.GetMessaging(app);

                    await SendAnyAsync(job, messaging, ct);

                    await UpdateAsync(notification, ProcessStatus.Handled);
                }
                catch (Exception ex)
                {
                    if (isLastAttempt)
                    {
                        await UpdateAsync(notification, ProcessStatus.Failed);
                    }

                    if (ex is DomainException domainException)
                    {
                        await logStore.LogAsync(app.Id, domainException.Message);
                    }
                    else
                    {
                        throw;
                    }
                }
            }));