Skip to content

Commit 27a18f6

Browse files
committed
Merge branch 'gonzobot+milkshake-data-fix' of github.com:linuxdaemon/CloudBot into gonzobot+milkshake-data-fix
2 parents 8a5d87c + 9cf5a8c commit 27a18f6

2 files changed

Lines changed: 48 additions & 23 deletions

File tree

data/gnomecards.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@
571571
"It's a pity that kids these days are all getting involved with {}.",
572572
"In 1,000 years, when paper money is but a distant memory, {} will be our currency.",
573573
"Major League Baseball has banned {} for giving players an unfair advantage.",
574-
"What is Batman's guilty pleasure?",
574+
"What is Batman's guilty pleasure? {}",
575575
"Next from J.K. Rowling: Harry Potter and the Chamber of {}.",
576576
"I'm sorry, Professor, but I couldn't complete my homework because of {}.",
577577
"What did I bring back from Mexico? {}",
@@ -660,4 +660,4 @@
660660
"And I would have gotten away with it, too, if it hadn't been for {}.",
661661
"What brought the orgy to a grinding halt? {}"
662662
]
663-
}
663+
}

plugins/grab.py

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import logging
12
import random
2-
33
from collections import defaultdict
4+
from threading import RLock
5+
46
from sqlalchemy import Table, Column, String
7+
from sqlalchemy.exc import SQLAlchemyError
8+
59
from cloudbot import hook
610
from cloudbot.util import database
711

@@ -18,19 +22,25 @@
1822
)
1923

2024
grab_cache = {}
25+
grab_locks = defaultdict(dict)
26+
grab_locks_lock = RLock()
27+
cache_lock = RLock()
28+
29+
logger = logging.getLogger("cloudbot")
2130

2231

2332
@hook.on_start()
2433
def load_cache(db):
2534
"""
2635
:type db: sqlalchemy.orm.Session
2736
"""
28-
grab_cache.clear()
29-
for row in db.execute(table.select().order_by(table.c.time)):
30-
name = row["name"].lower()
31-
quote = row["quote"]
32-
chan = row["chan"]
33-
grab_cache.setdefault(chan, {}).setdefault(name, []).append(quote)
37+
with cache_lock:
38+
grab_cache.clear()
39+
for row in db.execute(table.select().order_by(table.c.time)):
40+
name = row["name"].lower()
41+
quote = row["quote"]
42+
chan = row["chan"]
43+
grab_cache.setdefault(chan, {}).setdefault(name, []).append(quote)
3444

3545

3646
def two_lines(bigstring, chan):
@@ -90,27 +100,42 @@ def grab_add(nick, time, msg, chan, db, conn):
90100
load_cache(db)
91101

92102

103+
def get_latest_line(conn, chan, nick):
104+
for name, timestamp, msg in reversed(conn.history[chan]):
105+
if nick.casefold() == name.casefold():
106+
return name, timestamp, msg
107+
108+
return None, None, None
109+
110+
93111
@hook.command()
94112
def grab(text, nick, chan, db, conn):
95113
"""grab <nick> grabs the last message from the
96114
specified nick and adds it to the quote database"""
97115
if text.lower() == nick.lower():
98116
return "Didn't your mother teach you not to grab yourself?"
99117

100-
for item in conn.history[chan].__reversed__():
101-
name, timestamp, msg = item
102-
if text.lower() == name.lower():
103-
# check to see if the quote has been added
104-
if check_grabs(name.lower(), msg, chan):
105-
return "I already have that quote from {} in the database".format(text)
106-
else:
107-
# the quote is new so add it to the db.
108-
grab_add(name.lower(),timestamp, msg, chan, db, conn)
109-
if check_grabs(name.lower(), msg, chan):
110-
return "the operation succeeded."
111-
else:
112-
return "the operation failed"
113-
return "I couldn't find anything from {} in recent history.".format(text)
118+
with grab_locks_lock:
119+
grab_lock = grab_locks[conn.name.casefold()].setdefault(chan.casefold(), RLock())
120+
121+
with grab_lock:
122+
name, timestamp, msg = get_latest_line(conn, chan, text)
123+
if not msg:
124+
return "I couldn't find anything from {} in recent history.".format(text)
125+
126+
if check_grabs(text.casefold(), msg, chan):
127+
return "I already have that quote from {} in the database".format(text)
128+
129+
try:
130+
grab_add(name.casefold(), timestamp, msg, chan, db, conn)
131+
except SQLAlchemyError:
132+
logger.exception("Error occurred when grabbing %s in %s", name, chan)
133+
return "Error occurred."
134+
135+
if check_grabs(name.casefold(), msg, chan):
136+
return "the operation succeeded."
137+
else:
138+
return "the operation failed"
114139

115140

116141
def format_grab(name, quote):

0 commit comments

Comments
 (0)