#1 2022-10-28 07:56:45

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 392

Stopping a mormot2 server for update

We have a windows service that runs a second executable that it is the real mormot2 server
The service has the responsibility to automatically update the mormot2 server executable when an update by the developer is available.
What will be a reliable way to update it?
We want to stop receiving new requests till the existing are served and closed, to terminate the server and the mormot2 server executable and to update the executable and restart it.
Killing the windows process is not a solution as I think. What do you propose?
Creating a rest function to request to finish? And if mormot2 is in dead-lock situation? And how do you find that a mormot2 server windows process is exited? By trying to delete or to modify the mormot2 executable as file?

In linux, we have the systemd for a number of linux distos
Do you run mormot2 server executable as a process controlled by systemd (to rely to databases being ready for example) or by eg rc.local?
How do you start it? Using -f or -r switch in TsynDaemon?
How do you stop it? Using SIGSTOP?
Again how is it better to look for updates and auto-update?
Do you run (and how do you initiate it) an other script to stop systemd service, update and restart it?

Let's consider that the update host and procedure to receive the updated version is safe discussing about security considerations

Thank you in advance

Last edited by dcoun (2022-10-28 07:58:08)

Offline

#2 2022-10-28 11:26:20

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,206
Website

Re: Stopping a mormot2 server for update

Don't mess too much with low-level stuff.

I have slightly enhanced TSynDaemon: https://github.com/synopse/mORMot2/commit/ea9475a2

Just run the TSynDaemon command parameters: /stop and /start - it will work on Windows and POSIX.
On POSIX, if you encapsulated the daemon with a systemd file, you should use the "service" system command instead.

Then the TRestServer will properly shutdown.
And the executable will block until /stop is finished, so that you can safely replace the executable. It is always good to try DeleteFile() in a loop and retry for a few seconds after a Sleep(100) because the file may not be immediately released.

I guess I will make a service monitoring system integrated with mORMot, which supports in-place update, and with a simple watchdog system.

Offline

#3 2022-10-28 11:31:36

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,206
Website

Re: Stopping a mormot2 server for update

A typical systemd configuration file:

# Systemd service for My Company Daemon
[Unit]
Description=My Company Daemon
After=network-online.target

[Service]
Type=forking
RemainAfterExit=no
User=mycomp
Group=mycomp
UMask=0007
PIDFile=/home/mycompany/bin/.mycompanyDaemon.pid
ExecStart=/home/mycompany/bin/mycompanyDaemon -f
ExecStop=/home/mycompany/bin/mycompanyDaemon -k
TimeoutSec=300

[Install]
WantedBy=multi-user.target

And to install it:

mv mycompanyDaemon.service /etc/systemd/system/mycompanyDaemon.service
systemctl enable mycompanyDaemon.service

Then you can run e.g.

systemctl start mycompanyDaemon

Offline

#4 2022-10-28 14:40:25

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 392

Re: Stopping a mormot2 server for update

Thank you a lot @ab

Offline

#5 2022-10-30 15:19:32

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 392

Re: Stopping a mormot2 server for update

The service daemon with the last updates works perfectly. I noticed that if you just run from the command line the service executable without options I can log an exception from the windows' Tservice but it is harmless.

Another point that needs to be handled better in windows is the platform independent commands in mormot.core.os to run an executable from a mormot server (RunProcess, RunCommand, etc)

In my scenario, the service periodically looks for updates. If it finds an available update, it downloads it and runs an other executable to do the update, the ServiceUpdater.
I have created this executable, the ServiceUpdater, using mormot framework that stops the service, unzips and replaces the executable and re-runs the service.
This executable does not work, if the services runs it. It waits for the service to stop till timeout.
Looking the code, I noticed that all mormot's commands from windows are based on Createprocess where the host process can not end before the executed process

I change the mormot's RunProcess/RunCommand to ShellExecute with command 'open' and now the service stops and the ServiceUpdater can continue to do the update.
Probably it could be included for windows. I will test this situation also in linux.

Offline

Board footer

Powered by FluxBB