GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

commons.py 2.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
# -*- coding: utf-8 -*-
#
# This file is part of GetTor, a Tor Browser distribution system.
#
# :authors: isra <ilv@torproject.org>
#           see also AUTHORS file
#
# :copyright:   (c) 2008-2014, The Tor Project, Inc.
#               (c) 2014-2018, Israel Leiva
#
# :license: This is Free Software. See LICENSE for license information.

13 14
from twisted.logger import Logger, LogLevel
from twisted.logger import FilteringLogObserver, LogLevelFilterPredicate
15 16 17
from twisted.application import internet
# Define an application logger
log = Logger(namespace="gettor")
18 19
predicate = LogLevelFilterPredicate(defaultLogLevel=LogLevel.info)
log.observer = FilteringLogObserver(log.observer, [predicate])
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

class BaseService(internet.TimerService):
    """
    Base service for Email and Sendmail. It extends the TimerService.
    """

    def __init__(self, name, step, instance, *args, **kwargs):
        """
        Constructor.

        :param name (str): name of the service (just for logging purposes).
        :param step (float): time interval for TimerService, in seconds.
        :param instance (object): instance of Email, Sendmail classes
        """

        log.info("SERVICE:: Initializing {} service.".format(name))
        self.name = name
        self.instance = instance
        log.debug("SERVICE:: Initializing TimerService.")
        internet.TimerService.__init__(
            self, step, self.instance.get_new, **kwargs
        )

    def startService(self):
        """
        Starts the service. Overridden from parent class to add extra logging
        information.
        """
        log.info("SERVICE:: Starting {} service.".format(self.name))
        internet.TimerService.startService(self)
        log.info("SERVICE:: Service started.")

    def stopService(self):
        """
        Stop the service. Overridden from parent class to close connection to
        database, shutdown the service and add extra logging information.
        """
        log.info("SERVICE:: Stopping {} service.".format(self.name))
        internet.TimerService.stopService(self)
        log.info("SERVICE:: Service stopped.")