[SOLVED] Intermediate Python : `signal` Module | Sololearn: Learn to code for FREE!


[SOLVED] Intermediate Python : `signal` Module

Some of you may already be familiar with Mod-Bot. If you are not - allow me to explain a couple of things, so that this question may be a bit easier to understand. Mod-Bot is a program that listens for bot-commands to be entered by users on Discord. It is hosted by a web-service called Heroku (a server that keeps it alive & operational 24/7). Mod-Bot's source-code is stored on GitHub. I have Heroku & GitHub linked in such a way, that whenever I push changes/updates to the GitHub repository - the Heroku app (Mod-Bot) is automatically redeployed/rebooted. I am only aware of two ways to reboot the bot: a) to push changes to the GitHub repository, and b) to navigate to Heroku directly and redeploying it manually (what the Mod-Bot Admins do whenever the bot gets fussy). However, while reading the app's logs, I've noticed a few things. When the app is redeployed, it'll list out the steps it's taking into the app's logs in real time. Example: https://www.sololearn.com/post/1070667 "Stopping all processes with SIGTERM", "Process exited with status 143", and "Starting process with command `python3 main.py`" are outputted to the logs during every reboot (whether rebooted automatically (it cycles every 24 hours) or manually). Following a bit of research, I've learned that SIGTERM is in reference to one of Python's modules "signal" after importing the "signal" module and entering `print(signal.__dict__.keys())`. After noting the information & example above, my question is as follows: Given the information above, is it possible to use `signal.SIGTERM` to reboot an application (that is *not* running on my local machine) by executing a command from the command-line (in Discord)? If so, how might it be done programmatically? How might I go about directly identifying/referencing a process prior to restarting it (programmatically)?

5/5/2021 3:37:51 AM


4 Answers

New Answer


Maybe I am off the marks, but it seems like you could use the "Restart Dyno" API of Heroku. https://devcenter.heroku.com/articles/platform-api-reference#dyno The example there is for cURL but you can also invoke a REST API from Python. https://www.nylas.com/blog/use-python-requests-module-rest-apis/


Signals helps applications get notified when actions occur somewhere. They cannot reboot your application but whenever you start/quit an application, they can quickly make some changes due to an event that has occured. If you knw JavaScript, i'll say signals are "eventListeners", they must accept a signal handlers (event handlers) and an action must occured otherwise they won't be executed. SIGTERM = signal terminator It kills a running signal, it's like a polite way to tell a running signal to end. More of "return 0" in some cpp main's function. There maybe another module that can reboot your system but it's surely not going to be a signal cos all they do is handle asynchronous task. Such modules are going to be platform specific and will have something to do with either "os", "subprocess" or "sys". However if you find a function that can refresh/reboot your server, then you can make the function to be a signal handler


I just woke up and began doing more research and found exactly what you just mentioned Tibor Santa, Lol. I was on my way here to share the news about figuring it out and I see that you already did. I'll be adding a command that includes something like the following: os.system('curl -n -X DELETE https://api.heroku.com/apps/$appname/dynos/worker.1 -H "Content-Type: application/json" -H "Accept: application/vnd.heroku+json; version=3"') I just tested it using the `mod.console` command and it was successful. It's strange that I didn't find that until now. I mean, you'd think that out of all of the Google-searches I've done - I would've found it sooner. *shrug*


Fox I'm glad you managed to solve it ;) I am not too familiar with Heroku, but with these cloud services most of the time there are plenty of options to remote control them, even in a scriptable way, via a CLI (command line interface) or API (usually REST these days)... So this is always worth exploring as a first guess.