private void Run(CommandQueueElement next, BotDb db) { lock (_lock) { if (next.Id == 0) { db.CommandQueue.Add(next); db.SaveChanges(); } else { db.CommandQueue.Attach(next); } // Execute if ((next.ScheduledAt ?? DateTimeOffset.Now) <= DateTimeOffset.Now) { Logger.Instance.Log(LogLevel.Debug, $"Running {next.Command} (id: {next.Id})"); CommandQueueElement following = next.Command.RunInternal(); next.ScheduledAt = null; Logger.Instance.Log(LogLevel.Debug, $"Finished {next.Command} (id: {next.Id})"); if (following != null) { if (following.Command == next.Command) { next.ScheduledAt = following.ScheduledAt; Logger.Instance.Log(LogLevel.Debug, $"Requeueing {next.Command} (id: {next.Id})"); } else { db.CommandQueue.Add(following); Logger.Instance.Log(LogLevel.Debug, $"Queueing {following.Command} (id: {following.Id}, by: {next.Id})"); } } db.SaveChanges(); } } }
public void Run(CommandQueueElement next) { using (BotDb db = new BotDb()) Run(next, db); }