Service Fabric Guest Executable hosting and graceful stop - azure-service-fabric

I have a .NET 4.6.2 console application I want to host in Service Fabric.
My application is listening on console input events for administration tasks.
The most important one is 'q' to stop the application - but in a graceful manner.
Is it somehow possible to also have this functionality in Service Fabric with a guest executable?
Of course not on 'q button pressed' but rather on service deleted or whatever events there are.
If there is no way to achieve this kind of integration with a guest executable - which project template do you recommend to build upon?

Service Fabric sends a Ctrl-C signal to the guest executable process before killing it, allowing for graceful shutdown. You can use the Console.CancelKeyPress event or the Win32 SetConsoleCtrlHandler function to get a notification. Note the exe must be compiled as a console app for this to work.

In your stateful or stateless service class:
class MyService : StatefulService /* or StatelessService */
{
protected override Task OnCloseAsync(CancellationToken cancellationToken)
{
// your graceful shutdown code goes here
}
}
But execution of that method is not guaranteed, for instance when the hosting VM unexpectedly dies.

One way to do this, would be to programmatically delete the service when you're done with it, using DeleteServiceAsync
You can recreate it too, using CreateServiceAsync

Related

Rollback Service installation in unattended mode with install4j

I'm trying to create an Install4j updater in unattended mode using Stand Alone Update Downloader and invoke it using the api. (this is working)
The application is a Windows Service and it opens http ports.
I would like to be able to rollback to the previous installed service if the new installed version isn't working. There should be no user interaction to do it.
What I was planning to do is:
Install the service
Start the service
Call Wait for Http Server Action that calls an Url of the service to check if my new updated service is working as intended.
If it doesn't returns a 2xx it will initiate the rollback process
But what I'm seeing is that the rollback process starts but it stops and uninstalls the new service but the previous service is never reinstalled.
What I would want it to do when rollbacking is:
Stop the new service
Uninstall it
Rollback any other files added by the new version
Reinstall the previous service
Start the previous service
Is there any way to achieve it ?
Edit:
I have those actions in the installer:
In this version of Install4j (10.0.4) the rollback mechanism doesn't handle this scenario. As specified by the comment of Ingo Kegel this will be improved in further version.
What is working with this version is that:
Add the condition on Install service to not to run it when updating like this: !context.isUpdateInstallation(). There will no rollback of the service when updating.
To keep the service started, we added a Stop a Service at the beginning. This way if there's a rollback, the Stop a Service will be rollbacked, so it will restart the service.

Borland C++: How can a Windows service shutdown itself?

I have an old Windows Service written in Borland C++ Builder that I need to extend so that it can shutdown itself under certain conditions.
If I shutdown the service manually via the service control manager, it shuts down properly without any problems. So I thought, calling this->DoShutdown(); would be sufficient (this being an instance derived from TService). But this leaves the service in the state "Shutting down...". I could call ExitProcess afterwards, but this creates an entry in the event log that the service has been shut down unexpectedly.
So what is the proper way to make a Borland C++ Windows service shut down itself?
DoShutdown() is called by TService when it receives a SERVICE_CONTROL_SHUTDOWN request from the SCM while Windows is being shut down. DoShutdown() is not intended to be called directly in user code.
The easiest way to have your service terminate itself is to call its Controller() method (either directly, or via its global ServiceController() function), passing either SERVICE_CONTROL_STOP or SERVICE_CONTROL_SHUTDOWN in the CtrlCode parameter. Let the service handle the request as if it had come from the SCM, so it can act accordingly.

Restarting service (declared in init.rc) from a system app in a Java context

I have a service declared in my init.rc on my platform that runs always, and an Android system app (signed with the platform keys) that is persistent (i.e. always runs), which communicate through TCP/IP.
At some point I would like to restarte the service from the app, the app runs under a defined system uid, however it does not have permission to call start or stop on the console, unfortunately.
I would like the app to restart the service, just like if I had executed the following in the console:
stop service
start service
Is there an android framework call I can make to restart a specific service?

Disable force stop option for a service or an activity

I have a requirement to create a service that cannot be stopped by end user.
If the OS kills it, it is fine.
I have seen some processes that I am not able to quit. I need help on how this is possible.
For example, if i launch a 2.3 emulator and check the running services, one of the service - Japanese IME input does not a contain a stop option.
I cannot stop this service. Similarly with Swype. How is it possible to create such a service.
Thanks in advance.

Stopping the service and the babysited application before uninstalling

I have a service MyService.exe that is babysitting my application MyApp.exe, meaning it starts the application when this one crashes or whatever. Basically when the service is stopped the application is stopped (by the service) and when the service is started the application is started by the service.
In order to stop my service and by that my application when uninstalling I'm doing:
<ServiceControl Id='MyServiceControl' Name='MyServiceForTest' Start='install' Stop='uninstall' Remove='uninstall'/>
But when I want to uninstall everything I get the error message: "The setup must update files or services that cannot be updated while the system is running. If you choose to continue, a reboot will be required to complete the setup.". If I manually stop the service before running the uninstaller I don't get this msg as both my service and my application aren't then running anymore.
In the log file I noticed that this happens in InstallValidate and I get this message b/c of MyApp.exe being running.
I think what happens is: the uninstallers checks the running applications, it notices that the MyService.exe and MyApp.exe are both running, detects probably that the MyService.exe will be stopped by the uninstaller itself as instructed, but doesn't know about the MyApp.exe that this one will also be terminated once the service will be stopped so it will show the reboot-message.
I can't just close MyApp.exe from uninstaller b/c the service will restart it again.
How could I solve this problem so that the user won't need to reboot or to manually stop the service before doing an uninstall/upgrade? Also, I can't change MyService and MyApp code anymore so I will have to do this from the (un)installer only.
TIA,
Viv
I would expose a mechanism in your service in which your installer can instruct it to stand down and terminate the application. This way when Windows Installer costing looks for locked files it doesn't find any.