diff --git a/Oqtane.Client/Modules/Admin/Jobs/Edit.razor b/Oqtane.Client/Modules/Admin/Jobs/Edit.razor index 0e8edac5..24ab9849 100644 --- a/Oqtane.Client/Modules/Admin/Jobs/Edit.razor +++ b/Oqtane.Client/Modules/Admin/Jobs/Edit.razor @@ -11,26 +11,24 @@
@if (!_editable) { - + } -
- -
- -
-
- + +
+
+
+ +
+
- @@ -83,17 +81,20 @@
- +
- +

- - @if (!_editable) + @if (_editable) { - + + } + else + { + } @SharedLocalizer["Cancel"]
@@ -128,6 +129,11 @@ public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Host; protected override async Task OnInitializedAsync() + { + await LoadJob(); + } + + protected async Task LoadJob() { try { @@ -165,23 +171,15 @@ private async Task SaveJob() { - if (!Utilities.ValidateEffectiveExpiryDates(_startDate, _endDate)) + try { - AddModuleMessage(Localizer["Message.StartEndDateError"], MessageType.Warning); - return; - } - validated = true; - var interop = new Interop(JSRuntime); - if (await interop.FormValid(form)) - { - var job = await JobService.GetJobAsync(_jobId); - job.Name = _name; - job.JobType = _jobType; - var enabledChanged = job.IsEnabled != Boolean.Parse(_isEnabled); - job.IsEnabled = Boolean.Parse(_isEnabled); - - if (_editable) + var interop = new Interop(JSRuntime); + if (await interop.FormValid(form)) { + var job = await JobService.GetJobAsync(_jobId); + job.Name = _name; + job.IsEnabled = bool.Parse(_isEnabled); + job.Frequency = _frequency; if (job.Frequency == "O") // once { @@ -191,6 +189,7 @@ { job.Interval = int.Parse(_interval); } + job.StartDate = _startDate.HasValue && _startTime.HasValue ? LocalToUtc(_startDate.GetValueOrDefault().Date.Add(_startTime.GetValueOrDefault().TimeOfDay)) : null; @@ -200,80 +199,63 @@ : null; job.RetentionHistory = int.Parse(_retentionHistory); - } - if (!job.IsEnabled) - { - job.NextExecution = null; - } - else if (enabledChanged) - { - job.NextExecution = null; - if(job.StartDate != null && job.StartDate < DateTime.UtcNow) + if (!job.IsEnabled || Utilities.ValidateEffectiveExpiryDates(job.StartDate, job.EndDate)) { - var startDate = DateTime.UtcNow; - if ((job.Frequency == "d" || job.Frequency == "w" || job.Frequency == "M") && job.StartDate.Value.TimeOfDay.TotalSeconds != 0) + if (!job.IsEnabled || (job.StartDate >= DateTime.UtcNow || job.StartDate == null)) { - // set the start time - startDate = startDate.Date.Add(job.StartDate.Value.TimeOfDay); - if(startDate < DateTime.UtcNow) - { - switch (job.Frequency) - { - case "d": - startDate = startDate.AddDays(job.Interval); - break; - case "w": - startDate = startDate.AddDays(job.Interval * 7); - break; - case "M": - startDate = startDate.AddMonths(job.Interval); - break; - } - } + job.NextExecution = null; + job = await JobService.UpdateJobAsync(job); + await logger.LogInformation("Job Updated {Job}", job); + NavigationManager.NavigateTo(NavigateUrl()); } - - job.StartDate = startDate; + else + { + AddModuleMessage(Localizer["Message.StartDateError"], MessageType.Warning); + } + } + else + { + AddModuleMessage(Localizer["Message.StartEndDateError"], MessageType.Warning); } } - - await PerformSaveJobAction(job); - NavigationManager.NavigateTo(NavigateUrl()); - } - else - { - AddModuleMessage(Localizer["Message.Required.JobInfo"], MessageType.Warning); - } - } - - private async Task ExecuteJob() - { - var job = await JobService.GetJobAsync(_jobId); - if (job != null) - { - job.NextExecution = null; - - await PerformSaveJobAction(job); - - if(!job.IsStarted) + else { - await JobService.StartJobAsync(_jobId); + AddModuleMessage(Localizer["Message.Required.JobInfo"], MessageType.Warning); } - - NavigationManager.NavigateTo(NavigateUrl()); - } - } - - private async Task PerformSaveJobAction(Job job) - { - try - { - job = await JobService.UpdateJobAsync(job); - await logger.LogInformation("Job Updated {Job}", job); } catch (Exception ex) { - await logger.LogError(ex, "Error Updating Job {Job} {Error}", job, ex.Message); + await logger.LogError(ex, "Error Updating Job {JobId} {Error}", _jobId, ex.Message); + AddModuleMessage(Localizer["Error.Job.Update"], MessageType.Error); + } + } + + private async Task DisableJob() + { + try + { + var job = await JobService.GetJobAsync(_jobId); + if (job != null) + { + if (job.IsExecuting) + { + AddModuleMessage(Localizer["Message.ExecutingError"], MessageType.Warning); + } + else + { + job.IsEnabled = false; + job.NextExecution = null; + job = await JobService.UpdateJobAsync(job); + await logger.LogInformation("Job Updated {Job}", job); + await LoadJob(); + StateHasChanged(); + } + } + } + catch (Exception ex) + { + await logger.LogError(ex, "Error Updating Job {JobId} {Error}", _jobId, ex.Message); AddModuleMessage(Localizer["Error.Job.Update"], MessageType.Error); } } diff --git a/Oqtane.Client/Resources/Modules/Admin/Jobs/Edit.resx b/Oqtane.Client/Resources/Modules/Admin/Jobs/Edit.resx index 0ac43748..939bb336 100644 --- a/Oqtane.Client/Resources/Modules/Admin/Jobs/Edit.resx +++ b/Oqtane.Client/Resources/Modules/Admin/Jobs/Edit.resx @@ -139,7 +139,7 @@ Error Updating Job - You Must Provide The Job Name, Type, Frequency, and Retention + You Must Provide The Job Name, Frequency, and Retention Enter the job name @@ -154,7 +154,7 @@ Select how often you want the job to run - Optionally enter the date and time when this job should start executing + Optionally enter the date and time when this job should start executing. If no date or time is specified, the job will execute immediately. Optionally enter the date and time when this job should stop executing @@ -163,7 +163,7 @@ Number of log entries to retain for this job - Optionally modify the date and time when this job should execute next + The date and time when this job will execute next. This value cannot be modified. Use the settings above to control the execution of the job. Type: @@ -193,12 +193,15 @@ Execute Once - Start Date cannot be after End Date. + The Start Date Cannot Be Later Than The End Date + + + The Start Date Cannot Be Prior To The Current Date + + + The Job Is Currently Executing. You Must Wait Until The Job Has Completed. - The job properties is not able to be modified because it's enabled or running in progress. - - - Execute + The Job Cannot Be Modified As It Is Currently Enabled. You Must Disable The Job To Change Its Settings. \ No newline at end of file diff --git a/Oqtane.Server/Infrastructure/Jobs/HostedServiceBase.cs b/Oqtane.Server/Infrastructure/Jobs/HostedServiceBase.cs index 349ce812..14282b0e 100644 --- a/Oqtane.Server/Infrastructure/Jobs/HostedServiceBase.cs +++ b/Oqtane.Server/Infrastructure/Jobs/HostedServiceBase.cs @@ -198,41 +198,27 @@ namespace Oqtane.Infrastructure { case "m": // minutes nextExecution = nextExecution.AddMinutes(job.Interval); + if (nextExecution < DateTime.UtcNow) nextExecution = DateTime.UtcNow; break; case "H": // hours nextExecution = nextExecution.AddHours(job.Interval); + if (nextExecution < DateTime.UtcNow) nextExecution = DateTime.UtcNow; break; case "d": // days + nextExecution = DateTime.UtcNow.Date.Add(nextExecution.TimeOfDay); // preserve time of day nextExecution = nextExecution.AddDays(job.Interval); - if (job.StartDate != null && job.StartDate.Value.TimeOfDay.TotalSeconds != 0) - { - // set the start time - nextExecution = nextExecution.Date.Add(job.StartDate.Value.TimeOfDay); - } break; case "w": // weeks + nextExecution = DateTime.UtcNow.Date.Add(nextExecution.TimeOfDay); // preserve time of day nextExecution = nextExecution.AddDays(job.Interval * 7); - if (job.StartDate != null && job.StartDate.Value.TimeOfDay.TotalSeconds != 0) - { - // set the start time - nextExecution = nextExecution.Date.Add(job.StartDate.Value.TimeOfDay); - } break; case "M": // months + nextExecution = DateTime.UtcNow.Date.Add(nextExecution.TimeOfDay); // preserve time of day nextExecution = nextExecution.AddMonths(job.Interval); - if (job.StartDate != null && job.StartDate.Value.TimeOfDay.TotalSeconds != 0) - { - // set the start time - nextExecution = nextExecution.Date.Add(job.StartDate.Value.TimeOfDay); - } break; case "O": // one time break; } - if (nextExecution < DateTime.UtcNow) - { - nextExecution = DateTime.UtcNow; - } return nextExecution; }