Ask HN: What is the best architecture for a bot running background service

3 points by prashantsengar ↗ HN
I built a Telegram bot that allowed users to run a configurable script on my server.

Here is how it works:

1. User contacts the bot 2. Configures how he wants to run the script. 3. User asks the bot to start it.

What happens when the user starts it is that I create a new thread running the script. The script involves listening from updates from another API so I thought running it as a thread would be a good choice.

Now what happens is that some users tell me that the script stops working after a while. Perhaps the thread gets killed by the OS after a while?

I am trying to understand what is a good architecture for such an application.

Right now, there are 10 people who use the bot so it handles it effectively. What if I run it on EC2 instances with load balancing enabled?

Another way I was thinking of making it work is like this: 1. Running the bot on a separate EC 2 instance 2. Running a web server on another instance with load balancing. 3. Now when a user asks the bot to run it, the bot calls the web server and runs a new process/thread there.

I wonder what would be the implications when load balancing comes into the picture.

Another question: thread vs new process. Different processes did not work properly the last time I tested so I want to know whether it is my mistake or running a different process IS a wrong way of doing it.

8 comments

[ 0.24 ms ] story [ 27.9 ms ] thread
What language are you most familiar with, and what language is currently being used for the "configurable script"?
I am most familiar with Python and I am using it for the complete work.

The Telegram bot is written using a async-based library (telethon).

I should have also explained this in the post that when the user asks the bot to run the script, I do a `thread.start()`

Have you looked into celery at all? You could setup a pretty simple task queue with celery and have multiple workers. Setup a main process with the bot who can put jobs on the queue, and then have a few worker processes that take jobs from the queue and do the work. Celery is pretty standard when doing background, long-running, scheduled, and async tasks.
I had not looked into it till now but checking it out now.

Do you have any recommendations to where I should learn more about using it? (I am checking the docs to get a basic understanding rn)

There should be plenty of documentation and info online if you just search "task queue celery python" or some similar string. It's pretty much the de facto standard in the Python community.
Is there a reason you're not using a webhook to capture updates from the API? Or do you just need to keep polling?

If you need to just keep polling, I'd recommend using a managed background task service like SWF on AWS or Cloud Tasks on GCP. Not worth the trouble of debugging to build it yourself these days

I kept using the most popular telegram API library which is based on async.

I will check out SWF, thanks for the help!