CLI Command RunAsync support passing in/propagating a CancelationToken
See original GitHub issueIs your feature request related to a problem? Please describe.
By convention in DotNet Core one wild supply a CancellationToken
into an Async method when await
ed!
Describe the solution you’d like
Support passing in a CancelationToken
such that is propagated to the AsyncCommand.ExecuteAsync
and for that matter provide a ValidateAsync
that also takes the token.
In this way a command handler can detect it’s being canceled (via async Main for example)
Describe alternatives you’ve considered
One can make the organ available via static and class scoped but its not very well encapsulated.
Additional context N/A
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:10 (3 by maintainers)
Top Results From Across the Web
StatelessService.RunAsync(CancellationToken) Method
Make sure cancellationToken passed to RunAsync(CancellationToken) is honored and once it has been signaled, RunAsync(CancellationToken) exits gracefully as soon ...
Read more >Untitled
Web26 Nov 2019 · In fact, if you pass a cancellationToken to ... the girl songs CLI Command RunAsync support passing in/propagating …...
Read more >c# - CancellationToken doesn't propagate
Run(async () => await action(cancellationToken.Token, observation)); you are scheduling a Task that will start another task which is action ...
Read more >Temporalio 0.1.0-beta1
This can accept a cancellation token, but if none given, defaults to Workflow.CancellationToken . Task.Delay or any other .NET timer-related call cannot be...
Read more >Developing with asyncio — Python 3.11.4 documentation
Passing debug=True to asyncio.run() . ... One way of doing that is by using the -W default command line option. When the debug...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I recently encountered the need to support cancellation in my CLI, and wanted to share my solution/workaround.
In my case,
SIGINT/SIGTERM
would terminate the cli process, but some background work would continue running. The API managing this background work supported passing aCancellationToken
, so my solution consist of aabstract
implementation ofAsyncCommand
that uses aCancellationTokenSource
to encapsulate theSystem.Console.CancelKeyPress
andAppDomain.CurrentDomain.ProcessExit
events:When targeting
NETCOREAPP
directly aPosixSignalRegistration
, as @Simonl9l suggests, makes for a simpler implementation:With this approach, commands inherit from
CancellableAsyncCommand
, rather thanAsyncCommand
, and no changes should be needed to theProgram.cs
:References:
ConsoleLifetime.netcoreapp.cs
ConsoleLifetime.notnetcoreapp.cs
P.S. I’ve just started using Spectre after using natemcmaster/CommandLineUtils for a while, and playing around with dotnet/command-line-api a bit; I think I’m in love… ✌🏻❤️
@Cryptoc1 nice workaround, it feels it would be a nice and simple addition to the library (yet I’m sure it has some implications)
I’ve decouple the cancellation token source from the abstract command implementation so it can be used on both AsyncCommand and AsyncCommand<TSettings>, but otherwise pretty nice solution for now!