-
-
Notifications
You must be signed in to change notification settings - Fork 254
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
Adding new task to scheduler from another service #77
Comments
For testing i tried tried injecting IScheduler and adding new task to it and it actually worked. Code what i tried:
|
If there's a way to hook into some event that's emited when the config files change, here's what you could do: When scheduling, add an ID to each scheduled event (this is an internal method, which maybe I should make public via the interface...) scheduler.EveryMinute().AssignUniqueIndentifier("SomeID"); Keep track of all the IDS that you have (statically). Then, when you need to refresh - do this for each ID: scheduler.TryUnschedule("SomeID"); Then re-schedule again. Those are thread safe methods too. Not necessarily recommended for production, but would work in dev for sure (assuming you can detect when the config files change - which Im looking into) |
So you can detect when the IConfiguration is changed. Take a look here https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.iconfiguration.getreloadtoken?view=aspnetcore-2.2 |
There will be actually just one scheduled task ever created what i would like to change the execution time on. So if there would be a way to unschedule it and schedule new one that should do the business. |
So try in ChangeToken.OnChange(() => this.Configuration.GetReloadToken(), () => {
using (var scope = app.ApplicationServices.CreateScope())
{
IScheduler scheduler = scope.ServiceProvider.GetRequiredService<IScheduler>();
IConfiguration config = scope.ServiceProvider.GetRequiredService<IConfiguration>();
// Remove your past scheduled event/task
(scheduler as Scheduler).TryUnschedule("SomeID");
// Add a new one.
var scheduledEvent = scheduler
.Schedule(() => Console.WriteLine("A new scheduled task!"))
.Cron(config["MyCronConfigValue"]);
// Assign a unique ID so you can remove it next time.
(scheduledEvent as ScheduledEvent).AssignUniqueIndentifier("SomeID");
//Or
scheduledEvent.PreventOverlapping("SomeID");
}
}); |
This can stay closed, but in case anyone is looking for a generic way to update scheduled tasks using reflection;
Many thanks to James for |
I would like to add tasks to scheduler dynamically in a service what gets the execution times from configuration file. At the moment its done in the application Configure method and every time configuration is changed i would have to restart the whole program. Is this something what could be possible with current scheduler implementation?
The text was updated successfully, but these errors were encountered: