Changeset View
Changeset View
Standalone View
Standalone View
swh/vault/backend.py
# Copyright (C) 2017 The Software Heritage developers | # Copyright (C) 2017-2018 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import smtplib | import smtplib | ||||
import psycopg2 | import psycopg2 | ||||
from psycopg2.extras import RealDictCursor | from psycopg2.extras import RealDictCursor | ||||
from functools import wraps | from functools import wraps | ||||
from email.mime.text import MIMEText | from email.mime.text import MIMEText | ||||
from swh.model import hashutil | from swh.model import hashutil | ||||
from swh.scheduler.backend import SchedulerBackend | from swh.scheduler import get_scheduler | ||||
from swh.scheduler.utils import create_oneshot_task_dict | from swh.scheduler.utils import create_oneshot_task_dict | ||||
from swh.vault.cache import VaultCache | from swh.vault.cache import VaultCache | ||||
from swh.vault.cookers import get_cooker | from swh.vault.cookers import get_cooker | ||||
from swh.vault.cooking_tasks import SWHCookingTask # noqa | from swh.vault.cooking_tasks import SWHCookingTask # noqa | ||||
cooking_task_name = 'swh.vault.cooking_tasks.SWHCookingTask' | cooking_task_name = 'swh.vault.cooking_tasks.SWHCookingTask' | ||||
NOTIF_EMAIL_FROM = ('"Software Heritage Vault" ' | NOTIF_EMAIL_FROM = ('"Software Heritage Vault" ' | ||||
▲ Show 20 Lines • Show All 81 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() | self.smtp_server = smtplib.SMTP() | ||||
if self.config['scheduling_db'] is not None: | if self.config['scheduler'] is not None: | ||||
self.scheduler = SchedulerBackend( | self.scheduler = get_scheduler(**self.config['scheduler']) | ||||
scheduling_db=self.config['scheduling_db']) | |||||
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=RealDictCursor, | cursor_factory=RealDictCursor, | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 349 Lines • Show Last 20 Lines |