|
| 1 | +using System; |
| 2 | +using Api.Controllers.Models; |
| 3 | +using Api.Database.Models; |
| 4 | +using Api.Services; |
| 5 | +using Api.Services.MissionLoaders; |
| 6 | +using Api.Utilities; |
| 7 | +using Hangfire; |
| 8 | + |
| 9 | +namespace Api.HostedServices |
| 10 | +{ |
| 11 | + public class InspectionFrequencyHostedService : IHostedService, IDisposable |
| 12 | + { |
| 13 | + private readonly ILogger<InspectionFrequencyHostedService> _logger; |
| 14 | + private readonly IServiceScopeFactory _scopeFactory; |
| 15 | + private Timer? _timer = null; |
| 16 | + |
| 17 | + public InspectionFrequencyHostedService( |
| 18 | + ILogger<InspectionFrequencyHostedService> logger, |
| 19 | + IServiceScopeFactory scopeFactory |
| 20 | + ) |
| 21 | + { |
| 22 | + _logger = logger; |
| 23 | + _scopeFactory = scopeFactory; |
| 24 | + } |
| 25 | + |
| 26 | + private IMissionDefinitionService MissionDefinitionService => |
| 27 | + _scopeFactory |
| 28 | + .CreateScope() |
| 29 | + .ServiceProvider.GetRequiredService<IMissionDefinitionService>(); |
| 30 | + |
| 31 | + private IMissionSchedulingService MissionSchedulingService => |
| 32 | + _scopeFactory |
| 33 | + .CreateScope() |
| 34 | + .ServiceProvider.GetRequiredService<IMissionSchedulingService>(); |
| 35 | + |
| 36 | + private IRobotService RobotService => |
| 37 | + _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IRobotService>(); |
| 38 | + |
| 39 | + private IMissionLoader MissionLoader => |
| 40 | + _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IMissionLoader>(); |
| 41 | + |
| 42 | + public Task StartAsync(CancellationToken stoppingToken) |
| 43 | + { |
| 44 | + _logger.LogInformation("Inspection Frequency Hosted Service Running."); |
| 45 | + |
| 46 | + var timeUntilMidnight = (DateTime.UtcNow.AddDays(1) - DateTime.UtcNow).TotalSeconds; |
| 47 | + _timer = new Timer( |
| 48 | + DoWork, |
| 49 | + null, |
| 50 | + TimeSpan.FromSeconds(timeUntilMidnight), |
| 51 | + TimeSpan.FromDays(1) |
| 52 | + ); |
| 53 | + return Task.CompletedTask; |
| 54 | + } |
| 55 | + |
| 56 | + private async void DoWork(object? state) |
| 57 | + { |
| 58 | + var missionQuery = new MissionDefinitionQueryStringParameters(); |
| 59 | + |
| 60 | + List<MissionDefinition>? missionDefinitions; |
| 61 | + try |
| 62 | + { |
| 63 | + missionDefinitions = await MissionDefinitionService.ReadByHasInspectionFrequency(); |
| 64 | + } |
| 65 | + catch (InvalidDataException e) |
| 66 | + { |
| 67 | + _logger.LogError(e, "{ErrorMessage}", e.Message); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if (missionDefinitions == null) |
| 72 | + { |
| 73 | + _logger.LogInformation("No mission definitions with inspection frequency found."); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + var selectedMissionDefinitions = missionDefinitions.Where(m => |
| 78 | + m.AutoScheduleFrequency != null |
| 79 | + && m.AutoScheduleFrequency.GetSchedulingTimesForNext24Hours() != null |
| 80 | + ); |
| 81 | + |
| 82 | + if (selectedMissionDefinitions.Any() == false) |
| 83 | + { |
| 84 | + _logger.LogInformation( |
| 85 | + "No mission definitions with inspection frequency found that are due for inspection today." |
| 86 | + ); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + foreach (var missionDefinition in selectedMissionDefinitions) |
| 91 | + { |
| 92 | + var jobDelays = |
| 93 | + missionDefinition.AutoScheduleFrequency!.GetSchedulingTimesForNext24Hours(); |
| 94 | + |
| 95 | + if (jobDelays == null) |
| 96 | + { |
| 97 | + _logger.LogWarning( |
| 98 | + "No job schedules found for mission definition {MissionDefinitionId}.", |
| 99 | + missionDefinition.Id |
| 100 | + ); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + foreach (var jobDelay in jobDelays) |
| 105 | + { |
| 106 | + _logger.LogInformation( |
| 107 | + "Scheduling mission run for mission definition {MissionDefinitionId} in {TimeLeft}.", |
| 108 | + missionDefinition.Id, |
| 109 | + jobDelay |
| 110 | + ); |
| 111 | + BackgroundJob.Schedule( |
| 112 | + () => AutomaticScheduleMissionRun(missionDefinition), |
| 113 | + jobDelay |
| 114 | + ); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public async Task AutomaticScheduleMissionRun(MissionDefinition missionDefinition) |
| 120 | + { |
| 121 | + _logger.LogInformation( |
| 122 | + "Scheduling mission run for mission definition {MissionDefinitionId}.", |
| 123 | + missionDefinition.Id |
| 124 | + ); |
| 125 | + |
| 126 | + if (missionDefinition.InspectionArea == null) |
| 127 | + { |
| 128 | + _logger.LogWarning( |
| 129 | + "Mission definition {MissionDefinitionId} has no inspection area.", |
| 130 | + missionDefinition.Id |
| 131 | + ); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + IList<Robot> robots; |
| 136 | + try |
| 137 | + { |
| 138 | + robots = await RobotService.ReadRobotsForInstallation( |
| 139 | + missionDefinition.InstallationCode |
| 140 | + ); |
| 141 | + } |
| 142 | + catch (Exception e) |
| 143 | + { |
| 144 | + _logger.LogError(e, "{ErrorMessage}", e.Message); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + if (robots == null) |
| 149 | + { |
| 150 | + _logger.LogInformation( |
| 151 | + "No robots found for installation code {InstallationCode}.", |
| 152 | + missionDefinition.InstallationCode |
| 153 | + ); |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + var robot = robots.FirstOrDefault(r => |
| 158 | + r.CurrentInspectionArea?.Id == missionDefinition.InspectionArea.Id |
| 159 | + ); |
| 160 | + if (robot == null) |
| 161 | + { |
| 162 | + _logger.LogWarning( |
| 163 | + "No robot found for mission definition {MissionDefinitionId} and inspection area {InspectionAreaId}.", |
| 164 | + missionDefinition.Id, |
| 165 | + missionDefinition.InspectionArea.Id |
| 166 | + ); |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + _logger.LogInformation( |
| 171 | + "Scheduling mission run for mission definition {MissionDefinitionId} and robot {RobotId}.", |
| 172 | + missionDefinition.Id, |
| 173 | + robot.Id |
| 174 | + ); |
| 175 | + |
| 176 | + try |
| 177 | + { |
| 178 | + await MissionSchedulingService.ScheduleMissionRunFromMissionDefinitionLastSuccessfullRun( |
| 179 | + missionDefinition.Id, |
| 180 | + robot.Id |
| 181 | + ); |
| 182 | + } |
| 183 | + catch (Exception e) |
| 184 | + { |
| 185 | + _logger.LogError(e, "{ErrorMessage}", e.Message); |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + return; |
| 190 | + } |
| 191 | + |
| 192 | + public Task StopAsync(CancellationToken stoppingToken) |
| 193 | + { |
| 194 | + _logger.LogInformation("Inspection Frequency Hosted Service is stopping."); |
| 195 | + |
| 196 | + _timer?.Change(Timeout.Infinite, 0); |
| 197 | + |
| 198 | + return Task.CompletedTask; |
| 199 | + } |
| 200 | + |
| 201 | + public void Dispose() |
| 202 | + { |
| 203 | + _timer?.Dispose(); |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments