mirror of
https://github.com/agessaman/meshcore-bot.git
synced 2026-07-28 14:29:30 +00:00
Refactor database connection handling in GreeterCommand to use centralized method
- Introduced a new get_connection method in DBManager to standardize database connection creation with proper configuration. - Updated all instances in GreeterCommand where sqlite3.connect was used to utilize the new get_connection method, improving code consistency and maintainability.
This commit is contained in:
@@ -45,7 +45,7 @@ class GreeterCommand(BaseCommand):
|
||||
# Auto-start rollout if enabled, rollout_days > 0, and no active rollout exists
|
||||
if self.enabled and self.rollout_days > 0:
|
||||
try:
|
||||
with sqlite3.connect(self.bot.db_manager.db_path) as conn:
|
||||
with self.bot.db_manager.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
# Check for active rollout (more robust check)
|
||||
cursor.execute('''
|
||||
@@ -182,7 +182,7 @@ class GreeterCommand(BaseCommand):
|
||||
def _init_greeter_tables(self):
|
||||
"""Initialize database tables for greeter tracking"""
|
||||
try:
|
||||
with sqlite3.connect(self.bot.db_manager.db_path) as conn:
|
||||
with self.bot.db_manager.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Create greeted_users table for tracking who has been greeted
|
||||
@@ -232,7 +232,7 @@ class GreeterCommand(BaseCommand):
|
||||
return
|
||||
|
||||
try:
|
||||
with sqlite3.connect(self.bot.db_manager.db_path) as conn:
|
||||
with self.bot.db_manager.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Check if there's an active rollout
|
||||
@@ -284,7 +284,7 @@ class GreeterCommand(BaseCommand):
|
||||
def _mark_active_users_as_greeted(self, rollout_id: int):
|
||||
"""Mark all users who have posted on public channels during rollout period as greeted"""
|
||||
try:
|
||||
with sqlite3.connect(self.bot.db_manager.db_path) as conn:
|
||||
with self.bot.db_manager.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Get rollout start date
|
||||
@@ -372,7 +372,7 @@ class GreeterCommand(BaseCommand):
|
||||
return {'success': False, 'error': 'Greeter is disabled'}
|
||||
|
||||
try:
|
||||
with sqlite3.connect(self.bot.db_manager.db_path) as conn:
|
||||
with self.bot.db_manager.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Check if message_stats table exists
|
||||
@@ -488,7 +488,7 @@ class GreeterCommand(BaseCommand):
|
||||
|
||||
rollout_days = days or self.rollout_days
|
||||
|
||||
with sqlite3.connect(self.bot.db_manager.db_path) as conn:
|
||||
with self.bot.db_manager.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Check if there's already an active rollout
|
||||
@@ -564,7 +564,7 @@ class GreeterCommand(BaseCommand):
|
||||
return None
|
||||
|
||||
try:
|
||||
with sqlite3.connect(self.bot.db_manager.db_path) as conn:
|
||||
with self.bot.db_manager.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
|
||||
if self.per_channel_greetings:
|
||||
@@ -606,7 +606,7 @@ class GreeterCommand(BaseCommand):
|
||||
True if user has been greeted (globally or on this channel, depending on config)
|
||||
"""
|
||||
try:
|
||||
with sqlite3.connect(self.bot.db_manager.db_path) as conn:
|
||||
with self.bot.db_manager.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
|
||||
if self.per_channel_greetings:
|
||||
@@ -653,10 +653,9 @@ class GreeterCommand(BaseCommand):
|
||||
True if user was marked (or already marked), False on error
|
||||
"""
|
||||
try:
|
||||
db_path = self.bot.db_manager.db_path
|
||||
self.logger.debug(f"Marking {sender_id} as greeted (channel: {channel}, db: {db_path})")
|
||||
self.logger.debug(f"Marking {sender_id} as greeted (channel: {channel})")
|
||||
|
||||
with sqlite3.connect(db_path, timeout=10.0) as conn:
|
||||
with self.bot.db_manager.get_connection() as conn:
|
||||
# Use WAL mode for better concurrency (if not already enabled)
|
||||
# This helps with race conditions
|
||||
conn.execute('PRAGMA journal_mode=WAL')
|
||||
@@ -783,7 +782,7 @@ class GreeterCommand(BaseCommand):
|
||||
def get_greeted_users_count(self) -> int:
|
||||
"""Get count of users who have been greeted (for verification)"""
|
||||
try:
|
||||
with sqlite3.connect(self.bot.db_manager.db_path) as conn:
|
||||
with self.bot.db_manager.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('SELECT COUNT(*) FROM greeted_users')
|
||||
count = cursor.fetchone()[0]
|
||||
@@ -795,7 +794,7 @@ class GreeterCommand(BaseCommand):
|
||||
def _cleanup_duplicate_greetings(self):
|
||||
"""Remove duplicate entries from greeted_users table, keeping the earliest (first) greeting"""
|
||||
try:
|
||||
with sqlite3.connect(self.bot.db_manager.db_path) as conn:
|
||||
with self.bot.db_manager.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Find duplicates - count how many exist per (sender_id, channel)
|
||||
@@ -855,8 +854,7 @@ class GreeterCommand(BaseCommand):
|
||||
def get_recent_greeted_users(self, limit: int = 10) -> List[Dict[str, Any]]:
|
||||
"""Get recent greeted users (for verification) - ordered by first greeting time"""
|
||||
try:
|
||||
with sqlite3.connect(self.bot.db_manager.db_path) as conn:
|
||||
conn.row_factory = sqlite3.Row
|
||||
with self.bot.db_manager.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('''
|
||||
SELECT sender_id, channel, MIN(greeted_at) as greeted_at,
|
||||
@@ -909,7 +907,7 @@ class GreeterCommand(BaseCommand):
|
||||
# Get recent activity from message_stats if available
|
||||
if info['recent_activity_24h'] == 0:
|
||||
try:
|
||||
with sqlite3.connect(self.bot.db_manager.db_path) as conn:
|
||||
with self.bot.db_manager.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
# Check if message_stats table exists
|
||||
cursor.execute('''
|
||||
@@ -1005,7 +1003,7 @@ class GreeterCommand(BaseCommand):
|
||||
def _is_rollout_active(self) -> bool:
|
||||
"""Check if there's an active rollout period"""
|
||||
try:
|
||||
with sqlite3.connect(self.bot.db_manager.db_path) as conn:
|
||||
with self.bot.db_manager.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
# Use SQLite's datetime functions to calculate end date and compare with current time
|
||||
# This handles timezone issues automatically since both are in UTC
|
||||
@@ -1068,7 +1066,7 @@ class GreeterCommand(BaseCommand):
|
||||
return False
|
||||
|
||||
try:
|
||||
with sqlite3.connect(self.bot.db_manager.db_path) as conn:
|
||||
with self.bot.db_manager.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Check if message_stats table exists
|
||||
@@ -1301,7 +1299,7 @@ class GreeterCommand(BaseCommand):
|
||||
# If the record was just created (within last 5 seconds), we likely created it
|
||||
# If it's older, another process may have created it first
|
||||
try:
|
||||
with sqlite3.connect(self.bot.db_manager.db_path) as conn:
|
||||
with self.bot.db_manager.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
if self.per_channel_greetings:
|
||||
cursor.execute('''
|
||||
|
||||
@@ -500,6 +500,16 @@ class DBManager:
|
||||
"""Set bot start time in metadata"""
|
||||
self.set_metadata('start_time', str(start_time))
|
||||
|
||||
def get_connection(self) -> sqlite3.Connection:
|
||||
"""Get a database connection with proper configuration
|
||||
|
||||
Returns:
|
||||
sqlite3.Connection with row factory and timeout configured
|
||||
"""
|
||||
conn = sqlite3.connect(str(self.db_path), timeout=30.0)
|
||||
conn.row_factory = sqlite3.Row
|
||||
return conn
|
||||
|
||||
def set_system_health(self, health_data: Dict[str, Any]):
|
||||
"""Store system health data in metadata"""
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user