Baldinof / caddy-supervisor

Run and supervise background processes from Caddy
MIT License
80 stars 7 forks source link

[Feature request] Hook on caddy reload event? #10

Closed bpolaszek closed 8 months ago

bpolaszek commented 8 months ago

Hello there,

I usually run a caddy reload after deployment to avoid downtimes, but I'd like that some background tasks are restarted when this occurs.

Is it something technically feasible?

Thanks! Ben

Baldinof commented 8 months ago

Hello!

I think you can use the --force flag, it should reload the module even if no changes has been made in the config.

Another solution could be to make a change on the supervisor configuration on each reload, maybe by using a random env var like env CURRENT_DATE {$CURRENT_DATE}, and call reload like this CURRENT_DATE=$(date) caddy reload. It should trigger a supervisor restart.

Let me know if it works :)

francislavoie commented 8 months ago

You might be looking for https://github.com/abiosoft/caddy-exec but unfortunately it's actually coded to avoid running commands on reloads 😅 you could propose a fix to that plugin to explicitly trigger on reload, or you could fork it or whatever.

bpolaszek commented 8 months ago

Hello!

I think you can use the --force flag, it should reload the module even if no changes has been made in the config.

Another solution could be to make a change on the supervisor configuration on each reload, maybe by using a random env var like env CURRENT_DATE {$CURRENT_DATE}, and call reload like this CURRENT_DATE=$(date) caddy reload. It should trigger a supervisor restart.

Let me know if it works :)

That's brilliant, didn't think about that! 🚀 Actually all processes are restarted even those which don't have the env CURRENT_DATE {$CURRENT_DATE} directive, but it doesn't matter.

Here's my systemd configuration, for the records (it didn't work on the 1st try, because systemd would not re-evaluate $(date)):

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
EnvironmentFile=/srv/config/caddy/caddy.env
Type=notify
User=caddy
Group=caddy
ExecStart=/usr/bin/bash -c "export CURRENT_DATE=$(date) && /usr/local/bin/caddy run --environ --config /srv/config/caddy/Caddyfile"
ExecReload=/usr/bin/bash -c "export CURRENT_DATE=$(date) && /usr/local/bin/caddy reload --config /srv/config/caddy/Caddyfile"
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

Thanks a lot! 🙏