EXECUTE Plugin - Torrent Added - Restart deluged

Suggest, post, or discuss plugins for Deluge
dugiehowsa
New User
New User
Posts: 5
Joined: Mon Aug 22, 2022 2:56 pm

EXECUTE Plugin - Torrent Added - Restart deluged

Post by dugiehowsa »

Greetings all.

I have been working on a script that will run once a torrent is added via the EXECUTE plugin.

The purpose of the script is to:
- Stop deluged
- Do Stuff
- Start deluged

The script executes without issue from terminal.

The problem is that I can't seem to separate the script from it's parent process when running it from the EXECUTE plugin. The script stops as soon as the command to stop deluged is run. I have tried &, nohup, screen and tmux to break the dependency between the parent deluged process and the child script process, but no success so far.

Any thoughts?
Last edited by dugiehowsa on Mon Sep 25, 2023 12:01 pm, edited 3 times in total.
mhertz
Moderator
Moderator
Posts: 2215
Joined: Wed Jan 22, 2014 5:05 am
Location: Denmark

Re: EXECUTE Plugin - Torrent Added - Restart deluged

Post by mhertz »

Interesting question :) I had likeminded issue in a plugin some time ago too.

If all else fail, then I can later look up some flags for some python commands, for seperating from parent when running commands or script, and use that in an intermidiate python script, but I just tested a bash script from execute's added hook and running deluge-console's halt command, to quit gracefully, and do something, and then start deluged again, which worked fine. Only issue being if having deluge-gtk open, and even though having auto-connect enabled, then will not connect back automatically and need done manually, but else worked fine.

Can you give me some pointers to what you're doing, or better yet, a reproducal scenario for me to test.

I'm though not gonna be able to mess with it until tomorrow evening or so, so if others have input then please jump in instead.
User avatar
ambipro
Moderator
Moderator
Posts: 445
Joined: Thu May 19, 2022 3:33 am
Contact:

Re: EXECUTE Plugin - Torrent Added - Restart deluged

Post by ambipro »

Might be more effective to set this up in crontab then depend on deluge....what exactly is the "stuff" because if you're doing this every time a torrent completes or something, it seems like it would potentially be restarting the daemon alot.

Give us some details as @mhertz said on ways to test this, because I am also not seeing this. Code would be useful too if possible.
mhertz
Moderator
Moderator
Posts: 2215
Joined: Wed Jan 22, 2014 5:05 am
Location: Denmark

Re: EXECUTE Plugin - Torrent Added - Restart deluged

Post by mhertz »

BTW, looking at the code puzzles me it's even working, as uses a deferred twisted function, capturing return code, so would think when twisted gets brought down with rest of deluged then would kill script, but I'm guessing it's already fired-off in a subprocess and just the monitoring part that breaks presumably.

I remember now it was subprocess.popen I used in a plugin once(restart deluge and run script between, from plugin), which e.g has creation flags for detached_process and create_new_process_group etc, but if still needed can look at that later as said, if not going other routes, as the helpful tip by the good ambipro above.
User avatar
ambipro
Moderator
Moderator
Posts: 445
Joined: Thu May 19, 2022 3:33 am
Contact:

Re: EXECUTE Plugin - Torrent Added - Restart deluged

Post by ambipro »

mhertz wrote: Fri Sep 22, 2023 10:29 pm BTW, looking at the code puzzles me it's even working, as uses a deferred twisted function, capturing return code, so would think when twisted gets brought down with rest of deluged then would kill script, but I'm guessing it's already fired-off in a subprocess and just the monitoring part that breaks presumably.

I remember now it was subprocess.popen I used in a plugin once(restart deluge and run script between, from plugin), which e.g has creation flags for detached_process and create_new_process_group etc, but if still needed can look at that later as said, if not going other routes, as the helpful tip by the good ambipro above.
I think this was the plugin you wrote for me to restart :P
mhertz
Moderator
Moderator
Posts: 2215
Joined: Wed Jan 22, 2014 5:05 am
Location: Denmark

Re: EXECUTE Plugin - Torrent Added - Restart deluged

Post by mhertz »

@ambipro

Sure was buddy, good times :)
mhertz
Moderator
Moderator
Posts: 2215
Joined: Wed Jan 22, 2014 5:05 am
Location: Denmark

Re: EXECUTE Plugin - Torrent Added - Restart deluged

Post by mhertz »

@dugiehowsa

I just came to think about that the most usual way of running deluged on linux is from a service-manager e.g. systemd nowadays mostly, and that was something I stupidly haden't tested, and so I just did to make sure haden't took my foot in my mouth so-to-speak, and indeed you're right, sorry.

So in that case then the requested functionality breaks, as you stated all along, so you don't have to bring a reproducal scenerio, and I will mess little with it later today or evening, or maybe first tommorow evening, not fully sure but will take a look, also even though you decide going other route's instead as can be helpful for others finding this from a search, and intrigues me also honestly :)

