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

Start/Stop CAP BackgroundService Manually #1238

Open
PoteRii opened this issue Nov 18, 2022 · 7 comments
Open

Start/Stop CAP BackgroundService Manually #1238

PoteRii opened this issue Nov 18, 2022 · 7 comments

Comments

@PoteRii
Copy link
Contributor

PoteRii commented Nov 18, 2022

Hello, is there any way to stop CAP jobs (publishing, consumption, execution of received messages) at any given time manually?
Basically what I need is to stop CAP processes when CTRL+C is pressed (but wait for the ongoing executions to complete) and not wait until StopAsync method of Bootstrapper class is executed by host itself (because HostOptions.ShutdownTimeout is increased). Would it be correct to invoke StopAsync myself inside of IHostApplicationLifetime.ApplicationStopping.Register? (Some reflection would be needed to get the Bootstrapper instance, since it is internal). It is a matter of graceful shutdown.

@yang-xiaodong
Copy link
Member

Hello, Thanks for your feedback

We plan to add CancellationToken support for BootstrapAsync in version 7.0

public interface IBootstrapper
{
     Task BootstrapAsync(CancellationToken token = default);
}

@PoteRii
Copy link
Contributor Author

PoteRii commented Nov 21, 2022

Thank you for the reply. Can you provide more details, how that would solve the mentioned case?

@yang-xiaodong
Copy link
Member

Usage see:

https://github.com/dotnetcore/CAP/blob/master/samples/Sample.RabbitMQ.MySql/Controllers/ValuesController.cs#L23

We will release the next preview version tomorrow, you need to wait our next preview version to use.

@PoteRii
Copy link
Contributor Author

PoteRii commented Nov 21, 2022

Oh I see the IBootstrapper will have DisposeAsync method. That`s exactly what I need. Thank you!

@PoteRii PoteRii closed this as completed Nov 21, 2022
@yang-xiaodong
Copy link
Member

Fixed in 7.0.0-preview-185881699

@PoteRii
Copy link
Contributor Author

PoteRii commented Feb 6, 2025

Hello,
We have an issue with bootstrapper.DisposeAsync(), let me try to explain our use case.
Our service is hosted in Kubernetes. As soon as pod receives TERM signal, (new deployment was triggered and current pod has to shut down) we fail health check in order to stop API traffic coming to this pod. Actual shutdown of this pod starts after 1 min, because we want to finish processing requests that were received just before receiving TERM signal.
As we fail the health check to stop API traffic, we also Dispose bootstrapper to stop traffic from Kafka as well, but calling Dispose on bootstrapper also stops publishing, because of this code in PublishInternalAsync function:

if (!_bootstrapper.IsStarted)
{
   throw new InvalidOperationException("CAP has not been started!");
}

Now, the ongoing requests (that were received just before TERM signal) and have 1 min to complete may have business logic that produces events in the end, but because bootstrapper.DisposeAsync() was called PublishAsync throws "CAP has not been started!" exception. Meaning that we gave time for requests to complete but all of them finish with exceptions and some business events are lost/not published.
Is there a way to stop only consume and still be able to publish messages? (at least store message in published table without throwing exception, for the new pods to pick it up and really publish to broker)

@PoteRii PoteRii reopened this Feb 6, 2025
@PoteRii
Copy link
Contributor Author

PoteRii commented Feb 13, 2025

Would something like this make more sense? (instead of throwing an exception in the beginning)

Image

Maybe removing this condition at all can work as well? the message will just be stored with Failed state and it will be retried by new pods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants