Пример #1
0
        public async Task InitializeAsync(CancellationToken ct)
        {
            var subcriptionName = new SubscriptionName(options.ProjectId, $"{options.Prefix}{topicId}");

            subscriberClient = await SubscriberClient.CreateAsync(subcriptionName);

            subscriberClient.StartAsync(async(pubSubMessage, subscriberToken) =>
            {
                try
                {
                    var message = serializer.Deserialize <T>(pubSubMessage.Data.Span);

                    await consumer.HandleAsync(message, subscriberToken);
                }
                catch (JsonException ex)
                {
                    log.LogError(ex, w => w
                                 .WriteProperty("action", "ConsumeMessage")
                                 .WriteProperty("system", "GooglePubSub")
                                 .WriteProperty("status", "Failed")
                                 .WriteProperty("reason", "Failed to deserialize from JSON."));
                }
                catch (Exception ex)
                {
                    log.LogError(ex, w => w
                                 .WriteProperty("action", "ConsumeMessage")
                                 .WriteProperty("system", "GooglePubSub")
                                 .WriteProperty("status", "Failed"));
                }

                return(SubscriberClient.Reply.Ack);
            }).Forget();
        }
Пример #2
0
        private async Task <bool> MakeIdentityOperation(Func <Task <IdentityResult> > action, [CallerMemberName] string operationName = null)
        {
            try
            {
                var result = await action();

                if (!result.Succeeded)
                {
                    var errorMessageBuilder = new StringBuilder();

                    foreach (var error in result.Errors)
                    {
                        errorMessageBuilder.Append(error.Code);
                        errorMessageBuilder.Append(": ");
                        errorMessageBuilder.AppendLine(error.Description);
                    }

                    log.LogError(w => w
                                 .WriteProperty("action", operationName)
                                 .WriteProperty("status", "Failed")
                                 .WriteProperty("message", errorMessageBuilder.ToString()));
                }

                return(result.Succeeded);
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("action", operationName)
                             .WriteProperty("status", "Failed"));

                return(false);
            }
        }
Пример #3
0
        private void Consume(object?parameter)
        {
            var config = (ConsumerConfig)parameter !;

            var kafkaConsumer =
                new ConsumerBuilder <string, T>(config)
                .SetLogHandler(KafkaLogFactory <string, T> .ConsumerLog(log))
                .SetErrorHandler(KafkaLogFactory <string, T> .ConsumerError(log))
                .SetStatisticsHandler(KafkaLogFactory <string, T> .ConsumerStats(log))
                .SetValueDeserializer(new KafkaJsonSerializer <T>(serializer))
                .Build();

            using (kafkaConsumer)
            {
                kafkaConsumer.Subscribe(topicName);

                try
                {
                    while (!cancellationTokenSource.IsCancellationRequested)
                    {
                        try
                        {
                            var result = kafkaConsumer.Consume(cancellationTokenSource.Token);

                            consumer.HandleAsync(result.Message.Value, cancellationTokenSource.Token).Wait();
                        }
                        catch (OperationCanceledException)
                        {
                            return;
                        }
                        catch (JsonException ex)
                        {
                            log.LogError(ex, w => w
                                         .WriteProperty("action", "ConsumeMessage")
                                         .WriteProperty("system", "Kafka")
                                         .WriteProperty("status", "Failed")
                                         .WriteProperty("reason", "Failed to deserialize from JSON.")
                                         .WriteProperty("topic", topicName));
                        }
                        catch (Exception ex)
                        {
                            log.LogError(ex, w => w
                                         .WriteProperty("action", "ConsumeMessage")
                                         .WriteProperty("system", "Kafka")
                                         .WriteProperty("status", "Failed")
                                         .WriteProperty("topic", topicName));
                        }
                    }
                }
                finally
                {
                    kafkaConsumer.Close();
                }
            }
        }
