-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the generic host and application lifetime to handle sigint/sigter…
…m events
- Loading branch information
Showing
14 changed files
with
508 additions
and
416 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,81 @@ | ||
using System; | ||
using Microsoft.Extensions.Hosting; | ||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Runly.Hosting | ||
{ | ||
class GetAction : IHostAction | ||
internal class GetAction : HostedAction | ||
{ | ||
readonly bool verbose; | ||
string filePath; | ||
readonly string jobType; | ||
readonly JobCache cache; | ||
private readonly bool _verbose; | ||
private string _filePath; | ||
private readonly string _jobType; | ||
private readonly JobCache _cache; | ||
private readonly IHostApplicationLifetime _applciationLifetime; | ||
|
||
internal GetAction(bool verbose, string jobType, string filePath, JobCache cache) | ||
internal GetAction(bool verbose, string jobType, string filePath, JobCache cache, IHostApplicationLifetime applicationLifetime) | ||
{ | ||
this.verbose = verbose; | ||
this.jobType = jobType; | ||
this.filePath = filePath; | ||
this.cache = cache; | ||
_verbose = verbose; | ||
_jobType = jobType; | ||
_filePath = filePath; | ||
_cache = cache; | ||
_applciationLifetime = applicationLifetime; | ||
} | ||
|
||
public async Task RunAsync(CancellationToken cancel) | ||
protected override async Task RunAsync(CancellationToken cancel) | ||
{ | ||
TextWriter writer = null; | ||
JobInfo job; | ||
|
||
try | ||
{ | ||
job = cache.Get(jobType); | ||
} | ||
catch (TypeNotFoundException) | ||
{ | ||
Console.WriteLine($"Could not find the job type '{jobType}'."); | ||
return; | ||
} | ||
TextWriter writer = null; | ||
JobInfo job; | ||
|
||
var config = cache.GetDefaultConfig(job); | ||
try | ||
{ | ||
job = _cache.Get(_jobType); | ||
} | ||
catch (TypeNotFoundException) | ||
{ | ||
Console.WriteLine($"Could not find the job type '{_jobType}'."); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
var config = _cache.GetDefaultConfig(job); | ||
|
||
if (filePath == null) | ||
try | ||
{ | ||
writer = Console.Out; | ||
|
||
if (_filePath == null) | ||
{ | ||
writer = Console.Out; | ||
} | ||
else | ||
{ | ||
// If path is an existing directory, such as ".", add a file name | ||
if (Directory.Exists(_filePath)) | ||
_filePath = Path.Combine(_filePath, job.JobType.Name + ".json"); | ||
|
||
writer = new StreamWriter(File.Open(_filePath, FileMode.Create)); | ||
} | ||
|
||
await writer.WriteAsync(_verbose ? ConfigWriter.ToJson(config) : ConfigWriter.ToReducedJson(config)); | ||
} | ||
else | ||
finally | ||
{ | ||
// If path is an existing directory, such as ".", add a file name | ||
if (Directory.Exists(filePath)) | ||
filePath = Path.Combine(filePath, job.JobType.Name + ".json"); | ||
|
||
writer = new StreamWriter(File.Open(filePath, FileMode.Create)); | ||
if (_filePath != null && writer != null) | ||
{ | ||
await writer.FlushAsync(); | ||
writer.Dispose(); | ||
} | ||
} | ||
|
||
await writer.WriteAsync(verbose ? ConfigWriter.ToJson(config) : ConfigWriter.ToReducedJson(config)); | ||
|
||
if (_filePath != null) | ||
Console.WriteLine($"Default config for {job.JobType.FullName} saved to {Path.GetFullPath(_filePath)}"); | ||
} | ||
finally | ||
{ | ||
if (filePath != null && writer != null) | ||
{ | ||
await writer.FlushAsync(); | ||
writer.Dispose(); | ||
} | ||
_applciationLifetime?.StopApplication(); | ||
} | ||
|
||
if (filePath != null) | ||
Console.WriteLine($"Default config for {job.JobType.FullName} saved to {Path.GetFullPath(filePath)}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using Microsoft.Extensions.Hosting; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Runly.Hosting | ||
{ | ||
internal abstract class HostedAction : IHostedService | ||
{ | ||
private Task _run; | ||
private CancellationTokenSource _stoppingCts; | ||
|
||
/// <summary> | ||
/// Triggered when the application host is ready to start the service. | ||
/// </summary> | ||
/// <param name="cancellationToken">Indicates that the start process has been aborted.</param> | ||
/// <returns>A <see cref="Task"/> that represents the asynchronous Start operation.</returns> | ||
public virtual Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
// Create linked token to allow cancelling executing task from provided token | ||
_stoppingCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); | ||
|
||
// Store the task we're executing | ||
_run = RunAsync(_stoppingCts.Token); | ||
|
||
// If the task is completed then return it, this will bubble cancellation and failure to the caller | ||
if (_run.IsCompleted) | ||
{ | ||
return _run; | ||
} | ||
|
||
// Otherwise it's running | ||
return Task.CompletedTask; | ||
} | ||
|
||
/// <summary> | ||
/// Triggered when the application host is performing a graceful shutdown. | ||
/// </summary> | ||
/// <param name="cancellationToken">Indicates that the shutdown process should no longer be graceful.</param> | ||
/// <returns>A <see cref="Task"/> that represents the asynchronous Stop operation.</returns> | ||
public virtual async Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
// Stop called without start | ||
if (_run == null) | ||
return; | ||
|
||
try | ||
{ | ||
// Signal cancellation to the executing method | ||
_stoppingCts!.Cancel(); | ||
} | ||
finally | ||
{ | ||
// Wait until the task completes or the stop token triggers | ||
var tcs = new TaskCompletionSource<object>(); | ||
using CancellationTokenRegistration registration = cancellationToken.Register(s => ((TaskCompletionSource<object>)s!).SetCanceled(), tcs); | ||
// Do not await the _executeTask because cancelling it will throw an OperationCanceledException which we are explicitly ignoring | ||
await Task.WhenAny(_run, tcs.Task).ConfigureAwait(false); | ||
} | ||
|
||
} | ||
|
||
/// <inheritdoc /> | ||
public virtual void Dispose() | ||
{ | ||
_stoppingCts?.Cancel(); | ||
} | ||
|
||
protected abstract Task RunAsync(CancellationToken cancellation); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.