Ejemplo n.º 1
0
def lock_instance():
    try:
        lock = LockFile('file_syncer.lock')
    except LockError:
        print('Can not run multiple instance of this script. Try it later! :)')
        exit()
    yield
    lock.close()
Ejemplo n.º 2
0
def send_all(block_size=500, backend=None):
    """
    Send all non-deferred messages in the queue.

    A lock file is used to ensure that this process can not be started again
    while it is already running.

    The ``block_size`` argument allows for queued messages to be iterated in
    blocks, allowing new prioritised messages to be inserted during iteration
    of a large number of queued messages.

    """
    logger.debug("Acquiring lock...")
    try:
        lock = LockFile(LOCK_PATH)
    except LockError:
        logger.debug("Lock already in place. Exiting.")
        return
    logger.debug("Lock acquired.")

    start_time = time.time()

    sent = deferred = skipped = 0

    try:
        if constants.EMAIL_BACKEND_SUPPORT:
            connection = get_connection(backend=backend)
        else:
            connection = get_connection()
        blacklist = models.Blacklist.objects.values_list('email', flat=True)
        connection.open()
        for message in _message_queue(block_size):
            result = send_queued_message(message, smtp_connection=connection,
                                  blacklist=blacklist)
            if result == constants.RESULT_SENT:
                sent += 1
            elif result == constants.RESULT_FAILED:
                deferred += 1
            elif result == constants.RESULT_SKIPPED:
                skipped += 1
        connection.close()
    finally:
        logger.debug("Releasing lock...")
        lock.close()
        logger.debug("Lock released.")

    logger.debug("")
    if sent or deferred or skipped:
        log = logger.warning
    else:
        log = logger.info
    log("%s sent, %s deferred, %s skipped." % (sent, deferred, skipped))
    logger.debug("Completed in %.2f seconds." % (time.time() - start_time))
Ejemplo n.º 3
0
    def process_conversion_queue(self):
        """process the queue.
        """
        try:
            lock = LockFile(LOCKFILE_NAME)
        except LockError:
            return "`process_conversion_queue` is locked by another process (%r)." % (
                LOCKFILE_NAME)

        try:
            return self._process_conversion_queue()
        finally:
            lock.close()
Ejemplo n.º 4
0
    def process_conversion_queue(self):
        """process the queue.
        """
        try:
            lock = LockFile(LOCKFILE_NAME)
        except LockError:
            return "`process_conversion_queue` is locked by another process (%r)." % (
                LOCKFILE_NAME)

        try:
            return self._process_conversion_queue()
        finally:
            lock.close()
Ejemplo n.º 5
0
    def process_conversion_queue(self):
        """process the queue.
        """
        try:
            lock = LockFile(LOCKFILE_NAME)
        except LockError:
            return '`process_conversion_queue` is locked by another ' + \
                   'process ({0}).'.format(LOCKFILE_NAME)

        try:
            return self._process_conversion_queue()
        finally:
            lock.close()
Ejemplo n.º 6
0
    def process_conversion_queue(self):
        """process the queue.
        """
        try:
            lock = LockFile(LOCKFILE_NAME)
        except LockError:
            return '`process_conversion_queue` is locked by another ' + \
                   'process ({0}).'.format(LOCKFILE_NAME)

        try:
            return self._process_conversion_queue()
        finally:
            lock.close()
Ejemplo n.º 7
0
    def process_conversion_queue(self):
        """process the queue.
        """
        if HAS_PLONE_PROTECT:
            # Disabling CSRF protection
            alsoProvides(self.request, IDisableCSRFProtection)

        try:
            lock = LockFile(LOCKFILE_NAME)
        except LockError:
            return '`process_conversion_queue` is locked by another ' + \
                   'process ({0}).'.format(LOCKFILE_NAME)

        try:
            return self._process_conversion_queue()
        finally:
            lock.close()
