public List <CompilationErrorTS> Compile(bool inMemory) { SystemEventLogLogic.Log("DynamicController.Compile"); var compileResult = new List <DynamicLogic.CompilationResult>(); if (!inMemory) { DynamicLogic.CleanCodeGenFolder(); } Dictionary <string, CodeFile> codeFiles = DynamicLogic.GetCodeFilesDictionary(); compileResult.Add(DynamicLogic.Compile(codeFiles, inMemory: inMemory, assemblyName: DynamicCode.CodeGenAssembly, needsCodeGenAssembly: false)); if (DynamicApiLogic.IsStarted) { Dictionary <string, CodeFile> apiFiles = DynamicApiLogic.GetCodeFiles().ToDictionaryEx(a => a.FileName, "CodeGenController C# code file"); compileResult.Add(DynamicLogic.Compile(apiFiles, inMemory: inMemory, assemblyName: DynamicCode.CodeGenControllerAssembly, needsCodeGenAssembly: true)); codeFiles.AddRange(apiFiles); } return((from cr in compileResult from ce in cr.Errors let fileName = Path.GetFileName(ce.FileName) select(new CompilationErrorTS { fileName = fileName, column = ce.Column, line = ce.Line, errorNumber = ce.ErrorNumber, errorText = ce.ErrorText, fileContent = codeFiles.GetOrThrow(fileName).FileContent })).ToList()); }
public void Disable() { CachePermission.ViewCache.AssertAuthorized(); CacheLogic.GloballyDisabled = true; SystemEventLogLogic.Log("CacheLogic.Disable"); }
public static void ForceReset() { foreach (var controller in controllers.Values.NotNull()) { controller.ForceReset(); } SystemEventLogLogic.Log("CacheLogic.ForceReset"); }
public static void Start(IApplicationBuilder app, IApplicationLifetime lifetime) { SystemEventLogLogic.Log("Application Start"); lifetime.ApplicationStopping.Register(() => { SystemEventLogLogic.Log("Application Stop"); }); }
public void RestartServer() { DynamicPanelPermission.RestartApplication.AssertAuthorized(); SystemEventLogLogic.Log("DynamicController.RestartServer"); DynamicCode.OnApplicationServerRestarted?.Invoke(); lifeTime.StopApplication(); }
public static void StartScheduledTasks() { if (running) { throw new InvalidOperationException("SchedulerLogic is already Running in {0}".FormatWith(Environment.MachineName)); } running = true; ReloadPlan(); SystemEventLogLogic.Log("Start ScheduledTasks"); }
public static XDocument ExportRules(bool exportAll = false) { SystemEventLogLogic.Log("Export AuthRules"); return(new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement("Auth", new XElement("Roles", RolesInOrder().Select(r => new XElement("Role", new XAttribute("Name", r.ToString()), GetMergeStrategy(r) == MergeStrategy.Intersection? new XAttribute("MergeStrategy", MergeStrategy.Intersection) : null, new XAttribute("Contains", roles.Value.RelatedTo(r).ToString(","))))), ExportToXml.GetInvocationListTyped().Select(a => a(exportAll)).NotNull().OrderBy(a => a.Name.ToString())))); }
public static XDocument ExportRules(bool exportAll = false) { var imported = Database.Query <LastAuthRulesImportEntity>().SingleOrDefault(); SystemEventLogLogic.Log("Export AuthRules"); return(new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement("Auth", imported == null ? null : new XElement("Imported", new XAttribute("On", imported.Date.ToString("s"))), new XElement("Exported", new XAttribute("On", TimeZoneManager.Now.ToString("s"))), new XElement("Roles", RolesInOrder().Select(r => new XElement("Role", new XAttribute("Name", r.ToString()), GetMergeStrategy(r) == MergeStrategy.Intersection? new XAttribute("MergeStrategy", MergeStrategy.Intersection) : null, new XAttribute("Contains", roles.Value.RelatedTo(r).ToString(","))))), ExportToXml.GetInvocationListTyped().Select(a => a(exportAll)).NotNull().OrderBy(a => a.Name.ToString())))); }
public List <CompilationErrorTS> Compile() { SystemEventLogLogic.Log("DynamicController.Compile"); Dictionary <string, CodeFile> codeFiles = DynamicLogic.GetCodeFilesDictionary(); var result = DynamicLogic.Compile(codeFiles, inMemory: true); return((from ce in result.Errors.Cast <CompilerError>() let fileName = Path.GetFileName(ce.FileName) select(new CompilationErrorTS { fileName = fileName, column = ce.Column, line = ce.Line, errorNumber = ce.ErrorNumber, errorText = ce.ErrorText, fileContent = codeFiles.GetOrThrow(fileName).FileContent })).ToList()); }
public static void AutomaticImportAuthRules(string fileName) { Schema.Current.Initialize(); var script = AuthLogic.ImportRulesScript(XDocument.Load(fileName), interactive: false); if (script == null) { SafeConsole.WriteColor(ConsoleColor.Green, "AuthRules already synchronized"); return; } using (var tr = new Transaction()) { SafeConsole.WriteColor(ConsoleColor.Yellow, "Executing AuthRules changes..."); SafeConsole.WriteColor(ConsoleColor.DarkYellow, script.PlainSql()); script.PlainSqlCommand().ExecuteLeaves(); tr.Commit(); } SystemEventLogLogic.Log("Import AuthRules"); }
public static void StopScheduledTasks() { if (!running) { throw new InvalidOperationException("SchedulerLogic is already Stopped in {0}".FormatWith(Environment.MachineName)); } lock (priorityQueue) { if (!running) { return; } running = false; timer.Change(Timeout.Infinite, Timeout.Infinite); priorityQueue.Clear(); } SystemEventLogLogic.Log("Stop ScheduledTasks"); }
public static void StartRunningProcesses() { if (running) { throw new InvalidOperationException("ProcessLogic is running"); } using (ExecutionContext.SuppressFlow()) Task.Factory.StartNew(() => { var database = Schema.Current.Table(typeof(ProcessEntity)).Name.Schema?.Database; SystemEventLogLogic.Log("Start ProcessRunner"); ExceptionEntity exception = null; using (AuthLogic.Disable()) { try { running = true; (from p in Database.Query <ProcessEntity>() where p.IsMine() && (p.State == ProcessState.Executing || p.State == ProcessState.Suspending || p.State == ProcessState.Suspended) || p.IsShared() && p.State == ProcessState.Suspended select p).SetAsQueued(); CancelNewProcesses = new CancellationTokenSource(); autoResetEvent.Set(); timerNextExecution = new Timer(ob => WakeUp("TimerNextExecution", null), // main timer null, Timeout.Infinite, Timeout.Infinite); if (!CacheLogic.WithSqlDependency) { timerPeriodic = new Timer(ob => WakeUp("TimerPeriodic", null), null, PoolingPeriodMilliseconds, PoolingPeriodMilliseconds); } while (autoResetEvent.WaitOne()) { if (CancelNewProcesses.IsCancellationRequested) { return; } using (HeavyProfiler.Log("PWL", () => "Process Runner")) { (from p in Database.Query <ProcessEntity>() where p.State == ProcessState.Planned && p.PlannedDate <= TimeZoneManager.Now select p).SetAsQueued(); var list = Database.Query <ProcessEntity>() .Where(p => p.IsMine() || p.IsShared()) .Where(p => p.State == ProcessState.Planned) .Select(p => p.PlannedDate) .ToListWakeup("Planned dependency"); SetNextPannedExecution(list.Min()); lock (executing) { int remaining = MaxDegreeOfParallelism - executing.Count; if (remaining > 0) { retry: var queued = Database.Query <ProcessEntity>() .Where(p => p.State == ProcessState.Queued) .Where(p => p.IsMine() || p.IsShared()) .Select(a => new { Process = a.ToLite(), a.QueuedDate, a.MachineName }) .ToListWakeup("Planned dependency"); var afordable = queued .OrderByDescending(p => p.MachineName == Environment.MachineName) .OrderBy(a => a.QueuedDate) .Take(remaining).ToList(); var taken = afordable.Where(p => p.MachineName == ProcessEntity.None).Select(a => a.Process).ToList(); if (taken.Any()) { using (Transaction tr = Transaction.ForceNew()) { Database.Query <ProcessEntity>() .Where(p => taken.Contains(p.ToLite()) && p.MachineName == ProcessEntity.None) .UnsafeUpdate() .Set(p => p.MachineName, p => Environment.MachineName) .Set(p => p.ApplicationName, p => Schema.Current.ApplicationName) .Execute(); tr.Commit(); } goto retry; } foreach (var pair in afordable) { ProcessEntity pro = pair.Process.Retrieve(); IProcessAlgorithm algorithm = ProcessLogic.GetProcessAlgorithm(pro.Algorithm); ExecutingProcess executingProcess = new ExecutingProcess(algorithm, pro); executing.Add(pro.ToLite(), executingProcess); executingProcess.TakeForThisMachine(); using (ExecutionContext.SuppressFlow()) Task.Run(() => { try { executingProcess.Execute(); } catch (Exception ex) { try { ex.LogException(edn => { edn.ControllerName = "ProcessWorker"; edn.ActionName = executingProcess.CurrentProcess.ToLite().Key(); }); } catch { } } finally { lock (executing) { executing.Remove(pro.ToLite()); WakeUp("Process ended", null); } } }); } var suspending = Database.Query <ProcessEntity>() .Where(p => p.State == ProcessState.Suspending) .Where(p => p.IsMine()) .Select(a => a.ToLite()) .ToListWakeup("Suspending dependency"); foreach (var s in suspending) { ExecutingProcess execProc = executing.GetOrThrow(s); if (execProc.CurrentProcess.State != ProcessState.Finished) { execProc.CurrentProcess = s.Retrieve(); execProc.CancelationSource.Cancel(); } } } } } } } catch (ThreadAbortException) { //Ignore } catch (Exception e) { try { exception = e.LogException(edn => { edn.ControllerName = "ProcessWorker"; edn.ActionName = "MainLoop"; }); } catch { } } finally { lock (executing) executing.Clear(); SystemEventLogLogic.Log("Stop ProcessRunner", exception); running = false; } } }, TaskCreationOptions.LongRunning); }
private static void ExecuteProcess() { if (running) { throw new InvalidOperationException("WorkflowScriptRunner process is already running"); } using (ExecutionContext.SuppressFlow()) { Task.Factory.StartNew(() => { SystemEventLogLogic.Log("Start WorkflowScriptRunner"); ExceptionEntity exception = null; try { running = true; CancelProcess = new CancellationTokenSource(); autoResetEvent.Set(); timer = new Timer(ob => WakeUp("TimerNextExecution", null), null, Timeout.Infinite, Timeout.Infinite); GC.KeepAlive(timer); using (UserHolder.UserSession(AuthLogic.SystemUser)) { if (CacheLogic.WithSqlDependency) { SetSqlDependency(); } while (autoResetEvent.WaitOne()) { if (CancelProcess.IsCancellationRequested) { return; } timer.Change(Timeout.Infinite, Timeout.Infinite); nextPlannedExecution = null; using (HeavyProfiler.Log("WorkflowScriptRunner", () => "Execute process")) { processIdentifier = Guid.NewGuid(); if (RecruitQueuedItems()) { while (queuedItems > 0 || RecruitQueuedItems()) { var items = Database.Query <CaseActivityEntity>().Where(m => m.DoneDate == null && m.ScriptExecution.ProcessIdentifier == processIdentifier) .Take(WorkflowLogic.Configuration.ChunkSizeRunningScripts).ToList(); queuedItems = items.Count; foreach (var caseActivity in items) { CancelProcess.Token.ThrowIfCancellationRequested(); try { using (Transaction tr = Transaction.ForceNew()) { caseActivity.Execute(CaseActivityOperation.ScriptExecute); tr.Commit(); } } catch { try { var ca = caseActivity.ToLite().Retrieve(); var retry = ca.WorkflowActivity.Script.RetryStrategy; var nextDate = retry?.NextDate(ca.ScriptExecution.RetryCount); if (nextDate == null) { ca.Execute(CaseActivityOperation.ScriptFailureJump); } else { ca.Execute(CaseActivityOperation.ScriptScheduleRetry, nextDate.Value); } } catch (Exception e) { e.LogException(); throw; } } queuedItems--; } queuedItems = Database.Query <CaseActivityEntity>() .Where(m => m.ScriptExecution.ProcessIdentifier == processIdentifier && m.DoneDate == null) .Count(); } } SetTimer(); SetSqlDependency(); } } } } catch (Exception e) { try { exception = e.LogException(edn => { edn.ControllerName = "WorkflowScriptRunner"; edn.ActionName = "ExecuteProcess"; }); } catch { } } finally { SystemEventLogLogic.Log("Stop WorkflowScriptRunner", exception); running = false; } }, TaskCreationOptions.LongRunning); } }
private static void ExecuteProcess() { if (running) { throw new InvalidOperationException("EmailAsyncSender process is already running"); } using (ExecutionContext.SuppressFlow()) { Task.Factory.StartNew(() => { SystemEventLogLogic.Log("Start AsyncEmailSender"); ExceptionEntity exception = null; try { running = true; CancelProcess = new CancellationTokenSource(); autoResetEvent.Set(); timer = new Timer(ob => WakeUp("TimerNextExecution", null), null, Timeout.Infinite, Timeout.Infinite); GC.KeepAlive(timer); using (AuthLogic.Disable()) { if (EmailLogic.Configuration.AvoidSendingEmailsOlderThan.HasValue) { DateTime firstDate = TimeZoneManager.Now.AddHours(-EmailLogic.Configuration.AvoidSendingEmailsOlderThan.Value); Database.Query <EmailMessageEntity>().Where(m => m.State == EmailMessageState.ReadyToSend && m.CreationDate < firstDate).UnsafeUpdate() .Set(m => m.State, m => EmailMessageState.Outdated) .Execute(); } if (CacheLogic.WithSqlDependency) { SetSqlDepndency(); } while (autoResetEvent.WaitOne()) { if (!EmailLogic.Configuration.SendEmails) { throw new ApplicationException("Email configuration does not allow email sending"); } if (CancelProcess.IsCancellationRequested) { return; } timer.Change(Timeout.Infinite, Timeout.Infinite); nextPlannedExecution = null; using (HeavyProfiler.Log("EmailAsyncSender", () => "Execute process")) { processIdentifier = Guid.NewGuid(); if (RecruitQueuedItems()) { while (queuedItems > 0 || RecruitQueuedItems()) { var items = Database.Query <EmailMessageEntity>().Where(m => m.ProcessIdentifier == processIdentifier && m.State == EmailMessageState.RecruitedForSending) .Take(EmailLogic.Configuration.ChunkSizeSendingEmails).ToList(); queuedItems = items.Count; foreach (var email in items) { CancelProcess.Token.ThrowIfCancellationRequested(); try { using (Transaction tr = Transaction.ForceNew()) { EmailLogic.SenderManager.Send(email); tr.Commit(); } } catch { try { if (email.SendRetries < EmailLogic.Configuration.MaxEmailSendRetries) { using (Transaction tr = Transaction.ForceNew()) { var nm = email.ToLite().Retrieve(); nm.SendRetries += 1; nm.State = EmailMessageState.ReadyToSend; nm.Save(); tr.Commit(); } } } catch { } } queuedItems--; } queuedItems = Database.Query <EmailMessageEntity>().Where(m => m.ProcessIdentifier == processIdentifier && m.State == EmailMessageState.RecruitedForSending).Count(); } } SetTimer(); SetSqlDepndency(); } } } } catch (Exception e) { try { exception = e.LogException(edn => { edn.ControllerName = "EmailAsyncSender"; edn.ActionName = "ExecuteProcess"; }); } catch { } } finally { SystemEventLogLogic.Log("Stop AsyncEmailSender", exception); running = false; } }, TaskCreationOptions.LongRunning); } }
public void RestartServer() { SystemEventLogLogic.Log("DynamicController.RestartServer"); System.Web.HttpRuntime.UnloadAppDomain(); }
public void RestartServer() { SystemEventLogLogic.Log("DynamicController.RestartServer"); DynamicCode.OnApplicationServerRestarted?.Invoke(); lifeTime.StopApplication(); }
public static void Start(string connectionString, bool isPostgres, string?azureStorageConnectionString, string?broadcastSecret, string?broadcastUrls, bool includeDynamic = true, bool detectSqlVersion = true) { AzureStorageConnectionString = azureStorageConnectionString; using (HeavyProfiler.Log("Start")) using (var initial = HeavyProfiler.Log("Initial")) { StartParameters.IgnoredDatabaseMismatches = new List <Exception>(); StartParameters.IgnoredCodeErrors = new List <Exception>(); string?logDatabase = Connector.TryExtractDatabaseNameWithPostfix(ref connectionString, "_Log"); SchemaBuilder sb = new CustomSchemaBuilder { LogDatabaseName = logDatabase, Tracer = initial }; sb.Schema.Version = typeof(Starter).Assembly.GetName().Version !; sb.Schema.ForceCultureInfo = CultureInfo.GetCultureInfo("en-US"); MixinDeclarations.Register <OperationLogEntity, DiffLogMixin>(); MixinDeclarations.Register <UserEntity, UserEmployeeMixin>(); MixinDeclarations.Register <OrderDetailEmbedded, OrderDetailMixin>(); MixinDeclarations.Register <BigStringEmbedded, BigStringMixin>(); ConfigureBigString(sb); OverrideAttributes(sb); if (!isPostgres) { var sqlVersion = detectSqlVersion ? SqlServerVersionDetector.Detect(connectionString) : SqlServerVersion.AzureSQL; Connector.Default = new SqlServerConnector(connectionString, sb.Schema, sqlVersion !.Value); } else { var postgreeVersion = detectSqlVersion ? PostgresVersionDetector.Detect(connectionString) : null; Connector.Default = new PostgreSqlConnector(connectionString, sb.Schema, postgreeVersion); } CacheLogic.Start(sb, serverBroadcast: sb.Settings.IsPostgres ? new PostgresBroadcast() : broadcastSecret != null && broadcastUrls != null ? new SimpleHttpBroadcast(broadcastSecret, broadcastUrls) : null);/*Cache*/ /* LightDynamic * DynamicLogic.Start(sb, withCodeGen: false); * LightDynamic */ DynamicLogicStarter.Start(sb); if (includeDynamic)//Dynamic { DynamicLogic.CompileDynamicCode(); DynamicLogic.RegisterMixins(); DynamicLogic.BeforeSchema(sb); }//Dynamic // Framework modules TypeLogic.Start(sb); OperationLogic.Start(sb); ExceptionLogic.Start(sb); QueryLogic.Start(sb); // Extensions modules MigrationLogic.Start(sb); CultureInfoLogic.Start(sb); FilePathEmbeddedLogic.Start(sb); BigStringLogic.Start(sb); EmailLogic.Start(sb, () => Configuration.Value.Email, (template, target, message) => Configuration.Value.EmailSender); AuthLogic.Start(sb, "System", "Anonymous"); /* null); anonymous*/ AuthLogic.Authorizer = new SouthwindAuthorizer(() => Configuration.Value.ActiveDirectory); AuthLogic.StartAllModules(sb, activeDirectoryIntegration: true); AzureADLogic.Start(sb, adGroups: true, deactivateUsersTask: true); ResetPasswordRequestLogic.Start(sb); UserTicketLogic.Start(sb); SessionLogLogic.Start(sb); TypeConditionLogic.RegisterCompile <UserEntity>(SouthwindTypeCondition.UserEntities, u => u.Is(UserEntity.Current)); ProcessLogic.Start(sb); PackageLogic.Start(sb, packages: true, packageOperations: true); SchedulerLogic.Start(sb); OmniboxLogic.Start(sb); UserQueryLogic.Start(sb); UserQueryLogic.RegisterUserTypeCondition(sb, SouthwindTypeCondition.UserEntities); UserQueryLogic.RegisterRoleTypeCondition(sb, SouthwindTypeCondition.RoleEntities); UserQueryLogic.RegisterTranslatableRoutes(); ChartLogic.Start(sb, googleMapsChartScripts: false /*requires Google Maps API key in ChartClient */); UserChartLogic.RegisterUserTypeCondition(sb, SouthwindTypeCondition.UserEntities); UserChartLogic.RegisterRoleTypeCondition(sb, SouthwindTypeCondition.RoleEntities); UserChartLogic.RegisterTranslatableRoutes(); DashboardLogic.Start(sb, GetFileTypeAlgorithm(p => p.CachedQueryFolder)); DashboardLogic.RegisterUserTypeCondition(sb, SouthwindTypeCondition.UserEntities); DashboardLogic.RegisterRoleTypeCondition(sb, SouthwindTypeCondition.RoleEntities); DashboardLogic.RegisterTranslatableRoutes(); ViewLogLogic.Start(sb, new HashSet <Type> { typeof(UserQueryEntity), typeof(UserChartEntity), typeof(DashboardEntity) }); SystemEventLogLogic.Start(sb); DiffLogLogic.Start(sb, registerAll: true); ExcelLogic.Start(sb, excelReport: true); ToolbarLogic.Start(sb); ToolbarLogic.RegisterTranslatableRoutes(); SMSLogic.Start(sb, null, () => Configuration.Value.Sms); NoteLogic.Start(sb, typeof(UserEntity), /*Note*/ typeof(OrderEntity)); AlertLogic.Start(sb, typeof(UserEntity), /*Alert*/ typeof(OrderEntity)); FileLogic.Start(sb); TranslationLogic.Start(sb, countLocalizationHits: false); TranslatedInstanceLogic.Start(sb, () => CultureInfo.GetCultureInfo("en")); HelpLogic.Start(sb); WordTemplateLogic.Start(sb); MapLogic.Start(sb); PredictorLogic.Start(sb, GetFileTypeAlgorithm(p => p.PredictorModelFolder)); PredictorLogic.RegisterAlgorithm(TensorFlowPredictorAlgorithm.NeuralNetworkGraph, new TensorFlowNeuralNetworkPredictor()); PredictorLogic.RegisterPublication(ProductPredictorPublication.MonthlySales, new PublicationSettings(typeof(OrderEntity))); RestLogLogic.Start(sb); RestApiKeyLogic.Start(sb); WorkflowLogicStarter.Start(sb, () => Starter.Configuration.Value.Workflow); ProfilerLogic.Start(sb, timeTracker: true, heavyProfiler: true, overrideSessionTimeout: true); // Southwind modules EmployeeLogic.Start(sb); ProductLogic.Start(sb); CustomerLogic.Start(sb); OrderLogic.Start(sb); ShipperLogic.Start(sb); StartSouthwindConfiguration(sb); TypeConditionLogic.Register <OrderEntity>(SouthwindTypeCondition.CurrentEmployee, o => o.Employee.Is(EmployeeEntity.Current)); if (includeDynamic)//2 { DynamicLogic.StartDynamicModules(sb); }//2 SetupCache(sb); Schema.Current.OnSchemaCompleted(); if (includeDynamic)//3 { DynamicLogic.RegisterExceptionIfAny(); }//3 } }