From 9b449c4f891db459759b2cf02e31d395393e708d Mon Sep 17 00:00:00 2001 From: Sinan TAVILOGLU Date: Thu, 24 Nov 2022 15:43:09 +0300 Subject: [PATCH] Update Cron.cs - MinuteInterval & HourInterval Updated MinuteInterval & HourInterval methods to make them available (not obselete) again. Added defensive code against interval value to get rid of the problem described in HangfireIO/Hangfire#1041 Also related: HangfireIO/Hangfire#1054 HangfireIO/Hangfire#1779 --- src/Hangfire.Core/Cron.cs | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/Hangfire.Core/Cron.cs b/src/Hangfire.Core/Cron.cs index ac6542d54..99d1510e2 100644 --- a/src/Hangfire.Core/Cron.cs +++ b/src/Hangfire.Core/Cron.cs @@ -1,4 +1,4 @@ -// This file is part of Hangfire. Copyright © 2013-2014 Hangfire OÜ. +// This file is part of Hangfire. Copyright © 2013-2014 Hangfire OÜ. // // Hangfire is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as @@ -114,7 +114,7 @@ public static string Weekly(DayOfWeek dayOfWeek, int hour) /// The minute in which the schedule will be activated (0-59). public static string Weekly(DayOfWeek dayOfWeek, int hour, int minute) { - return $"{minute} {hour} * * {(int) dayOfWeek}"; + return $"{minute} {hour} * * {(int)dayOfWeek}"; } /// @@ -213,32 +213,40 @@ public static string Yearly(int month, int day, int hour, int minute) return $"{minute} {hour} {day} {month} *"; } - /// - /// Returns cron expression that never fires. Specifically 31st of February - /// - /// - public static string Never() - { - return Yearly(2, 31); - } + /// + /// Returns cron expression that never fires. Specifically 31st of February + /// + /// + public static string Never() + { + return Yearly(2, 31); + } /// /// Returns cron expression that fires every <> minutes. /// - /// The number of minutes to wait between every activation. - [Obsolete("Please use Cron expressions instead. Will be removed in 2.0.0")] + /// The number of minutes to wait between every activation. Only 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30 values are allowed. public static string MinuteInterval(int interval) { + if (interval < 1 || interval > 30 || 60 % interval != 0) + { + throw new ArgumentOutOfRangeException(nameof(interval), "Invalid interval value."); + } + return $"*/{interval} * * * *"; } /// /// Returns cron expression that fires every <> hours. /// - /// The number of hours to wait between every activation. - [Obsolete("Please use Cron expressions instead. Will be removed in 2.0.0")] + /// The number of hours to wait between every activation. Only 1, 2, 3, 4, 6, 8, 12 value are allowed. public static string HourInterval(int interval) { + if (interval < 1 || interval > 12 || 24 % interval != 0) + { + throw new ArgumentOutOfRangeException(nameof(interval), "Invalid interval value."); + } + return $"0 */{interval} * * *"; }