Пример #4
0
 private async Task DumpAsync(InvocationResponse input)
 {
     try
     {
         await webhookRepository.AddInvokationAsync(input.Webhook.Id, input.Dump, input.Result, input.Elapsed);
     }
     catch (Exception ex)
     {
         log.LogError(ex, w => w
                      .WriteProperty("action", "DumpHook")
                      .WriteProperty("status", "Failed"));
     }
 }
Пример #5
0
 private async Task QueryAsync(CancellationToken cancellationToken)
 {
     try
     {
         await webhookEventRepository.QueryPendingAsync(blockBlock.SendAsync, cancellationToken);
     }
     catch (Exception ex)
     {
         log.LogError(ex, w => w
                      .WriteProperty("action", "QueueWebhookEvents")
                      .WriteProperty("status", "Failed"));
     }
 }
Пример #6
0
 private async Task CleanupBackupAsync(BackupStateJob job)
 {
     try
     {
         await assetStore.DeleteAsync(job.Id.ToString(), 0, null);
     }
     catch (Exception ex)
     {
         log.LogError(ex, w => w
                      .WriteProperty("action", "deleteBackup")
                      .WriteProperty("status", "failed")
                      .WriteProperty("backupId", job.Id.ToString()));
     }
 }
Пример #7
0
        private async Task QueryAsync(CancellationToken cancellationToken)
        {
            try
            {
                var now = clock.GetCurrentInstant();

                await ruleEventRepository.QueryPendingAsync(now, blockBlock.SendAsync, cancellationToken);
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("action", "QueueWebhookEvents")
                             .WriteProperty("status", "Failed"));
            }
        }
Пример #8
0
        public async Task QueryAsync()
        {
            try
            {
                var now = clock.GetCurrentInstant();

                await ruleEventRepository.QueryPendingAsync(now, requestBlock.SendAsync);
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("action", "QueueWebhookEvents")
                             .WriteProperty("status", "Failed"));
            }
        }
Пример #9
0
        public async Task StopAsync(CancellationToken cancellationToken)
        {
            foreach (var initializable in initializables.OrderBy(x => x.InitializationOrder))
            {
                try
                {
                    log.LogDebug(w => w
                                 .WriteProperty("action", "ReleaseService")
                                 .WriteProperty("status", "Started")
                                 .WriteProperty("service", initializable.GetType().FullName));

                    await initializable.ReleaseAsync(cancellationToken);

                    log.LogInformation(w => w
                                       .WriteProperty("action", "ReleaseService")
                                       .WriteProperty("status", "Finished")
                                       .WriteProperty("service", initializable.ToString()));
                }
                catch (Exception ex)
                {
                    log.LogError(ex, w => w
                                 .WriteProperty("action", "ReleaseService")
                                 .WriteProperty("status", "Failed")
                                 .WriteProperty("service", initializable.ToString()));
                }
            }
        }
Пример #10
0
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            if (context.Request.Query.TryGetValue("error", out var header) && int.TryParse(header, out var statusCode) && IsErrorStatusCode(statusCode))
            {
                var(error, _) = ApiExceptionConverter.ToErrorDto(statusCode, context);

                await WriteErrorAsync(context, error);

                return;
            }

            try
            {
                await next(context);
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w.WriteProperty("message", "An unexpected exception has occurred."));

                if (!context.Response.HasStarted)
                {
                    var(error, _) = ex.ToErrorDto(context);

                    await WriteErrorAsync(context, error);
                }
            }

            if (IsErrorStatusCode(context.Response.StatusCode) && !context.Response.HasStarted)
            {
                var(error, _) = ApiExceptionConverter.ToErrorDto(context.Response.StatusCode, context);

                await WriteErrorAsync(context, error);
            }
        }
