Forums

Restart server via API gives 502 error in log

I just successfully added a "Restart server" button to my site's admin page so that I have an easier way to restart after I update my database and script files. And it works nicely!

However, since the button click triggers a regular POST from the browser, the server-side "restart" part of the admin script is never allowed to finish (I assume), since the server restart happens while the script is still running. And this results in a 502 error page being returned to the browser. But worse, this also generates the same 502 error page in the access log, which another script of mine periodically scans for issues...

I thought about just hitting the correct API page (incl. token) with an ajax request from my admin page, but then that's a big cross-site scripting no-no. I could also route the ajax request though a python script on my own site, and let that one do the restart, but while that would solve the in-browser 502 page it would still generate the 502 error in the logs, right?

What are my options here?

You could try updating the file timestamp on your WSGI file (say, using the Path.touch method) -- that would update your website asynchronously, so it might work better.

But TBH, if your site is quite busy, you may well get 502 errors while it reloads anyway from other hits to it. Perhaps an alternative would be to write out a file when your site is restarting with a timestamp of the last restart (or a history of restarts), and then to check that in your error scanner so that it doesn't alert you if you get a 502 within (say) 1 minute of a restart.

Ah, didn't think about other concurrent hits from other users... So in principle a restart will always have the potential to generate 502's.

In that case I think I'll opt for flat out ignoring 502's altogether; they're quite specific to this situation anyway I believe? (regular Python errors give me 500's, and those are really the thing I'm after anyway).

Thanks for thinking along!

502 in that circumstances are ignorable. You are right.