Changeset View
Changeset View
Standalone View
Standalone View
swh/vault/backend.py
Show First 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | class VaultBackend: | ||||
""" | """ | ||||
Backend for the Software Heritage vault. | Backend for the Software Heritage vault. | ||||
""" | """ | ||||
def __init__(self, config): | def __init__(self, config): | ||||
self.config = config | self.config = config | ||||
self.cache = VaultCache(self.config['cache']) | self.cache = VaultCache(self.config['cache']) | ||||
self.db = None | self.db = None | ||||
self.reconnect() | self.reconnect() | ||||
self.smtp_server = smtplib.SMTP('localhost') | self.smtp_server = smtplib.SMTP('localhost', 25) | ||||
def reconnect(self): | def reconnect(self): | ||||
"""Reconnect to the database.""" | """Reconnect to the database.""" | ||||
if not self.db or self.db.closed: | if not self.db or self.db.closed: | ||||
self.db = psycopg2.connect( | self.db = psycopg2.connect( | ||||
dsn=self.config['db'], | dsn=self.config['db'], | ||||
cursor_factory=psycopg2.extras.RealDictCursor, | cursor_factory=psycopg2.extras.RealDictCursor, | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 172 Lines • ▼ Show 20 Lines | def send_notification(self, n_id, email, obj_type, obj_id, cursor=None): | ||||
text = NOTIF_EMAIL_BODY.strip() | text = NOTIF_EMAIL_BODY.strip() | ||||
text = text.format(obj_type=obj_type, hex_id=hex_id, url=url) | text = text.format(obj_type=obj_type, hex_id=hex_id, url=url) | ||||
msg = MIMEText(text) | msg = MIMEText(text) | ||||
msg['Subject'] = (NOTIF_EMAIL_SUBJECT | msg['Subject'] = (NOTIF_EMAIL_SUBJECT | ||||
.format(obj_type=obj_type, short_id=short_id)) | .format(obj_type=obj_type, short_id=short_id)) | ||||
msg['From'] = NOTIF_EMAIL_FROM | msg['From'] = NOTIF_EMAIL_FROM | ||||
msg['To'] = email | msg['To'] = email | ||||
self.smtp_server.send_message(msg) | self._smtp_send(msg) | ||||
if n_id is not None: | if n_id is not None: | ||||
cursor.execute(''' | cursor.execute(''' | ||||
DELETE FROM vault_notif_email | DELETE FROM vault_notif_email | ||||
WHERE id = %s''', (n_id,)) | WHERE id = %s''', (n_id,)) | ||||
def _smtp_send(self, msg): | |||||
# Reconnect if needed | |||||
try: | |||||
status = self.smtp_server.noop()[0] | |||||
except: # smtplib.SMTPServerDisconnected | |||||
status = -1 | |||||
if status != 250: | |||||
self.smtp_server.connect() | |||||
# Send the message | |||||
self.smtp_server.send_message(msg) | |||||
@autocommit | @autocommit | ||||
def _cache_expire(self, cond, *args, cursor=None): | def _cache_expire(self, cond, *args, cursor=None): | ||||
"""Low-level expiration method, used by cache_expire_* methods""" | """Low-level expiration method, used by cache_expire_* methods""" | ||||
# Embedded SELECT query to be able to use ORDER BY and LIMIT | # Embedded SELECT query to be able to use ORDER BY and LIMIT | ||||
cursor.execute(''' | cursor.execute(''' | ||||
DELETE FROM vault_bundle | DELETE FROM vault_bundle | ||||
WHERE ctid IN ( | WHERE ctid IN ( | ||||
SELECT ctid | SELECT ctid | ||||
Show All 23 Lines |