Also, side-note but don't need deluge-console(or a few lines small python client script) to close grace-fully if not using systemd, like I seemed to have insinuated before, as there's a hook for sigint/sigterm so can close gracefully with that, e.g. using pkill etc(just not with sigkill/9 obviously) - which is what systemd does when closing, i.e. sigterm sent to the in foreground running service(-d switch of deluged), triggering said hook.

Sorry for confusion/oversight.

Edit: Looked little today, and 'systemctl stop' kills all child-processes by default, as KillMode=control-group default in services when not overridden/changed, so if adding KillMode=process to an systemd drop-in override file, then works I tested. Also, from the docs, 'systemctl kill' should work instead in our case, and does almost, but just cannot start deluged again as in same control-group or something - actually might as well just use pkill instead of 'systemctl kill' but still same issue though - will look tomorrow as said.
dugiehowsa
New User
New User
Posts: 5
Joined: Mon Aug 22, 2022 2:56 pm

Re: EXECUTE Plugin - Torrent Added - Restart deluged

Post by dugiehowsa »

mhertz wrote: Fri Sep 22, 2023 8:09 pm
Can you give me some pointers to what you're doing, or better yet, a reproducal scenario for me to test.
Thanks for taking a look.

The script checks to see if a vpn client is running. If it is, then do nothing.

If the vpn client is not running, then it stops deluge, starts the vpn client, and then restarts deluge.

I stop/start both deluge-web and deluged. I probably only need to stop/start deluged.

Code: Select all

#!/bin/sh

                        /usr/bin/systemctl stop deluge-web;
                        /usr/bin/systemctl stop deluged;
                        /usr/local/bin/vpnclient connect;
                        /usr/bin/systemctl start deluged;
                        /usr/bin/systemctl start deluge-web;
I originally was calling this "start" script using the add torrent option in the EXECUTE plugin, but when I realized that stopping deluge was killing the script, I tried using an "initialize" script that that just called the "start" script using nohup or another option to separate the start script from the deluged process.
mhertz
Moderator
Moderator
Posts: 2215
Joined: Wed Jan 22, 2014 5:05 am
Location: Denmark

Re: EXECUTE Plugin - Torrent Added - Restart deluged

Post by mhertz »

Thanks for elaborating. Personally I would do it different honestly, in-fact already do same myself, but just always start deluged from my own startup bash script, handling all this, and also bound to xdg-open for when clicking/running torrents/magnets, instead of other way around, as it's too insecure imho otherwise, plus ineffecient.

As said in my edit, I only currently found one solution to your issue i.e. adding a drop-in file setting KillMode=process to deluged.service, and works then, as every other method I tried for now didn't work, and doesn't matter whatever magic trying do, as everything run under same cgroup and that cgroup teared down upon stopping the service - also that python function I mentioned with flags for detached and new group was windows only I had forgotten, and still neither related to cgroups specifically, regardless if was for linux. As said I thought found workaround with systemctl kill or just pkill, but the service goes into deactivated mode shortly after and cannot run your script finished. Advanced workarounds need made to separate out from said cgroup, unless using my working systemd drop-in solution firstly mentioned, I could maybe find another way eventually, but as said I would highly suggest going other route.

If not wanting use a script like me(autorun when loading torrents first time), then how about adding a drop-in file enhancing deluged.service to also check/run your vpn firstly? E.g. have your script check if vpn up and if not start it, nothing else. If so then add a drop-in file with an execstartpre line with full path to your script(or just script it directly on the line), something like I previously among else referenced here: viewtopic.php?p=230404#p230404 (Just forget the needless parts).

Or maybe you rely on your setup as kinda kill-switch, though I still would highly dis-recommend that, as not safe enough, and ineffecient, and much better ways deal with such imho.

Anyway just some thoughts.
User avatar
ambipro
Moderator
Moderator
Posts: 445
Joined: Thu May 19, 2022 3:33 am
Contact:

Re: EXECUTE Plugin - Torrent Added - Restart deluged

Post by ambipro »

A few options I think are better suited for this are...

1. Use binhex's delugevpn container, or a regular deluge container and something like gluetun.

2. Create a crontab to do this check independently with your script.

3. Create a service that auto restarts the VPN if it crashes or something (whatever is preventing this from staying persistent) - when this service starts, have it instead run the script that shuts down deluged, starts the VPN, and restarts deluged.

Some things to note, if you are concerned about leaking your IP or other information, then your current method will likely negate the VPN's added privacy altogether. I'd imagine that a connection is likely made prior to deluged successfully exiting through the "Torrent Added" execute....it may only be for a split second to grab metadata, but a leak is a leak.

I'd recommend either option 3, with added iptables rules or something to prevent leaks, or option 1 if you aren't opposed to using docker. Option 2 is a little hacky and doesn't add the leak protection 3 or 1 could provide if deployed successfully.
Post Reply