/// <summary> /// 通常在 Startup.cs 中的 Configure() 方法中执行 /// </summary> /// <param name="app"></param> /// <param name="registerService">CO2NET 注册对象</param> /// <returns></returns> public static IApplicationBuilder UseXncfModules(IApplicationBuilder app, IRegisterService registerService) { foreach (var register in RegisterList) { try { register.UseXncfModule(app, registerService); } catch { } //执行中间件 if (register is IXncfMiddleware middlewareRegister) { try { middlewareRegister.UseMiddleware(app); } catch { } } //执行线程 if (register is IXncfThread threadRegister) { try { XncfThreadBuilder xncfThreadBuilder = new XncfThreadBuilder(); threadRegister.ThreadConfig(xncfThreadBuilder); xncfThreadBuilder.Build(app, register); } catch (Exception ex) { SenparcTrace.BaseExceptionLog(ex); } } } return(app); }
public void ThreadConfig(XncfThreadBuilder xncfThreadBuilder) { xncfThreadBuilder.AddThreadInfo(new Ncf.XncfBase.Threads.ThreadInfo( name: "定时备份", intervalTime: TimeSpan.FromSeconds(30), task: async(app, threadInfo) => { try { //SenparcTrace.SendCustomLog("执行调试", "DatabaseToolkit.Register.ThreadConfig"); threadInfo.RecordStory("开始检测并备份"); using (var scope = app.ApplicationServices.CreateScope()) { var serviceProvider = scope.ServiceProvider; //检测当前模块是否可用 XncfRegisterManager xncfRegisterManager = new XncfRegisterManager(serviceProvider); var xncfIsValiable = await xncfRegisterManager.CheckXncfValiable(this); if (!xncfIsValiable) { throw new NcfModuleException($"{this.MenuName} 模块当前不可用,跳过数据库自动备份轮询"); } //初始化数据库备份方法 BackupDatabase backupDatabase = new BackupDatabase(serviceProvider); //初始化参数 var backupParam = new BackupDatabase.BackupDatabase_Parameters(); var dbConfigService = serviceProvider.GetService <ServiceBase <DbConfig> >(); var dbConfig = await dbConfigService.GetObjectAsync(z => true); var stopBackup = false; try { if (dbConfig != null && dbConfig.BackupCycleMinutes > 0 && !dbConfig.BackupPath.IsNullOrEmpty()) { if (SystemTime.NowDiff(dbConfig.LastBackupTime) > TimeSpan.FromMinutes(dbConfig.BackupCycleMinutes)) { backupParam.Path = dbConfig.BackupPath; //await backupParam.LoadData(serviceProvider); //threadInfo.RecordStory("完成备份设置数据载入"); } else { stopBackup = true; } } else { threadInfo.RecordStory("不需要备份,或没有设置备份周期/路径,已忽略本次备份计划"); stopBackup = true; //不需要备份,或没有设置,返回 } } catch (Exception ex) { threadInfo.RecordStory($@"遇到异常,可能未配置数据库,已忽略本次备份计划。如需启动,请更新此模块到最新版本。 异常信息:{ex.Message} {ex.StackTrace}"); stopBackup = true; //可能没有配置数据库,返回 } if (stopBackup) { return; } //执行备份方法 threadInfo.RecordStory("备份开始:" + backupParam.Path); var result = backupDatabase.Run(backupParam); if (!result.Success) { threadInfo.RecordStory("执行备份发生异常:" + result.Message); throw new Exception("执行备份发生异常"); } dbConfig.RecordBackupTime(); await dbConfigService.SaveObjectAsync(dbConfig); threadInfo.RecordStory("完成数据库自动备份:" + result.Message); SenparcTrace.SendCustomLog("完成数据库自动备份", backupParam.Path); } } catch (NcfModuleException ex) { throw; } catch { throw; } finally { threadInfo.RecordStory("检测并备份结束"); } }, exceptionHandler: ex => { SenparcTrace.SendCustomLog("DatabaseToolkit", $@"{ex.Message} {ex.StackTrace} {ex.InnerException?.StackTrace}"); return(Task.CompletedTask); })); }