public Task <bool> DeleteAsync(BackgroundTask task)
        {
            var db = GetDbConnection();
            var t  = BeginTransaction((SqlConnection)db, out var owner);

            var success = false;

            try
            {
                var sql     = $@"
-- Primary relationship:
DELETE FROM {TagsTable} WHERE BackgroundTaskId = @Id;
DELETE FROM {TaskTable} WHERE Id = @Id;

-- Remove any orphaned tags:
DELETE FROM {TagTable}
WHERE NOT EXISTS (SELECT 1 FROM {TagsTable} st WHERE {TagTable}.Id = st.TagId)
";
                var deleted = db.Execute(sql, task, t);
                if (deleted != 1)
                {
                    _logger.Warn(() => "Task with ID {Id} was not deleted", task.Id);
                }

                success = true;
            }
            catch (Exception e)
            {
                _logger.Error(() => "Error deleting task with ID {Id}", e, task.Id);
            }
            finally
            {
                if (owner)
                {
                    if (success)
                    {
                        CommitTransaction(t);
                    }
                    else
                    {
                        RollbackTransaction(t);
                    }
                }
            }

            return(Task.FromResult(success));
        }
        public async Task <bool> DeleteAsync(BackgroundTask task)
        {
            var document = await _repository.RetrieveSingleOrDefaultAsync(x => x.TaskId == task.Id);

            if (document == null)
            {
                _logger.Warn(() => "Could not delete task with ID {Id} as it was not found.", task.Id);
                return(false);
            }

            var deleted = await _repository.DeleteAsync(document.Id);

            if (!deleted)
            {
                _logger.Warn(() => "Could not delete task with ID {Id} successfully.", task.Id);
                return(false);
            }

            return(true);
        }
Esempio n. 3
0
File: Add.cs Progetto: qiqi545/HQ
        public static IServiceCollection AddHq(this IServiceCollection services, IWebHostEnvironment env,
                                               IConfiguration config, ISafeLogger logger)
        {
            var subject = Assembly.GetCallingAssembly();

            if (!(config is IConfigurationRoot configRoot))
            {
                throw new ArgumentException("HQ requires access to the root configuration.", nameof(config));
            }

            services.TryAddSingleton(configRoot);
            services.TryAddSingleton(services);

            var hq = configRoot.GetSection("HQ");

            //
            // Core Services:
            services.AddTypeResolver();
            services.AddLocalTimestamps();
            services.AddSafeLogging();
            services.AddValidOptions();
            services.AddTraceContext();

            //
            // Platform Services:
            services.AddSecurityPolicies(hq.GetSection("Security"), hq.GetSection("SuperUser"), logger);
            services.AddVersioning(hq.GetSection("Versioning"));
            services.AddMultiTenancy <IdentityTenant, IdentityApplication>(hq.GetSection("MultiTenancy"))
            .AddIdentityTenantContextStore <IdentityTenant>()
            .AddIdentityApplicationContextStore <IdentityApplication>();

            //
            // Platform APIs:
            services
            .AddOperationsApi(hq.GetSection("Ops"))
            .AddPlatformApi(hq.GetSection("Api"))
            .AddBackgroundTasksApi(hq.GetSection("BackgroundTasks"))
            .AddConfigurationApi(configRoot, hq.GetSection("Configuration"))
            .AddIdentityApi(hq.GetSection("IdentityApi"))
            .AddSchemaApi(hq.GetSection("Schema"))
            .AddMetaApi(hq.GetSection("Meta"));

            var tasksBuilder    = services.AddBackgroundTasks(hq.GetSection("BackgroundTasks"));
            var identityBuilder = services.AddIdentityExtended(hq.GetSection("Identity"));
            var runtimeBuilder  = services.AddRuntimeApi(hq.GetSection("Runtime"));
            var schemaBuilder   = services.AddSchemaDiscovery(hq.GetSection("Schema"));

            //
            // Cloud:
            var cloud = configRoot.GetSection("Cloud");

            switch (cloud["Provider"])
            {
            case nameof(Azure):
            {
                var options = new AzureOptions();
                cloud.FastBind(options);
                services.AddCloudServices(logger, options);
                break;
            }
            }

            //
            // Backend Services:
            var backend  = configRoot.GetSection("Backend");
            var dbConfig = backend.GetSection("DbOptions");

            if (dbConfig?.Value == null)
            {
                dbConfig = null;
            }

            var backendType = backend["Type"];

            if (string.IsNullOrWhiteSpace(backendType))
            {
                logger.Warn(() => "No backend type found!");
            }
            else
            {
                logger.Info(() => "Installing {BackendType} back-end services.", backendType);
            }

            switch (backendType)
            {
            case nameof(DocumentDb):
                tasksBuilder.AddDocumentDbBackgroundTaskStore(backend.GetConnectionString("Tasks"));
                identityBuilder.AddDocumentDbIdentityStore <IdentityUserExtended, IdentityRoleExtended, IdentityTenant, IdentityApplication>(backend.GetConnectionString("Identity"));
                runtimeBuilder.AddDocumentDbRuntimeStores(backend.GetConnectionString("Runtime"), ConnectionScope.ByRequest, dbConfig);
                schemaBuilder.AddDocumentDbSchemaStores(backend.GetConnectionString("Schema"));
                break;

            case nameof(SqlServer):
                tasksBuilder.AddSqlServerBackgroundTasksStore(backend.GetConnectionString("Tasks"), r => r.GetRequiredService <IServerTimestampService>().GetCurrentTime());
                identityBuilder.AddSqlServerIdentityStore <IdentityUserExtended, IdentityRoleExtended, IdentityTenant, IdentityApplication>(backend.GetConnectionString("Identity"), ConnectionScope.ByRequest, dbConfig);
                runtimeBuilder.AddSqlServerRuntime(backend.GetConnectionString("Runtime"), ConnectionScope.ByRequest, dbConfig);
                schemaBuilder.AddSqlServerSchemaStores();
                break;

            case nameof(Sqlite):
                tasksBuilder.AddSqliteBackgroundTasksStore(backend.GetConnectionString("Tasks"));
                identityBuilder.AddSqliteIdentityStore <IdentityUserExtended, IdentityRoleExtended, IdentityTenant, IdentityApplication>(backend.GetConnectionString("Identity"), ConnectionScope.ByRequest, dbConfig);
                runtimeBuilder.AddSqliteRuntime(backend.GetConnectionString("Runtime"), ConnectionScope.ByRequest, dbConfig);
                schemaBuilder.AddSqliteSchemaStores();
                break;

            default:
                throw new ArgumentOutOfRangeException(backendType, typeof(string), null);
            }

            //
            // Runtime Services:
            {
                var runtimeOptions = new RuntimeOptions();
                hq.GetSection("Runtime").FastBind(runtimeOptions);

                if (runtimeOptions.EnableRest)
                {
                    services.AddRestRuntime();
                    logger.Info(() => "REST is enabled.");
                }

                if (runtimeOptions.EnableGraphQl)
                {
                    services.AddGraphQlRuntime();
                    logger.Info(() => "GraphQL is enabled.");
                }
            }

            //
            // Notification Services:
            services.AddEmailNotifications(hq.GetSection("Email"));

            //
            // Media Services:

            //
            // Custom Objects:
            services.ScanForGeneratedObjects(backendType, hq.GetSection("Security"), logger, "/api", subject);

            return(services);
        }