<commit_before>#
# Database Cursor
#

import pscheduler
import sys

this = sys.modules[__name__]

this.cursor = None

def dbcursor_init(dsn):
    """Connect to the database"""
    if this.cursor is None:
        this.cursor = pscheduler.pg_cursor(dsn)
        # TODO: What happens if the connection goes south?

def dbcursor():<commit_msg>Re-establish the database connection if the cursor comes up closed.  This will prevent any server thread from coughing up more than one error if the connection is reset.  Can't make it any more reliable without doing a round-trip to the server each time.  Related to #58.<commit_after>#
# Database Cursor
#

import pscheduler
import sys

from .log import log

this = sys.modules[__name__]

this.dsn = None
this.cursor = None

def dbcursor_init(dsn):
    """Connect to the database"""
    if this.cursor is None:
        this.cursor = pscheduler.pg_cursor(dsn)
        this.dsn = dsn

def dbcursor():
    if this.cursor.closed:
        log.warning("Database cursor is closed; reconnecting")
        this.cursor = None
        dbcursor_init(this.dsn)
{'fraction_non_alphanumeric': 0.08463661453541858, 'fraction_numerical': 0.0018399264029438822, 'mean_word_length': 3.3870967741935485, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1221586', 'n_tokens_mistral': 328, 'n_tokens_neox': 327, 'n_words': 120}