nxy irc bot
Go to file
jkhsjdhjs e03f5d0a43 add auto reconnect for postgres
This only works on the second database interaction, since psycopg2 only notices that
the connection is gone, when a query is executed.

So in the common case reconnect works as follows:
- some bot method calls a cursor function like .execute(), .fetchone(), etc.
  - this raises an error if the connection is broken
  - if following code then requests a new cursor, this will also fail since psycopg2
    now knows that the connection is gone
  - the error is caught in storage.DBConn.cursor(), a new connection will be set up
    of which a new cursor is yielded
If the error happens in connection.commit() or .rollback() instead we can instantly
reconnect since these methods are wrapped.

So why not wrap the cursor methods as well?
Consider the following example:
A query is the last thing that was executed on a cursor.
The database connection is lost.
Now .fetchone() is called on the cursor.
We could wrap .fetchone() and reconnect, but we'd have to use a new cursor since
cursors are linked to connections. And on this new cursor .fetchone() wouldn't
make any sense, since we haven't executed a query on this cursor.
2020-03-16 21:51:32 +00:00
bot add auto reconnect for postgres 2020-03-16 21:51:32 +00:00
files make quote nicks case insensitive 2019-03-31 18:32:44 +00:00
.gitignore Ignore vim .swp files 2017-09-05 21:24:09 +02:00
FORMATTING.md Massive refactoring/-structuring 2017-07-07 02:49:37 +02:00
README.md make quote nicks case insensitive 2019-03-31 18:32:44 +00:00
requirements.txt update requirements 2019-12-04 16:17:08 +00:00
TODOs Fixed bug in rainbow function which breaks output with numbers 2017-08-01 15:50:12 +02:00
tox.ini Updated flake8 settings 2017-08-22 19:02:45 +02:00

PROJECT ABANDONED

Installation

Open psql as user postgres:

psql -U postgres

Create a user with password, allowed to login and a database:

CREATE ROLE nxy WITH ENCRYPTED PASSWORD 'your-password';
ALTER ROLE nxy LOGIN;
CREATE DATABASE nxy_prod OWNER nxy;
\c nxy_prod
CREATE EXTENSION citext;
\q

Apply database schema and optionally restore a dump:

psql -U nxy -d nxy_prod < nxy/files/schema.sql
psql -U nxy -d nxy_prod < path/to/dump.sql

Clone git repo, create virtual env, activate it and install dependencies:

git clone https://gitfap.de/mrhanky/nxy.git
python3 -m venv env
source env/bin/activate
pip install -U -r nxy/requirements.txt

Copy sample configs to $HOME and edit them:

cp nxy/files/{.env,config.json} .
vim .env config.json

Running as Service

Enable linger for the bot user (so it starts at boot and keeps running), install and activate systemd unit:

sudo loginctl enable-linger nxy
mkdir -p $HOME/.config/systemd/user
ln -fs $HOME/nxy/files/nxy-bot.service $HOME/.config/systemd/user
systemctl --user enable --now nxy-bot.service

Install and activate timer for database dumps:

ln -fs $HOME/nxy/files/nxy-db-dump.{timer,service} $HOME/.config/systemd/user
systemctl --user enable --now nxy-db-dump.timer

Running as Standalone in Development Mode

For development set the BOT_DEV variable:

export BOT_DEV=

Then add the current git repository to your python path:

export PYTHONPATH=<git repository>:$PYTHONPATH

Finally run nxy:

python3 -m bot