Skip to content

Commit

Permalink
fix: Cron Validation (#4842)
Browse files Browse the repository at this point in the history
* Add Cron Next Time Validation

The cron job can't be created if the year is more than 100 years in the future.
Getting the next valid time will return null if this is the case.

* add next cron validation to api
* add next cron validation to job settings page

* Add Missing Import
  • Loading branch information
aj3x committed May 2, 2023
1 parent ca947bd commit 97cc42f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/Ombi.Settings/Settings/Models/JobSettingsHelper.cs
@@ -1,5 +1,6 @@
using Ombi.Helpers;
using Quartz;
using System;

namespace Ombi.Settings.Settings.Models
{
Expand Down Expand Up @@ -104,7 +105,9 @@ private static string Get(string settings, string defaultCron)

private static string ValidateCron(string cron)
{
if (CronExpression.IsValidExpression(cron))
CronExpression expression = new CronExpression(cron);
DateTimeOffset? nextFireUTCTime = expression.GetNextValidTimeAfter(DateTime.Now);
if (CronExpression.IsValidExpression(cron) && nextFireUTCTime != null)
{
return cron;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Ombi/Controllers/V1/SettingsController.cs
Expand Up @@ -652,7 +652,9 @@ public async Task<JobSettingsViewModel> JobSettings([FromBody]JobSettings settin
try
{
var isValid = CronExpression.IsValidExpression(expression);
if (!isValid)
CronExpression cron = new CronExpression(expression);
DateTimeOffset? nextFireUTCTime = cron.GetNextValidTimeAfter(DateTime.Now);
if (!isValid || nextFireUTCTime == null)
{
return new JobSettingsViewModel
{
Expand Down

0 comments on commit 97cc42f

Please sign in to comment.