Пример #11
0
        public static async Task Throw(this Task <IdentityResult> task, ISemanticLog log)
        {
            var result = await task;

            if (!result.Succeeded)
            {
                var errorMessageBuilder = new StringBuilder();

                foreach (var error in result.Errors)
                {
                    errorMessageBuilder.Append(error.Code);
                    errorMessageBuilder.Append(": ");
                    errorMessageBuilder.AppendLine(error.Description);
                }

                var errorMessage = errorMessageBuilder.ToString();

                log.LogError(errorMessage, (ctx, w) => w
                             .WriteProperty("action", "IdentityOperation")
                             .WriteProperty("status", "Failed")
                             .WriteProperty("message", ctx));

                throw new ValidationException(result.Errors.Select(x => new ValidationError(x.Description)).ToList());
            }
        }
Пример #12
0
        private async Task SendEmailAsync(string template, string emailSubj, string emailBody, IUser user, TemplatesVars vars)
        {
            if (string.IsNullOrWhiteSpace(emailBody))
            {
                LogWarning($"No email subject configured for {template}");
                return;
            }

            if (string.IsNullOrWhiteSpace(emailSubj))
            {
                LogWarning($"No email body configured for {template}");
                return;
            }

            vars.URL = urlGenerator.UI();

            vars.User = user;

            emailSubj = Format(emailSubj, vars);
            emailBody = Format(emailBody, vars);

            try
            {
                await emailSender.SendAsync(user.Email, emailSubj, emailBody);
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("action", "SendNotification")
                             .WriteProperty("status", "Failed"));

                throw;
            }
        }
Пример #13
0
        protected override async Task StartAsync(ISemanticLog log, CancellationToken ct)
        {
            IdentityModelEventSource.ShowPII = identityOptions.ShowPII;

            if (identityOptions.IsAdminConfigured())
            {
                using (var scope = serviceProvider.CreateScope())
                {
                    var userManager = scope.ServiceProvider.GetRequiredService <UserManager <IdentityUser> >();
                    var userFactory = scope.ServiceProvider.GetRequiredService <IUserFactory>();

                    var adminEmail = identityOptions.AdminEmail;
                    var adminPass  = identityOptions.AdminPassword;

                    var isEmpty = IsEmpty(userManager);

                    if (isEmpty || identityOptions.AdminRecreate)
                    {
                        try
                        {
                            var user = await userManager.FindByEmailWithClaimsAsync(adminEmail);

                            if (user != null)
                            {
                                if (identityOptions.AdminRecreate)
                                {
                                    var permissions = user.Permissions().Add(Permissions.Admin);

                                    var values = new UserValues
                                    {
                                        Password    = adminPass,
                                        Permissions = permissions
                                    };

                                    await userManager.UpdateAsync(user.Identity, values);
                                }
                            }
                            else
                            {
                                var values = new UserValues
                                {
                                    Email       = adminEmail,
                                    Password    = adminPass,
                                    Permissions = new PermissionSet(Permissions.Admin),
                                    DisplayName = adminEmail
                                };

                                await userManager.CreateAsync(userFactory, values);
                            }
                        }
                        catch (Exception ex)
                        {
                            log.LogError(ex, w => w
                                         .WriteProperty("action", "createAdmin")
                                         .WriteProperty("status", "failed"));
                        }
                    }
                }
            }
        }
