C
C#3w ago
acookook

Cron job "0 */30 15-16 * * ?" in Quartz .NET runs also at 16:30 p. m. Is this correct?

I would like to have a job that starts at 15:00, executes also 15:30 and 16:00. Is this possible without creating two jobs in Quartz .NET as ChatGPT says? Also our application executes at 15:00, 15:30, 16:00, 16:30. I don't see how 16:30 fits in my cron job above, but I definetly don't want this. Since other jobs are executing just fine and no other customers reported the problem, I don't neccessary blame the cron generator. Chat GPT suggest having 0 */30 15 * * ? and 0 0 16 * * ? .
1 Reply
acookook
acookookOP3w ago
Something I came quickly up
private static List<string> TransformCron(string cronSchedule, string minutes, string hours)
{
var originalCronSchedule = new List<string>() { cronSchedule };

// Both */ (minute interval) and "-" in hour interval need to be present so that we know that the transformation should occur.
if (!hours.Contains("-") && !minutes.Contains("*/"))
return originalCronSchedule;

// Hour string 15-16
var startEndHour = hours.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);

// Only split by '-'
if (startEndHour?.Length < 2)
return originalCronSchedule;

//15
var startHour = int.Parse(startEndHour[0]);
//16
var endHour = int.Parse(startEndHour[1]);
//16 - 15
var difference = endHour - startHour;

var interMediateCronJob = string.Empty;
var lastCronJob = string.Empty;

// Start and end hour are invalid if difference is lower than 0.
if (difference <= 0)
{
return originalCronSchedule;
}

interMediateCronJob = difference == 1 ? $"0 {minutes} {startHour} * * ?" : $"0 {minutes} {startHour}-{endHour - 1} * * ?";

lastCronJob = $"0 0 {endHour} * * ?";

return new List<string>()
{
interMediateCronJob,
lastCronJob
};
}
private static List<string> TransformCron(string cronSchedule, string minutes, string hours)
{
var originalCronSchedule = new List<string>() { cronSchedule };

// Both */ (minute interval) and "-" in hour interval need to be present so that we know that the transformation should occur.
if (!hours.Contains("-") && !minutes.Contains("*/"))
return originalCronSchedule;

// Hour string 15-16
var startEndHour = hours.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);

// Only split by '-'
if (startEndHour?.Length < 2)
return originalCronSchedule;

//15
var startHour = int.Parse(startEndHour[0]);
//16
var endHour = int.Parse(startEndHour[1]);
//16 - 15
var difference = endHour - startHour;

var interMediateCronJob = string.Empty;
var lastCronJob = string.Empty;

// Start and end hour are invalid if difference is lower than 0.
if (difference <= 0)
{
return originalCronSchedule;
}

interMediateCronJob = difference == 1 ? $"0 {minutes} {startHour} * * ?" : $"0 {minutes} {startHour}-{endHour - 1} * * ?";

lastCronJob = $"0 0 {endHour} * * ?";

return new List<string>()
{
interMediateCronJob,
lastCronJob
};
}
Want results from more Discord servers?
Add your server