-
Notifications
You must be signed in to change notification settings - Fork 4
Feature/scheduler #7
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "sdk": { | ||
| "version": "2.2.105" | ||
| "version": "2.2.401" | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "RoslynExtensionsOptions": { | ||
| "enableAnalyzersSupport": true | ||
| }, | ||
| "FormattingOptions": { | ||
| "enableEditorConfigSupport": true | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,8 +19,8 @@ variables: | |
| BuildConfiguration: "Release" | ||
| Project: "$(Build.SourcesDirectory)/src/KK.DotNet.BackgroundTasks.Scheduled/KK.DotNet.BackgroundTasks.Scheduled.csproj" | ||
| ProjectFolder: "$(Build.SourcesDirectory)/src/KK.DotNet.BackgroundTasks.Scheduled" | ||
| DotNetVersion: "2.2.105" | ||
| GitVersion: "4.0.1-beta1-62" | ||
| DotNetVersion: "2.2.401" | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the new SDK from global.json |
||
| GitVersion: "5.0.1" | ||
|
|
||
| name: $(Year:yy)$(DayOfYear)$(Rev:rr) | ||
|
|
||
|
|
@@ -85,4 +85,4 @@ steps: | |
| inputs: | ||
| pathtoPublish: "$(Build.ArtifactStagingDirectory)" | ||
| artifactName: NuGet | ||
| parallel: true | ||
| parallel: true | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| namespace KK.DotNet.BackgroundTasks.Scheduled.Sample.Web.Backgroundservices | ||
| { | ||
| using Microsoft.Extensions.Logging; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using KK.DotNet.BackgroundTasks.Scheduled; | ||
|
|
||
| public class RuntimeSampleTask : IScheduledTask | ||
| { | ||
| private readonly ILogger<RuntimeSampleTask> logger; | ||
|
|
||
| public RuntimeSampleTask( | ||
| IScheduledTaskOptions<RuntimeSampleTask> options, | ||
| ILogger<RuntimeSampleTask> logger | ||
| ) | ||
| { | ||
| this.Options = options; | ||
| this.logger = logger; | ||
| } | ||
|
|
||
| public IScheduledTaskOptions<IScheduledTask> Options { get; } | ||
|
|
||
| public async Task ExecuteAsync(CancellationToken stoppingToken) | ||
| { | ||
| this.logger.LogDebug("Runtime Sample Task started"); | ||
|
|
||
| _ = stoppingToken.Register(() => this.logger.LogDebug("Runtime Sample Task forced stopping.")); | ||
|
|
||
| var runCount = 0; | ||
| while (runCount < 5) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally, I'd prefer a for-loop, but i assume thats a matter of style.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point, I will change this. |
||
| { | ||
| if (stoppingToken.IsCancellationRequested) | ||
| { | ||
| // Task was killed from outside. | ||
| this.logger.LogDebug($"{System.DateTime.Now} Runtime Sample Task cancellation requested!"); | ||
| return; | ||
| } | ||
|
|
||
| this.logger.LogDebug($"{System.DateTime.Now} Runtime Sample Task loop {runCount}"); | ||
| await Task.Delay(1000, stoppingToken); | ||
| runCount++; | ||
| } | ||
|
|
||
| this.logger.LogDebug("Runtime Sample Task is finished"); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| namespace KK.DotNet.BackgroundTasks.Scheduled.Sample.Web.Pages | ||
| { | ||
| using System.Diagnostics; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
|
||
| [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] | ||
| public class ErrorModel : PageModel | ||
| { | ||
| public string RequestId { get; set; } | ||
|
|
||
| public bool ShowRequestId => !string.IsNullOrEmpty(this.RequestId); | ||
|
|
||
| public void OnGet() | ||
| => this.RequestId = Activity.Current?.Id ?? this.HttpContext.TraceIdentifier; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| @page | ||
| @model IndexModel | ||
| @{ | ||
| ViewData["Title"] = "Home page"; | ||
| } | ||
|
|
||
| <div class="text-center"> | ||
| <h1 class="display-4">Welcome</h1> | ||
| <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p> | ||
| <p>Scheduled Tasks running: @Model.Scheduler.Tasks.Count</p> | ||
| @foreach (var task in @Model.Scheduler.Tasks) | ||
| { | ||
| <p><strong>Name</strong>: @task.Name <br /><strong>NextStartTime</strong>: @task.NextStartTime</p> | ||
| } | ||
| <form method="POST"> | ||
| <div>Name: <input asp-for="Options.Name" /></div> | ||
| <div>Name: <input asp-for="Options.Schedule" /></div> | ||
| <input value="Add Task" type="submit" /> | ||
| </form> | ||
| </div> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| namespace KK.DotNet.BackgroundTasks.Scheduled.Sample.Web.Pages | ||
| { | ||
| using System.Threading.Tasks; | ||
| using KK.DotNet.BackgroundTasks.Scheduled.Sample.Web.Backgroundservices; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.AspNetCore.Mvc.RazorPages; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| public class IndexModel : PageModel | ||
| { | ||
| private readonly ILogger<RuntimeSampleTask> runtimeSampleTaskLogger; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also matter of preference but: since almost all classes only have one logger, I call them all the same: "logger". I only call them differently if there are different loggers in a given class to make myself and others aware that this might not be the logger one wants to use. Also, the shorter name might allow you to put the parameters in lines 22ff on a single line.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point, I will change that. |
||
|
|
||
| public Scheduler Scheduler { get; } | ||
|
|
||
|
|
||
| [BindProperty] | ||
| public ScheduledTaskOptions<RuntimeSampleTask> Options { get; set; } | ||
|
|
||
| // When using the default SchedulerHostedService than there is also a Scheduler | ||
| // this can be used to get access to the scheduled task list for example | ||
| public IndexModel( | ||
| Scheduler scheduler, | ||
| ILogger<RuntimeSampleTask> runtimeSampleTaskLogger | ||
| ) | ||
| { | ||
| this.Scheduler = scheduler; | ||
| this.runtimeSampleTaskLogger = runtimeSampleTaskLogger; | ||
| this.Options = new ScheduledTaskOptions<RuntimeSampleTask>() | ||
| { | ||
| Name = "Awesome Runntime Task", | ||
| Schedule = "*/15 * * * *" | ||
| }; | ||
| } | ||
|
|
||
| public void OnGet() | ||
| { | ||
| //this.Scheduler.SchedulerTasks; | ||
| } | ||
|
|
||
| public IActionResult OnPost() | ||
| { | ||
| if (!this.ModelState.IsValid) | ||
| { | ||
| return this.Page(); | ||
| } | ||
|
|
||
| this.Scheduler.AddTask(new RuntimeSampleTask(this.Options, this.runtimeSampleTaskLogger)); | ||
|
|
||
| return this.Page(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| @{ | ||
| @page | ||
| @model PrivacyModel | ||
| @{ | ||
| ViewData["Title"] = "Privacy Policy"; | ||
| } | ||
| <h1>@ViewData["Title"]</h1> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| namespace KK.DotNet.BackgroundTasks.Scheduled.Sample.Web.Pages | ||
| { | ||
| using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
|
||
| public class PrivacyModel : PageModel | ||
| { | ||
| public void OnGet() | ||
| { | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update to newest version