Пример #14
0
        public async Task InvokeAsync(HttpContext context, IActionResultExecutor <ObjectResult> writer, ISemanticLog log)
        {
            if (TryGetErrorCode(context, out var statusCode) && IsErrorStatusCode(statusCode))
            {
                var(error, _) = ApiExceptionConverter.ToErrorDto(statusCode, context);

                await WriteErrorAsync(context, error, writer);

                return;
            }

            try
            {
                await next(context);
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w.WriteProperty("message", "An unexpected exception has occurred."));

                if (!context.Response.HasStarted)
                {
                    var(error, _) = ex.ToErrorDto(context);

                    await WriteErrorAsync(context, error, writer);
                }
            }

            if (IsErrorStatusCode(context.Response.StatusCode) && !context.Response.HasStarted)
            {
                var(error, _) = ApiExceptionConverter.ToErrorDto(context.Response.StatusCode, context);

                await WriteErrorAsync(context, error, writer);
            }
        }
        private async Task TryPublishAsync(IContentEntity content)
        {
            var id = content.Id;

            try
            {
                var job = content.ScheduleJob;

                if (job != null)
                {
                    var command = new ChangeContentStatus
                    {
                        Actor       = job.ScheduledBy,
                        AppId       = content.AppId,
                        ContentId   = id,
                        SchemaId    = content.SchemaId,
                        Status      = job.Status,
                        StatusJobId = job.Id
                    };

                    await commandBus.PublishAsync(command);
                }
            }
            catch (DomainObjectNotFoundException)
            {
                await contentRepository.ResetScheduledAsync(content.UniqueId, default);
            }
            catch (Exception ex)
            {
                log.LogError(ex, content.Id.ToString(), (logContentId, w) => w
                             .WriteProperty("action", "ChangeStatusScheduled")
                             .WriteProperty("status", "Failed")
                             .WriteProperty("contentId", logContentId));
            }
        }
Пример #16
0
        public Task PublishAsync()
        {
            var now = clock.GetCurrentInstant();

            return(contentRepository.Value.QueryScheduledWithoutDataAsync(now, content =>
            {
                return Dispatch(async() =>
                {
                    try
                    {
                        var job = content.ScheduleJob;

                        if (job != null)
                        {
                            var command = new ChangeContentStatus {
                                ContentId = content.Id, Status = job.Status, Actor = job.ScheduledBy, JobId = job.Id
                            };

                            await commandBus.Value.PublishAsync(command);
                        }
                    }
                    catch (Exception ex)
                    {
                        log.LogError(ex, content.Id.ToString(), (logContentId, w) => w
                                     .WriteProperty("action", "ChangeStatusScheduled")
                                     .WriteProperty("status", "Failed")
                                     .WriteProperty("contentId", logContentId));
                    }
                });
            }));
        }
Пример #17
0
        public void Publish(string token, bool notifySelf)
        {
            try
            {
                var message = string.Join("#", (notifySelf ? Guid.Empty : InstanceId).ToString(), token);

                subscriber.Publish(channelName, message);
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("action", "PublishRedisMessage")
                             .WriteProperty("state", "Failed")
                             .WriteProperty("token", token));
            }
        }
Пример #18
0
        public async Task <IdentityResult> ValidateAsync(UserManager <IdentityUser> manager, IdentityUser user, string password)
        {
            if (string.IsNullOrWhiteSpace(password))
            {
                return(IdentityResult.Success);
            }

            try
            {
                var isBreached = await client.IsPasswordPwned(password);

                if (isBreached)
                {
                    var errorText = T.Get("security.passwordStolen");

                    return(IdentityResult.Failed(new IdentityError {
                        Code = "PwnedError", Description = errorText
                    }));
                }
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("operation", "CheckPasswordPwned")
                             .WriteProperty("status", "Failed"));
            }

            return(IdentityResult.Success);
        }
Пример #19
0
        private async Task ProcessAsync(State currentState, CancellationToken ct)
        {
            try
            {
                currentReminder = await RegisterOrUpdateReminder("KeepAlive", TimeSpan.Zero, TimeSpan.FromMinutes(2));

                var rules = await appProvider.GetRulesAsync(DomainId.Create(Key));

                var rule = rules.Find(x => x.Id == currentState.RuleId);

                if (rule == null)
                {
                    throw new InvalidOperationException("Cannot find rule.");
                }

                using (localCache.StartContext())
                {
                    if (currentState.FromSnapshots && ruleService.CanCreateSnapshotEvents(rule.RuleDef))
                    {
                        await EnqueueFromSnapshotsAsync(rule);
                    }
                    else
                    {
                        await EnqueueFromEventsAsync(currentState, rule, ct);
                    }
                }
            }
            catch (OperationCanceledException)
            {
                return;
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("action", "runRule")
                             .WriteProperty("status", "failed")
                             .WriteProperty("ruleId", currentState.RuleId?.ToString()));
            }
            finally
            {
                if (!isStopping)
                {
                    currentState.RuleId   = null;
                    currentState.Position = null;

                    await state.WriteAsync();

                    if (currentReminder != null)
                    {
                        await UnregisterReminder(currentReminder);

                        currentReminder = null;
                    }

                    currentJobToken?.Dispose();
                    currentJobToken = null;
                }
            }
        }
Пример #20
0
 private void Log(IIncomingGrainCallContext context, Exception ex)
 {
     log.LogError(ex, w => w
                  .WriteProperty("action", "GrainInvoked")
                  .WriteProperty("status", "Failed")
                  .WriteProperty("grain", context.Grain.ToString())
                  .WriteProperty("grainMethod", context.ImplementationMethod.ToString()));
 }
Пример #21
0
        private async Task UpsertUserAsync(IUser user)
        {
            if (client == null)
            {
                return;
            }

            try
            {
                var settings = new Dictionary <string, NotificationSettingDto>
                {
                    [Providers.WebPush] = new NotificationSettingDto
                    {
                        Send           = NotificationSend.Send,
                        DelayInSeconds = null
                    },

                    [Providers.Email] = new NotificationSettingDto
                    {
                        Send           = NotificationSend.Send,
                        DelayInSeconds = 5 * 60
                    }
                };

                var userRequest = new UpsertUserDto
                {
                    Id                = user.Id,
                    FullName          = user.Claims.DisplayName(),
                    PreferredLanguage = "en",
                    PreferredTimezone = null,
                    Settings          = settings
                };

                if (user.Email.IsEmail())
                {
                    userRequest.EmailAddress = user.Email;
                }

                var response = await client.Users.PostUsersAsync(options.AppId, new UpsertUsersDto
                {
                    Requests = new List <UpsertUserDto>
                    {
                        userRequest
                    }
                });

                var apiKey = response.First().ApiKey;

                await userResolver.SetClaimAsync(user.Id, SquidexClaimTypes.NotifoKey, response.First().ApiKey, true);
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("action", "RegisterToNotifo")
                             .WriteProperty("status", "Failed"));
            }
        }
Пример #22
0
        public async Task <IActionResult> GetUsers(string query)
        {
            try
            {
                var users = await userResolver.QueryByEmailAsync(query);

                var response = users.Where(x => !x.IsHidden()).Select(x => UserDto.FromUser(x, this)).ToArray();

                return(Ok(response));
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("action", nameof(GetUsers))
                             .WriteProperty("status", "Failed"));
            }

            return(Ok(new UserDto[0]));
        }
Пример #23
0
        public void Publish(object value, bool notifySelf)
        {
            try
            {
                var senderId = notifySelf ? Guid.Empty : selfId;

                var envelope = JsonConvert.SerializeObject(new Envelope {
                    Sender = senderId, Payload = (T)value
                });

                subscriber.Publish(channelName, envelope);
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("action", "PublishRedisMessage")
                             .WriteProperty("status", "Failed")
                             .WriteProperty("channel", channelName));
            }
        }
Пример #24
0
        private async Task ExecuteCommandAsync(BulkTaskCommand bulkCommand)
        {
            var(task, id, command) = bulkCommand;
            try
            {
                await task.Bus.PublishAsync(command);

                task.Results.Add(new BulkUpdateResultItem(id, task.JobIndex));
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("action", "BulkContent")
                             .WriteProperty("status", "Failed")
                             .WriteProperty("jobIndex", task.JobIndex)
                             .WriteProperty("jobType", task.CommandJob.Type.ToString()));

                task.Results.Add(new BulkUpdateResultItem(id, task.JobIndex, ex));
            }
        }
