Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advanced cron expressions #382

Open
KaiserMarvin opened this issue Apr 2, 2024 · 2 comments
Open

Advanced cron expressions #382

KaiserMarvin opened this issue Apr 2, 2024 · 2 comments

Comments

@KaiserMarvin
Copy link

KaiserMarvin commented Apr 2, 2024

I would like to run the task on the last day of the month or more specifically on the last working day of the month at 6pm.
This would work with the following cron expression "0 18 LW * *" unfortunately this is not supported by Coravel.
Would it be possible to extend the parser for these cron expressions to allow the usage of the commands L, W, ? and #, as it is the case in Hangfire, for example (https://github.com/HangfireIO/Cronos) ?

@ivoloshin
Copy link

ivoloshin commented Apr 4, 2024

You can probably do something like below. I assume it'll check daily at 6PM, and then run the predicate to check if it should run on that day.


scheduler.Schedule<MyJob>()
  .DailyAtHour(18)
  .When(IsLastWorkingDayOfMonth);

//compliments of ChatGPT with some mods
public async Task<bool> IsLastWorkingDayOfMonth()
{
	Func<DateTime, DateTime> GetLastWorkingDayOfMonth = date =>
	{
		DateTime lastDayOfMonth = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));

		// Check if the last day of the month falls on a weekend
		if (lastDayOfMonth.DayOfWeek == DayOfWeek.Saturday)
		{
			return lastDayOfMonth.AddDays(-1); // If it's Saturday, return the previous Friday
		}
		else if (lastDayOfMonth.DayOfWeek == DayOfWeek.Sunday)
		{
			return lastDayOfMonth.AddDays(-2); // If it's Sunday, return the previous Friday
		}
		else
		{
			return lastDayOfMonth; // If it's a weekday, return the last day of the month
		}
	};

	DateTime today = DateTime.Today;
	DateTime lastWorkingDay = GetLastWorkingDayOfMonth(today);
	
	return lastWorkingDay == today;
}

@KaiserMarvin
Copy link
Author

@ivoloshin thank you for the quick answer. For me, I actually solved it simply by running the job daily as from you suggested and using the already mentioned Cronos library (https://github.com/HangfireIO/Cronos) in the When function. For the future, however, it would of course be nice if Coravel itselfs supports the full cron expression scope natively.

@KaiserMarvin KaiserMarvin reopened this Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants