SLACK MEETUP BOT - DEPLOYMENT

In school, when we worked on the project as a team, we just hosted the app on Heroku. With Heroku, the app eventually went into hibernation. On my personal portfolio, I wanted people to have the ability to test out the app. Also to add to that trouble MongoDB was not supported by Heroku anymore at the time of writing. So I needed to keep the app active on Heroku, move the database from Heroku, and keep the database active because the database was also going to sleep.

April 4, 2022

So what per se is the problem?

When the app was built in school yet, the goal was just to get a working app. Longer-term hosting wasn't a priority because we only needed it for class. On my portfolio site I am trying to have all the apps and projects I worked on to have a working prototype. I want people to have the chance to play around with my apps and see them in action. Something tangible is worth more than just words.

The way Heroku works is after a period of inactivity the app will go to sleep to conserve resources. It will wake up after it gets something trying to access it. The Slack channel that has the bot running in is always active. If I try to use the bot in the channel when the Heroku app server is down I will just not get any reply. That isn't necessarily a problem because after a few moments after running the first command I can run it again and it will work. The app will wake up in those few moments. I personally didn't want to settle with that experience. I was hoping to have the app work every time.

That wasn't the only issue though. The app was built over a year ago. Heroku has stopped supporting MongoDB since then. I had to have the database hosted externally from Heroku. Once I figured out how to do that I ran into another issue. After some time of inactivity on the database server, the database would also go to sleep. So now if I wake up the app on Heroku it still won't work. It needs the database but the database is asleep and doesn't just wake up. It needs to get activated manually by logging into the dashboard for the MongoDB database.

Stirring up the gray matter

I had a few ideas of how to fix the various issues. The main problem I needed to address was how to keep the Heroku server up for the app. I had a chron job running on the bot server that would repopulate the database with more current data every 12 hours. That was enough to keep the database in active status. Therefore the only work that needed to get done with the database was to move it out of Heroku. That was just a quick google search and tutorial.

Javascript script

My first idea was to have a short javascript script running on my personal portfolio. Have it ping my bot server every 30 minutes. I wasn't too happy with that solution because my personal portfolio repository would have code in it that isn't in any way related to it. It should work though. While researching this method I got the idea of running a chron job on my personal DigitalOcean server. I figured it was possible but it wasn't my first idea because I thought it would be just super easy to run a javascript script. After more research it seemed like running a chron job on my server is just as easy if not easier yet.

Chron job on my server

I guess it would have been possible to just create a separate Heroku app that would only do one thing of pinging my bot server. I would then need something to keep that app awake. I think that would have worked but I wasn't a hundred percent sure. I suspected Heroku might require external requests to the server outside of Heroku to keep it awake. Either way, I didn't think that was too clean. My personal server was completely set up by me and I controlled what ran and how. That seemed like a much more solid solution.

Turns out an ubuntu server comes with chron job functionality built-in out of the box without needing to install anything. Researching how to run the chron job it made sense in my mind to create a chron job that would run a file every 30 minutes that would have a script to ping my bot server. I was thinking of writing a python script that would do what I needed. After more research, I realized this is probably all possible within a single chron job script.

I'm not sure I understood everything correctly but it seemed like just pinging my bot server from the DigitalOcean server wasn't super straightforward. From what I understood the ping command would need root permissions. For some reason, it wouldn't want to work. It would not wake up the bot server.

I later realized I could basically ping the server with a curl command. That did wake up the bot server. One thing that worried me was I'd be downloading data from the bot server with the curl command every 30 minutes. I wasn't sure how the DigitalOcean server deals with that data. I didn't want it to keep writing it to temporary storage anywhere on the server and hogging up all the storage on my server. So what I came up with in the end was a curl command that would cut out just the headers from the response and more specifically just the status header. Then I would just write that to a built-in log on the server that just deletes everything that gets written to it. Turns out all that functionality is available on the server by default.

Basic solution summarized

So basically all I really had to do was set up a chron job and run a curl command requesting data from the bot server. I would then delete the data in the response. This would keep the Heroku app active and while the app is active, it keeps the database active by writing new data to it every 12 hours.

Not perfect but it works

In the end, this is still not a perfect solution. The link to join the Slack channel will automatically add users only from a couple of email domains. Specifically, only accounts with google.com, inbox.com, and mail.com emails could be added automatically. Everyone else would have to send me a request and I would have to manually approve them. There was no way around this. This was a limiting factor from Slack's end. I'm not sure why they are so limited.

So the Slack channel is always active now. As is the bot server and database. If you want to check it out, use the live app link at the top of the page or just go here. If you need to get approved, send me a request through that link and I'll approve anyone as soon as possible.

Further Reading

About the Slack Meetup Bot

APR

04

2022