Пример #25
0
        private async Task <Envelope <IEvent> > HandleAsync(Envelope <IEvent> input)
        {
            var eventNumber = input.Headers.EventNumber();

            if (eventNumber <= lastReceivedEventNumber || !isRunning)
            {
                return(null);
            }

            var consumerName = eventConsumer.Name;

            var eventId   = input.Headers.EventId().ToString();
            var eventType = input.Payload.GetType().Name;

            try
            {
                log.LogInformation(w => w
                                   .WriteProperty("action", "HandleEvent")
                                   .WriteProperty("actionId", eventId)
                                   .WriteProperty("state", "Started")
                                   .WriteProperty("eventId", eventId)
                                   .WriteProperty("eventType", eventType)
                                   .WriteProperty("eventConsumer", consumerName));

                await eventConsumer.On(input);

                log.LogInformation(w => w
                                   .WriteProperty("action", "HandleEvent")
                                   .WriteProperty("actionId", eventId)
                                   .WriteProperty("state", "Completed")
                                   .WriteProperty("eventId", eventId)
                                   .WriteProperty("eventType", eventType)
                                   .WriteProperty("eventConsumer", consumerName));

                lastReceivedEventNumber = eventNumber;

                return(input);
            }
            catch (Exception ex)
            {
                OnError?.Invoke(ex);

                log.LogError(ex, w => w
                             .WriteProperty("action", "HandleEvent")
                             .WriteProperty("actionId", eventId)
                             .WriteProperty("state", "Started")
                             .WriteProperty("eventId", eventId)
                             .WriteProperty("eventType", eventType)
                             .WriteProperty("eventConsumer", consumerName));

                return(null);
            }
        }
Пример #26
0
 public async Task SendAsync(UserNotification notification, NotificationSetting setting, User user, App app, bool isUpdate, CancellationToken ct = default)
 {
     try
     {
         await streamClient.SendAsync(notification);
     }
     catch (Exception ex)
     {
         log.LogError(ex, w => w
                      .WriteProperty("action", "SendWeb")
                      .WriteProperty("status", "Failed"));
     }
 }
Пример #27
0
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            try
            {
                await next(context);
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w.WriteProperty("messag", "An unexpected exception has occurred."));

                context.Response.StatusCode = 500;
            }
        }
Пример #28
0
        private async Task ExecuteInlineAsync(string key, T job)
        {
            try
            {
                if (currentHandler != null)
                {
                    await currentHandler.HandleAsync(new List <T> {
                        job
                    }, false, default);
                }
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("action", "HandleJob")
                             .WriteProperty("status", "Failed"));

                var nextTime = Now.Plus(Duration.FromMinutes(5));

                await schedulerStore.EnqueueScheduledAsync(key, job, nextTime, 1);
            }
        }
Пример #29
0
 public static async Task DeleteAsync(IAssetStore assetStore, string id, ISemanticLog log)
 {
     try
     {
         await assetStore.DeleteAsync(id, 0, null);
     }
     catch (Exception ex)
     {
         log.LogError(ex, id, (logOperationId, w) => w
                      .WriteProperty("action", "deleteBackup")
                      .WriteProperty("status", "failed")
                      .WriteProperty("operationId", logOperationId));
     }
 }
Пример #30
0
 public static async Task CleanupRestoreErrorAsync(BackupHandler handler, Guid appId, Guid id, ISemanticLog log)
 {
     try
     {
         await handler.CleanupRestoreErrorAsync(appId);
     }
     catch (Exception ex)
     {
         log.LogError(ex, id.ToString(), (logOperationId, w) => w
                      .WriteProperty("action", "cleanupRestore")
                      .WriteProperty("status", "failed")
                      .WriteProperty("operationId", logOperationId));
     }
 }