Skip to content

Running your own instance.

JK Tan edited this page Sep 28, 2018 · 6 revisions

Initial Setup

Apart from utilizing python to run this bot, this bot uses webhooks, and will require some additional knowledge of web server configuration to set up and run successfully.

I have not attempted any setup on a Windows server, you are on your own.

The bot is using methods that are only implemented in Python3.6. Ubuntu 18.04LTS supports python 3.6 as the default python3. Alternatively, if you're using debian, centos or other distros which do not have 3.6 as the default, you can either compile it from source or install it using the deadsnakes ppa.

Please do not attempt to replace the system Python.

This wiki page assumes you're running a debian based system. It is possible to set this up on another system, but you will need to do some research on your own. I have previously hosted it on centOS and Fedora server before.


Web Server

Nginx

Nginx is used as the preferred method for setting up a reverse proxy.

PTB's documentation on the reverse proxy should be sufficient for the setup.

For reference, this is my own nginx configuration that I am using.

(venv) jingkai@ubuntu-bionic:~$ cat /etc/nginx/sites-available/your.public.na.me
server {
    listen              443 ssl;
    server_name         your.public.na.me; // ie: bot.jingkai.com
    ssl_certificate     /openssl_keys/cert.pem;
    ssl_certificate_key /openssl_keys/private.key;

    location /**STAGING_TOKEN_HERE**{

        proxy_pass http://127.0.0.1:8080;
    }
    location /**LIVE_TOKEN_HERE**{
	proxy_pass http://127.0.0.1:8081;
    }
}

Replace STAGING_TOKEN_HERE and LIVE_TOKEN_HERE respectively (Or simply use one port, whatever you prefer.)

Replace your.public.na.me with your front facing domain.


Data Stores and Message Brokers

MongoDB

MongoDB is used as the primary backend database.

DigitalOcean has some good guides on how to install MongoDB. Note that you should probably secure it after that, as mongo is notorious for having unsafe practices in their default installation configuration.

Redis

Redis is used as an in-memory data store. It's currently used to communicate between the bot application and celery.

DigitalOcean has some good guides on setting up Redis.

RabbitMQ

RabbitMQ acts as the message broker for Celery. Celery's documentation does cover how to get started with rabbitMQ as the default broker.


Additional Programs

Google Chrome

google-chrome-stable is avaliable as a ppa on debian-based system. You will need this for running the bot as it uses a headless instance to scrape records from SIMConnect


Setup and Running an instance.

  • Make a folder where you want to run the bot

    mkdir src; cd src

  • Initalise a git repository and do a git clone.

    git init

    git clone https://github.com/xlanor/SIM-UoW-Timetable-bot-v2.git

  • Optional: Set up your own virtualenv.

  • do a pip install requirements.txt

  • Edit the configuration file to fill up the proper values.

  • Run celery with

    celery -A cel worker -l info -f /your/log/location/celery.log

    You can also run this with the --concurrency flag, but I cheaped out on the server so I can't do that

    I could have ran this as a system service but it was throwing some errors, so I used nohup to run this.

  • Run the bot with python3 bot.py

    You can also daemonise it with systemd or initd. This is an example of my system service with venv.

(venv) jingkai@ubuntu-bionic:/projects/timetable_v2/src$ cat /etc/systemd/system/hera.service
[Unit]
Description=Hera - Timetable bot v2
After=network.target

[Service]
User=jingkai
Group=jingkai
Environment="PATH=/projects/timetable_v2/venv/bin"
WorkingDirectory=/projects/timetable_v2/src
ExecStart=/projects/timetable_v2/venv/bin/python bot.py
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target