Ejemplo n.º 8
0
    def process_conversion_queue(self):
        """process the queue.
        """
        if HAS_PLONE_PROTECT:
            # Disabling CSRF protection
            alsoProvides(self.request, IDisableCSRFProtection)

        try:
            lock = LockFile(LOCKFILE_NAME)
        except LockError:
            return '`process_conversion_queue` is locked by another ' + \
                   'process ({0}).'.format(LOCKFILE_NAME)

        try:
            return self._process_conversion_queue()
        finally:
            lock.close()
Ejemplo n.º 9
0
class LockTest(TestCase):
    """
    Tests for Django Mailer trying to send mail when the lock is already in
    place.
    """

    def setUp(self):
        # Create somewhere to store the log debug output.
        self.output = StringIO()
        # Create a log handler which can capture the log debug output.
        self.handler = logging.StreamHandler(self.output)
        self.handler.setLevel(logging.DEBUG)
        formatter = logging.Formatter('%(message)s')
        self.handler.setFormatter(formatter)
        # Add the log handler.
        logger = logging.getLogger('django_mailer')
        logger.addHandler(self.handler)

        # Use a test lock-file name in case something goes wrong, then emulate
        # that the lock file has already been acquired by another process.
        self.original_lock_path = engine.LOCK_PATH
        engine.LOCK_PATH += '.mailer-test'
        self.lock = LockFile(engine.LOCK_PATH)

    def tearDown(self):
        # Remove the log handler.
        logger = logging.getLogger('django_mailer')
        logger.removeHandler(self.handler)

        # Revert the lock file unique name
        engine.LOCK_PATH = self.original_lock_path
        self.lock.close()

    def test_locked(self):
        # Acquire the lock so that send_all will fail.
        engine.send_all()
        self.output.seek(0)
        self.assertEqual(self.output.readlines()[-1].strip(),
                         'Lock already in place. Exiting.')
Ejemplo n.º 10
0
def _do_base_backup(config):
    lock = LockFile(config['lock_file'])
    conn = connect("")
    cur = conn.cursor()
    label = datetime.now().strftime("%Y%m%d%H%M%S")
    cur.execute("SELECT pg_start_backup('%s');" % label)

    fi,file_name = mkstemp()
    fp = fdopen(fi,'w')
    tar_f = tarfile.open(fileobj=fp,mode='w:gz')
    tar_f.add(config['data_dir'],arcname="",exclude=lambda x: '/pg_xlog' in x)
    tar_f.close()
    fp.close()


    conn = connect_s3(config['access_key'], config['secret_key'])
    bucket = conn.get_bucket(config['bucket'])
    key = bucket.new_key(config['prefix']+"base_"+label+".tar.gz")
    key.set_contents_from_filename(file_name)

    unlink(file_name)
    cur.execute("SELECT pg_stop_backup();")
    lock.close()
Ejemplo n.º 11
0
                                     args=(log_queue, ))
    logger_thread.start()

    app = QtWidgets.QApplication(sys.argv)
    app.setWindowIcon(QtGui.QIcon(MiscUtils.get_app_icon_path()))
    app.setApplicationDisplayName(
        "Batch Media Compressor")  # TODO test + add org / ver
    app.setQuitOnLastWindowClosed(False)

    try:
        lock = LockFile(MiscUtils.get_lock_file_path())
        tray_icon = TrayIcon(log_queue)
        tray_icon.show()
        return_code = app.exec_()
        tray_icon.cleanup()
        lock.close()
    except LockError:
        error_msg = "Cannot acquire lock on file {}.\n\nAnother instance of the application is probably running.".format(
            MiscUtils.get_lock_file_path())
        logging.fatal(error_msg)
        QtWidgets.QMessageBox.critical(None, "Fatal Error", error_msg,
                                       QtWidgets.QMessageBox.Ok)
        return_code = -1

    logging.info("Application is being shutdown")
    log_queue.put(None)
    logging.debug("Waiting for logging thread to terminate")
    logger_thread.join()
    logging.debug("Exit Code: %s", return_code)
    sys.exit(return_code)