ScheduleEngine.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Threading.Tasks;
  3. using Comal.TaskScheduler.Shared;
  4. using InABox.IPC;
  5. using InABox.Clients;
  6. using InABox.Core;
  7. using InABox.Rpc;
  8. namespace PRSServer
  9. {
  10. internal class ScheduleEngine : Engine<ScheduleServerProperties>
  11. {
  12. private readonly Scheduler scheduler = new();
  13. private void CheckConnection()
  14. {
  15. // Wait for server connection
  16. while (!Client.Ping())
  17. {
  18. Logger.Send(LogType.Error, "", "Database server unavailable. Trying again in 30 seconds...");
  19. Task.Delay(30_000).Wait();
  20. Logger.Send(LogType.Information, "", "Retrying connection...");
  21. }
  22. ClientFactory.SetBypass();
  23. }
  24. public override void Run()
  25. {
  26. try
  27. {
  28. if (string.IsNullOrWhiteSpace(Properties.Server))
  29. {
  30. Logger.Send(LogType.Error, "", "Server is blank!");
  31. return;
  32. }
  33. var transport = new RpcClientPipeTransport(DatabaseServerProperties.GetPipeName(Properties.Server));
  34. ClientFactory.SetClientType(typeof(RpcClient<>), Platform.SchedulerEngine, Version, transport);
  35. CheckConnection();
  36. Logger.Send(LogType.Information, "", "Starting Scheduler: ");
  37. scheduler.Start();
  38. }
  39. catch (Exception ex)
  40. {
  41. Logger.Send(LogType.Error, "", "Error: " + ex.Message + "\n" + ex.StackTrace);
  42. throw ex;
  43. }
  44. }
  45. public override void Stop()
  46. {
  47. scheduler.Stop();
  48. }
  49. }
  50. }