public DatabaseController(ILogger logger, IEventManager events, IRpcHandler rpc, IRconManager rcon, DatabaseConfiguration configuration) : base(logger, events, rpc, rcon, configuration) { // Set global database options ServerConfiguration.DatabaseConnection = this.Configuration.Connection.ToString(); ServerConfiguration.AutomaticMigrations = this.Configuration.Migrations.Automatic; // Enable SQL query logging MySqlTrace.Switch.Level = SourceLevels.All; MySqlTrace.Listeners.Add(new ConsoleTraceListener()); BootHistory lastBoot; using (var context = new StorageContext()) { // Create database if needed if (!context.Database.Exists()) { this.Logger.Info($"No existing database found, creating new database \"{this.Configuration.Connection.Database}\""); context.Database.CreateIfNotExists(); } lastBoot = context.BootHistory.OrderByDescending(b => b.Created).FirstOrDefault() ?? new BootHistory(); this.currentBoot = new BootHistory(); context.BootHistory.Add(this.currentBoot); context.SaveChanges(); } Task.Factory.StartNew(UpdateBootHistory); this.Events.OnRequest(BootEvents.GetTime, () => this.currentBoot.Created); this.Events.OnRequest(BootEvents.GetLastTime, () => lastBoot.Created); this.Events.OnRequest(BootEvents.GetLastActiveTime, () => lastBoot.LastActive); }
public DatabaseController(ILogger logger, DatabaseConfiguration configuration, ICommunicationManager comms) : base(logger, configuration) { // Set global database options ServerConfiguration.DatabaseConnection = this.Configuration.Connection.ToString(); ServerConfiguration.AutomaticMigrations = this.Configuration.Migrations.Automatic; // Enable SQL query logging MySqlTrace.Switch.Level = SourceLevels.All; MySqlTrace.Listeners.Add(new ConsoleTraceListener()); BootHistory lastBoot; using (var context = new StorageContext()) { // Create database if needed if (!context.Database.Exists()) { this.Logger.Info($"No existing database found, creating new database \"{this.Configuration.Connection.Database}\""); } var migrator = new DbMigrator(new Migrations.Configuration()); foreach (var migration in migrator.GetPendingMigrations()) { this.Logger.Debug($"Running migration: {migration}"); migrator.Update(migration); } lastBoot = context.BootHistory.OrderByDescending(b => b.Created).FirstOrDefault() ?? new BootHistory(); this.currentBoot = new BootHistory(); context.BootHistory.Add(this.currentBoot); context.SaveChanges(); } Task.Factory.StartNew(UpdateBootHistory); comms.Event(BootEvents.GetTime).FromServer().On(e => e.Reply(this.currentBoot.Created)); comms.Event(BootEvents.GetLastTime).FromServer().On(e => e.Reply(lastBoot.Created)); comms.Event(BootEvents.GetLastActiveTime).FromServer().On(e => e.Reply(lastBoot.LastActive)); }