Automatic shutdown after client disconnect

I spin up two syncthings, one on server and one on client, to sync data. After a while the data is synced and the client shuts down. I’d like to shut the server down at that point as well. (When the client comes back, it pings the server first and the server starts up a new syncthing.) What’s the best way to do this? I can have a separate daemon which watches the server API for connections and then calls shutdown on a server once its client connection goes away, but that’s quite burdensome; is there an easier way?

(I could of course keep track of the running syncthing on the server, and reconnect to it, but that’s fiddly for various reasons I won’t bore you with and so I don’t want to do that.)

Do you have some background on why you’re doing this? I’m. It seeing the point of it. :slight_smile:

Basically, it’s for syncing files to the cloud from many clients. A client has a username and password: it calls a server API with those to log in. If the login is successful, the server spins up syncthing pointing to that client’s folders, and passes back the connection details for that syncthing server. The client then spins up its own syncthing and connects them, and that means the client is now syncing folders to the server.

When the client goes away, the server should shut down the syncthing it has running for that client. This is partially to save resources, and partially because when the client comes back again, we’ll spin up a new syncthing, and syncthing correctly does not like it when two syncthings are running with the same config.

OK, that’s a bit special, I have nothing obvious that comes to mind. Other than having a single syncthing instance on the server always running and with config for all clients and folders, that is.

I could do that (I thought about it), but firstly I’m a bit worried about leakage, secondly I don’t think I can have separate per-folder authentication details (but I’m happy to be corrected on that!), and thirdly I’d have to edit the syncthing config while syncthing is running, which I think will not end well?

I understood it so that you have (say) a hundred different client that all have an individual folder to sync. I would have done something like,

On the server side, one hundred folders, with random folder IDs (like we default to).

On the client side, on first startup, a certificate and hence device ID is automatically created as always. Your thing talks to the server and says “I am user foo, give me a sync connection, my device ID is abc123”. If this is the first time, the server will need to add the abc123 device to the user specific folder, but that’s a one time thing. It then responds “Okay, my device id is xyz789, use the folder ID ghj234”. The client will need to add this device and folder, the first time only.

After that, everything is set up and authenticated. There is exactly one folder shared between the server and client, and only between them. The server can be running always, the client can start and stop syncthing as desired, no more config changes need to happen on either side.

Doing the config changes to add devices and folders while syncthing is running is perfectly fine, assuming that you do it through the API of course.

That’s a great description you’ve given! I hadn’t thought of doing it that way. The main reason, though, is that I didn’t know that there was a way of doing per-folder authentication; how do I stop one client syncing another client’s folders?

You don’t add that client to that folder on the server. :slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.