Esempio n. 1
0
        public void Create([FromBody] TenantModel model)
        {
            if (!IsDefault())
            {
                throw this.Exception("非默认上下文禁止此操作");
            }

            if (ModelState.IsValid)
            {
                ValidateViewModel(model, true);
            }
            else
            {
                throw this.Exception(ModelState);
            }

            if (!ModelState.IsValid)
            {
                throw this.Exception(ModelState);
            }

            var engineSettings = new EngineSettings()
            {
                Name             = model.Name,
                RequestUrlPrefix = model.RequestUrlPrefix?.Trim(),
                RequestUrlHost   = model.RequestUrlHost,
                ConnectionString = model.ConnectionString,
                TablePrefix      = model.TablePrefix,
                DatabaseProvider = model.DatabaseProvider,
                State            = TenantStates.Uninitialized
            };

            _engineSettingsManager.SaveSettings(engineSettings);
            _engineHost.GetOrCreateEngineContext(engineSettings);
        }
Esempio n. 2
0
        public async Task Invoke(HttpContext httpContext)
        {
            await _next.Invoke(httpContext);

            var engineSettings = httpContext.Features.Get <EngineSettings>();

            if (engineSettings != null)
            {
                var deferredTaskEngine = httpContext.RequestServices.GetService <IDeferredTaskEngine>();

                if (deferredTaskEngine != null && deferredTaskEngine.HasPendingTasks)
                {
                    (httpContext.RequestServices as IDisposable).Dispose();

                    var engineContext = _engineHost.GetOrCreateEngineContext(engineSettings);

                    if (!engineContext.Released)
                    {
                        using (var scope = engineContext.EnterServiceScope())
                        {
                            await deferredTaskEngine.ExecuteTasksAsync(new DeferredTaskContext(scope.ServiceProvider));
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        private async void DoWorkAsync(object group)
        {
            EngineContext engineContext = _host.GetOrCreateEngineContext(_engineSettings);

            var groupName = group as string ?? "";

            foreach (var task in _tasks[groupName])
            {
                var taskName = task.GetType().FullName;

                using (var scope = engineContext.EnterServiceScope())
                {
                    try
                    {
                        if (_states[task] != BackgroundTaskState.Idle)
                        {
                            return;
                        }

                        lock (_states)
                        {
                            if (_states[task] != BackgroundTaskState.Idle)
                            {
                                return;
                            }

                            _states[task] = BackgroundTaskState.Running;
                        }

                        if (Logger.IsEnabled(LogLevel.Information))
                        {
                            Logger.LogInformation("Start processing background task '{BackgroundTaskName}'.", taskName);
                        }

                        await task.DoWorkAsync(scope.ServiceProvider, _applicationLifetime.ApplicationStopping);

                        if (Logger.IsEnabled(LogLevel.Information))
                        {
                            Logger.LogInformation("Finished processing background task '{BackgroundTaskName}'.", taskName);
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.LogError(ex, "Error while processing background task '{BackgroundTaskName}'", taskName);
                    }
                    finally
                    {
                        lock (_states)
                        {
                            if (_states[task] != BackgroundTaskState.Stopped)
                            {
                                _states[task] = BackgroundTaskState.Idle;
                            }
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        private async Task ExecuteStepAsync(ProjectExecutionContext projectStep)
        {
            var engineContext = _host.GetOrCreateEngineContext(_engineSettings);

            using (var scope = engineContext.EnterServiceScope())
            {
                if (!engineContext.IsActivated)
                {
                    var tenantEvents = scope.ServiceProvider.GetServices <IModuleTenantEvents>();

                    foreach (var tenantEvent in tenantEvents)
                    {
                        tenantEvent.ActivatingAsync().Wait();
                    }

                    engineContext.IsActivated = true;

                    foreach (var tenantEvent in tenantEvents)
                    {
                        tenantEvent.ActivatedAsync().Wait();
                    }
                }

                var projectStepHandlers = scope.ServiceProvider.GetServices <IProjectStepHandler>();
                //var scriptingManager = scope.ServiceProvider.GetRequiredService<IScriptingManager>();
                //scriptingManager.GlobalMethodProviders.Add(_environmentMethodProvider);

                //// Substitutes the script elements by their actual values
                //EvaluateScriptNodes(recipeStep, scriptingManager);

                foreach (var projectStepHandler in projectStepHandlers)
                {
                    if (_logger.IsEnabled(LogLevel.Information))
                    {
                        _logger.LogInformation("Executing project step '{0}'.", projectStep.Name);
                    }

                    await _projectEventHandlers.InvokeAsync(e => e.StepExecutingAsync(projectStep), _logger);

                    await projectStepHandler.ExecuteAsync(projectStep);

                    await _projectEventHandlers.InvokeAsync(e => e.StepExecutedAsync(projectStep), _logger);

                    if (_logger.IsEnabled(LogLevel.Information))
                    {
                        _logger.LogInformation("Finished executing project step '{0}'.", projectStep.Name);
                    }
                }
            }

            engineContext = _host.GetOrCreateEngineContext(_engineSettings);
            using (var scope = engineContext.EnterServiceScope())
            {
                var deferredTaskEngine = scope.ServiceProvider.GetService <IDeferredTaskEngine>();

                if (deferredTaskEngine != null && deferredTaskEngine.HasPendingTasks)
                {
                    await deferredTaskEngine.ExecuteTasksAsync(new DeferredTaskContext(scope.ServiceProvider));
                }
            }
        }
        public async Task Invoke(HttpContext httpContext)
        {
            _host.Initialize();

            var engineSettings = _runningEngineTable.Match(httpContext);

            if (engineSettings != null)
            {
                var engineContext = _host.GetOrCreateEngineContext(engineSettings);

                var hasPendingTasks = false;
                using (var scope = engineContext.EnterServiceScope())
                {
                    httpContext.Features.Set(engineContext);
                    // 把租户前缀加到 cookie 里
                    httpContext.Response.Cookies.Append("tenant_prefix", engineSettings.RequestUrlPrefix ?? "");
                    httpContext.Response.Cookies.Append("tenant_host", engineSettings.RequestUrlHost ?? "");

                    if (!engineContext.IsActivated)
                    {
                        var semaphore = _semaphores.GetOrAdd(engineSettings.Name, (name) => new SemaphoreSlim(1));

                        await semaphore.WaitAsync();

                        try
                        {
                            if (!engineContext.IsActivated)
                            {
                                using (var activatingScope = engineContext.EnterServiceScope())
                                {
                                    var tenantEvents = activatingScope.ServiceProvider.GetServices <IModuleTenantEvents>();

                                    foreach (var tenantEvent in tenantEvents)
                                    {
                                        await tenantEvent.ActivatingAsync();
                                    }

                                    httpContext.Items["BuildPipeline"] = true;

                                    foreach (var tenantEvent in tenantEvents.Reverse())
                                    {
                                        await tenantEvent.ActivatedAsync();
                                    }
                                }

                                engineContext.IsActivated = true;
                            }
                        }
                        finally
                        {
                            semaphore.Release();
                            _semaphores.TryRemove(engineSettings.Name, out semaphore);
                        }
                    }

                    await _next.Invoke(httpContext);

                    var deferredTaskEngine = scope.ServiceProvider.GetService <IDeferredTaskEngine>();
                    hasPendingTasks = deferredTaskEngine?.HasPendingTasks ?? false;
                }

                if (hasPendingTasks)
                {
                    engineContext = _host.GetOrCreateEngineContext(engineSettings);

                    using (var scope = engineContext.EnterServiceScope())
                    {
                        var deferredTaskEngine = scope.ServiceProvider.GetService <IDeferredTaskEngine>();
                        var context            = new DeferredTaskContext(scope.ServiceProvider);
                        await deferredTaskEngine.ExecuteTasksAsync(context);
                    }
                }
            }
        }