rama creada apartir de master en j
This commit is contained in:
@@ -16,6 +16,9 @@ using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Application.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Provides scheduling functionality as a service to manage and coordinate timed or periodic operations.
|
||||
/// </summary>
|
||||
public class SchedulerService
|
||||
{
|
||||
private readonly bool _activateCheckExpiredAlerts;
|
||||
@@ -54,19 +57,35 @@ public class SchedulerService
|
||||
private readonly Lazy<IDiagnosisService> _diagnosisService;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SchedulerService"/> class, assigning its dependencies and loading scheduler intervals and activation flags from the supplied application settings.
|
||||
/// </summary>
|
||||
/// <param name="apiSettings">Application configuration providing scheduler intervals, activation flags, and archival thresholds.</param>
|
||||
/// <param name="providersSettings">Configuration describing the external providers whose observations are retrieved.</param>
|
||||
/// <param name="patientService">Lazy provider of patient data operations.</param>
|
||||
/// <param name="treatmentService">Lazy provider of treatment data operations.</param>
|
||||
/// <param name="appointmentService">Lazy provider of appointment data operations.</param>
|
||||
/// <param name="diagnosisService">Lazy provider of diagnosis data operations.</param>
|
||||
/// <param name="medicineService">Lazy provider of medicine data operations.</param>
|
||||
/// <param name="observationService">Lazy provider of observation data operations.</param>
|
||||
/// <param name="configObservationService">Lazy provider of observation configuration operations.</param>
|
||||
/// <param name="logger">Logger used to record scheduler activity and diagnostics.</param>
|
||||
/// <param name="httpClientFactory">Factory used to create HTTP clients for provider integrations.</param>
|
||||
/// <param name="calculatedObservationsService">Lazy provider of calculated observations operations.</param>
|
||||
/// <param name="patientProcedureService">Lazy provider of patient care plan operations.</param>
|
||||
public SchedulerService(IOptions<ApiSettings> apiSettings,
|
||||
IOptions<List<ProvidersSettings>> providersSettings,
|
||||
Lazy<IPatientService> patientService,
|
||||
Lazy<ITreatmentService> treatmentService,
|
||||
Lazy<IAppointmentService> appointmentService,
|
||||
Lazy<IDiagnosisService> diagnosisService,
|
||||
Lazy<IMedicineService> medicineService,
|
||||
Lazy<IObservationService> observationService,
|
||||
Lazy<IConfigObservationService> configObservationService,
|
||||
ILogger<SchedulerService> logger,
|
||||
IHttpClientFactory httpClientFactory,
|
||||
Lazy<ICalculatedObservationsService> calculatedObservationsService,
|
||||
Lazy<IPatientCarePlanService> patientProcedureService)
|
||||
IOptions<List<ProvidersSettings>> providersSettings,
|
||||
Lazy<IPatientService> patientService,
|
||||
Lazy<ITreatmentService> treatmentService,
|
||||
Lazy<IAppointmentService> appointmentService,
|
||||
Lazy<IDiagnosisService> diagnosisService,
|
||||
Lazy<IMedicineService> medicineService,
|
||||
Lazy<IObservationService> observationService,
|
||||
Lazy<IConfigObservationService> configObservationService,
|
||||
ILogger<SchedulerService> logger,
|
||||
IHttpClientFactory httpClientFactory,
|
||||
Lazy<ICalculatedObservationsService> calculatedObservationsService,
|
||||
Lazy<IPatientCarePlanService> patientProcedureService)
|
||||
{
|
||||
_patientService = patientService;
|
||||
_treatmentService = treatmentService;
|
||||
@@ -145,6 +164,9 @@ public class SchedulerService
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the Quartz scheduler by wiring up service dependencies for background jobs, creating job and trigger definitions, and conditionally scheduling them based on feature flag configuration settings before starting the scheduler.
|
||||
/// </summary>
|
||||
private async Task InitScheduler()
|
||||
{
|
||||
//TODO con el cambio a .net core y el repaso a la inyección de dependencias de estructure map
|
||||
@@ -185,10 +207,10 @@ public class SchedulerService
|
||||
CalculateNewsJob.ExpiresIn = _checkNewsJobIntervalMinutes;
|
||||
|
||||
var jobDataMap = new JobDataMap
|
||||
{
|
||||
{ "archivePatientsWithoutObservationsSinceHours", _archivePatientsWithoutObservationsSinceHours },
|
||||
{ "sinceDischargeTime", _sinceDischargeTimeToArchive }
|
||||
};
|
||||
{
|
||||
{ "archivePatientsWithoutObservationsSinceHours", _archivePatientsWithoutObservationsSinceHours },
|
||||
{ "sinceDischargeTime", _sinceDischargeTimeToArchive }
|
||||
};
|
||||
|
||||
// Grab the Scheduler instance from the Factory
|
||||
var factory = new StdSchedulerFactory();
|
||||
@@ -370,6 +392,12 @@ public class SchedulerService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a scheduled job that checks the hydric balance of the RyC component, implemented as a Quartz.NET job.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The DisallowConcurrentExecution attribute prevents overlapping executions of this job.
|
||||
/// </remarks>
|
||||
[DisallowConcurrentExecution]
|
||||
public class CheckHydricBalanceRyCJob : IJob
|
||||
{
|
||||
@@ -379,6 +407,12 @@ public class CheckHydricBalanceRyCJob : IJob
|
||||
public static ICalculatedObservationsService CalculatedObservationsService { get; set; } = null!;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Executes the scheduled job that checks hydric balance for all patients and maps the most recent observation recorded within the current hour window.
|
||||
/// Iterates over every patient, retrieves the latest "Hydric_Balance" observation before a cutoff time, and forwards it to the calculated observations service when it falls within the current hour.
|
||||
/// All exceptions raised during processing are caught and logged, allowing the job to finish without interrupting the scheduler.
|
||||
/// </summary>
|
||||
/// <param name="context">The Quartz job execution context provided by the scheduler when the job trigger fires.</param>
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
var start = DateTime.Now;
|
||||
@@ -417,6 +451,12 @@ public class CheckHydricBalanceRyCJob : IJob
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a scheduled job that checks the status of active treatments.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The DisallowConcurrentExecution attribute ensures that only one instance of this job can execute at any given time.
|
||||
/// </remarks>
|
||||
[DisallowConcurrentExecution]
|
||||
public class CheckActiveTreatmentsJob : IJob
|
||||
{
|
||||
@@ -430,6 +470,10 @@ public class CheckActiveTreatmentsJob : IJob
|
||||
public static ICalculatedObservationsService CalculatedObservationsService { get; set; } = null!;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Executes the scheduled job that iterates through all patients, retrieves their active treatments and the associated medicines (excluding nutrition-type medicines), and calculates medicine observations for each patient. Exceptions are caught and logged without rethrowing, and the total execution duration is logged upon completion.
|
||||
/// </summary>
|
||||
/// <param name="context">The job execution context provided by the scheduler.</param>
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
var start = DateTime.Now;
|
||||
@@ -475,6 +519,12 @@ public class CheckActiveTreatmentsJob : IJob
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a scheduled job that checks for active appointments, implemented as an <see cref="IJob"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The <see cref="DisallowConcurrentExecutionAttribute"/> attribute prevents multiple instances of this job from running at the same time.
|
||||
/// </remarks>
|
||||
[DisallowConcurrentExecution]
|
||||
public class CheckActiveAppointmentsJob : IJob
|
||||
{
|
||||
@@ -486,6 +536,10 @@ public class CheckActiveAppointmentsJob : IJob
|
||||
public static IPatientService PatientService { get; set; } = null!;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Executes the scheduled check active appointments job, logging the start and completion times along with the total elapsed time. Any exception thrown during execution is caught and logged without being rethrown.
|
||||
/// </summary>
|
||||
/// <param name="context">The Quartz scheduler context that provides runtime information for the job execution.</param>
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
var start = DateTime.Now;
|
||||
@@ -507,6 +561,12 @@ public class CheckActiveAppointmentsJob : IJob
|
||||
(end - start).TotalMilliseconds);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Represents a scheduled job that checks for inactive patients.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The DisallowConcurrentExecution attribute prevents multiple instances of this job from running simultaneously.
|
||||
/// </remarks>
|
||||
[DisallowConcurrentExecution]
|
||||
public class CheckInactivePatientsJob : IJob
|
||||
{
|
||||
@@ -514,6 +574,11 @@ public class CheckInactivePatientsJob : IJob
|
||||
|
||||
public static IPatientService PatientService { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Executes the scheduled job that discharges patients who have been inactive (without observations) for a configured period of hours.
|
||||
/// Skips processing when another instance is already running, as indicated by the global <c>isCheckingInactivePatients</c> flag, and logs the elapsed execution time.
|
||||
/// </summary>
|
||||
/// <param name="context">The Quartz job execution context whose <see cref="IJobExecutionContext.JobDetail"/> data map supplies the inactivity and discharge thresholds.</param>
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
//LogExecutionContext.TrySetTraceIdentifier(Guid.NewGuid().ToString(), true);
|
||||
@@ -546,6 +611,12 @@ public class CheckInactivePatientsJob : IJob
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a scheduled job that checks opiate boluses, implementing the <see cref="IJob"/> interface.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The <c>DisallowConcurrentExecution</c> attribute ensures that overlapping executions of this job are prevented, guaranteeing that only one instance runs at a time.
|
||||
/// </remarks>
|
||||
[DisallowConcurrentExecution]
|
||||
public class CheckOpiateBolusesJob : IJob
|
||||
{
|
||||
@@ -553,6 +624,12 @@ public class CheckOpiateBolusesJob : IJob
|
||||
public static IPatientService PatientService { get; set; } = null!;
|
||||
public static ICalculatedObservationsService CalculatedObservationsService { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Executes a scheduled job that calculates opiate bolus observations for all patients.
|
||||
/// Retrieves every patient via the patient service and triggers the bolus opiates calculation for each one,
|
||||
/// logging any errors that occur and reporting the total execution time upon completion.
|
||||
/// </summary>
|
||||
/// <param name="context">The Quartz job execution context provided by the scheduler for this job run.</param>
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
var start = DateTime.Now;
|
||||
@@ -577,6 +654,12 @@ public class CheckOpiateBolusesJob : IJob
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a background job that checks for expired observations, scheduled to run using the Quartz.NET job scheduling system.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The <see cref="DisallowConcurrentExecutionAttribute"/> attribute ensures that only one instance of this job can execute at a given time, preventing overlapping runs that could lead to duplicate processing of expired observations.
|
||||
/// </remarks>
|
||||
[DisallowConcurrentExecution]
|
||||
public class CheckExpiredObservationsJob : IJob
|
||||
{
|
||||
@@ -585,6 +668,10 @@ public class CheckExpiredObservationsJob : IJob
|
||||
public static IObservationService ObservationService { get; set; } = null!;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Executes the scheduled job that checks and expires observations, recalculating dependent data. Uses a global flag to prevent concurrent execution of the expiration logic, and logs the start, duration, and any errors encountered.
|
||||
/// </summary>
|
||||
/// <param name="context">The job execution context provided by the scheduler.</param>
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
var start = DateTime.Now;
|
||||
@@ -632,6 +719,15 @@ public class CalculateNewsJob : IJob
|
||||
public static int ExpiresIn { get; set; } = 15;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Executes the scheduled job that calculates the National Early Warning Score (NEWS) for each patient
|
||||
/// located in an active Point of Care, based on the most recent vital sign observations
|
||||
/// (respiratory rate, SpO2, temperature, systolic blood pressure, and heart rate).
|
||||
/// Observations flagged as expired or whose configured expiration time has elapsed are skipped,
|
||||
/// and the resulting aggregate score is stored as a new NEWS observation for the patient when
|
||||
/// the latest underlying observation is not expired.
|
||||
/// </summary>
|
||||
/// <param name="context">The Quartz job execution context provided by the scheduler.</param>
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
//Filter patients in real locations
|
||||
@@ -783,6 +879,12 @@ public class CalculateNewsJob : IJob
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a scheduled job that checks for expired alerts.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This job is decorated with the <see cref="DisallowConcurrentExecutionAttribute"/> to prevent overlapping executions of the same job instance.
|
||||
/// </remarks>
|
||||
[DisallowConcurrentExecution]
|
||||
public class CheckExpiredAlertsJob : IJob
|
||||
{
|
||||
@@ -791,6 +893,11 @@ public class CheckExpiredAlertsJob : IJob
|
||||
public static IObservationService ObservationService { get; set; } = null!;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Executes the scheduled job that checks for expired alerts and triggers the power-off routine, while preventing concurrent executions and logging execution duration.
|
||||
/// Runs the expiration logic only when the global "isCheckingAlertsExpiration" flag is absent or set to false; any exception is caught and logged without being rethrown.
|
||||
/// </summary>
|
||||
/// <param name="context">The Quartz job execution context provided by the scheduler when the job trigger fires.</param>
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
var start = DateTime.Now;
|
||||
@@ -816,6 +923,12 @@ public class CheckExpiredAlertsJob : IJob
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a scheduled job that archives nurse data, preventing concurrent executions of the same job instance.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The <see cref="DisallowConcurrentExecutionAttribute"/> attribute ensures that the job will not be triggered while a previous execution is still running.
|
||||
/// </remarks>
|
||||
[DisallowConcurrentExecution]
|
||||
public class ArchiveNurseDataJob : IJob
|
||||
{
|
||||
@@ -828,6 +941,10 @@ public class ArchiveNurseDataJob : IJob
|
||||
|
||||
public static IPatientCarePlanService PatientCarePlanService { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Executes the ArchiveNurseDataJob which archives finished nurse procedures, tests, and treatments for patients whose associated end dates exceed the configured thresholds defined in <see cref="ArchiveNurseDataSettings"/>. Each archive category is only processed when its corresponding setting flag is active, and any exception is caught and logged without interrupting the remaining work.
|
||||
/// </summary>
|
||||
/// <param name="context">The Quartz.NET job execution context that provides runtime information for the scheduled job execution.</param>
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
var start = DateTime.Now;
|
||||
@@ -932,6 +1049,10 @@ public class ArchiveNurseDataJob : IJob
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a scheduled job that retrieves observations associated with providers.
|
||||
/// </summary>
|
||||
/// <remarks>The DisallowConcurrentExecution attribute prevents overlapping executions of this job instance.</remarks>
|
||||
[DisallowConcurrentExecution]
|
||||
public class GetProvidersObservationsJob : IJob
|
||||
{
|
||||
@@ -946,6 +1067,12 @@ public class GetProvidersObservationsJob : IJob
|
||||
|
||||
public static IHttpClientFactory HttpClientFactory { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Executes the scheduled job that retrieves observations from all configured providers for every patient.
|
||||
/// Uses reflection to resolve and instantiate each provider type (mapping the assembly name by replacing '-' with '_'),
|
||||
/// skips providers whose type or required constructor cannot be found, and logs and recovers from any exception raised during processing.
|
||||
/// </summary>
|
||||
/// <param name="context">The Quartz job execution context provided by the scheduler when the job is triggered.</param>
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
var start = DateTime.Now;
|
||||
@@ -970,7 +1097,7 @@ public class GetProvidersObservationsJob : IJob
|
||||
|
||||
var ctor = providerType.GetConstructor([
|
||||
typeof(IOptions<ProvidersSettings>),
|
||||
typeof(IHttpClientFactory)
|
||||
typeof(IHttpClientFactory)
|
||||
]);
|
||||
if (ctor == null)
|
||||
{
|
||||
@@ -980,7 +1107,7 @@ public class GetProvidersObservationsJob : IJob
|
||||
|
||||
var customProvider = (BaseProvider)ctor.Invoke([
|
||||
Options.Create(provider),
|
||||
HttpClientFactory
|
||||
HttpClientFactory
|
||||
]);
|
||||
|
||||
var patients = await PatientService.FindAll();
|
||||
|
||||
Reference in New Issue
Block a user