Example #1
0
 def process_request(self, request):
     """Begin a transaction if one doesn't already exist."""
     try:
         commands.begin()
     except OperationFailure as err:
         message = utils.get_error_message(err)
         if messages.TRANSACTION_EXISTS_ERROR not in message:
             raise err
Example #2
0
 def process_request(self, request):
     """Begin a transaction if one doesn't already exist."""
     try:
         commands.begin()
     except OperationFailure as err:
         message = utils.get_error_message(err)
         if messages.TRANSACTION_EXISTS_ERROR not in message:
             raise err
Example #3
0
def make_shell_context(auto_transact=True):
    from modularodm import Q
    from framework.auth import User, Auth
    from framework.mongo import database
    from website.app import init_app
    from website.project.model import Node
    from website import models  # all models
    from website import settings
    import requests
    from framework.transactions import commands
    from framework.transactions import context as tcontext

    app = init_app()

    def commit():
        commands.commit()
        print("Transaction committed.")
        if auto_transact:
            commands.begin()
            print("New transaction opened.")

    def rollback():
        commands.rollback()
        print("Transaction rolled back.")
        if auto_transact:
            commands.begin()
            print("New transaction opened.")

    context = {
        "transaction": tcontext.TokuTransaction,
        "start_transaction": commands.begin,
        "commit": commit,
        "rollback": rollback,
        "app": app,
        "db": database,
        "User": User,
        "Auth": Auth,
        "Node": Node,
        "Q": Q,
        "models": models,
        "run_tests": test,
        "rget": requests.get,
        "rpost": requests.post,
        "rdelete": requests.delete,
        "rput": requests.put,
        "settings": settings,
    }
    try:  # Add a fake factory for generating fake names, emails, etc.
        from faker import Factory

        fake = Factory.create()
        context["fake"] = fake
    except ImportError:
        pass
    if auto_transact:
        commands.begin()
    return context
Example #4
0
 def test_before_request_transaction_active(self):
     commands.begin()
     transactions_before = database.command('showLiveTransactions')
     handlers.transaction_before_request()
     transactions = database.command('showLiveTransactions')
     assert_equal(len(transactions['transactions']), 1)
     assert_not_equal(
         transactions_before['transactions'][0]['txnid'],
         transactions['transactions'][0]['txnid'],
     )
Example #5
0
 def __enter__(self):
     try:
         commands.begin(self.database)
         self.pending = True
     except OperationFailure as error:
         message = utils.get_error_message(error)
         if messages.TRANSACTION_EXISTS_ERROR not in message:
             raise
         logger.warn('Transaction already in progress')
     return self
Example #6
0
 def __enter__(self):
     try:
         commands.begin(self.database)
         self.pending = True
     except OperationFailure as error:
         message = utils.get_error_message(error)
         if messages.TRANSACTION_EXISTS_ERROR not in message:
             raise
         logger.warn('Transaction already in progress')
     return self
Example #7
0
 def test_before_request_transaction_active(self):
     commands.begin()
     transactions_before = database.command('showLiveTransactions')
     handlers.transaction_before_request()
     transactions = database.command('showLiveTransactions')
     assert_equal(len(transactions['transactions']), 1)
     assert_not_equal(
         transactions_before['transactions'][0]['txnid'],
         transactions['transactions'][0]['txnid'],
     )
Example #8
0
def make_shell_context(auto_transact=True):
    from modularodm import Q
    from framework.auth import User, Auth
    from framework.mongo import database
    from website.app import init_app
    from website.project.model import Node
    from website import models  # all models
    from website import settings
    import requests
    from framework.transactions import commands
    from framework.transactions import context as tcontext
    app = init_app()

    def commit():
        commands.commit()
        print('Transaction committed.')
        if auto_transact:
            commands.begin()
            print('New transaction opened.')

    def rollback():
        commands.rollback()
        print('Transaction rolled back.')
        if auto_transact:
            commands.begin()
            print('New transaction opened.')

    context = {
        'transaction': tcontext.TokuTransaction,
        'start_transaction': commands.begin,
        'commit': commit,
        'rollback': rollback,
        'app': app,
        'db': database,
        'User': User,
        'Auth': Auth,
        'Node': Node,
        'Q': Q,
        'models': models,
        'run_tests': test,
        'rget': requests.get,
        'rpost': requests.post,
        'rdelete': requests.delete,
        'rput': requests.put,
        'settings': settings,
    }
    try:  # Add a fake factory for generating fake names, emails, etc.
        from faker import Factory
        fake = Factory.create()
        context['fake'] = fake
    except ImportError:
        pass
    if auto_transact:
        commands.begin()
    return context
Example #9
0
def make_shell_context(auto_transact=True):
    from modularodm import Q
    from framework.auth import User, Auth
    from framework.mongo import database
    from website.app import init_app
    from website.project.model import Node
    from website import models  # all models
    from website import settings
    import requests
    from framework.transactions import commands
    from framework.transactions import context as tcontext
    app = init_app()

    def commit():
        commands.commit()
        print('Transaction committed.')
        if auto_transact:
            commands.begin()
            print('New transaction opened.')

    def rollback():
        commands.rollback()
        print('Transaction rolled back.')
        if auto_transact:
            commands.begin()
            print('New transaction opened.')

    context = {
        'transaction': tcontext.TokuTransaction,
        'start_transaction': commands.begin,
        'commit': commit,
        'rollback': rollback,
        'app': app,
        'db': database,
        'User': User,
        'Auth': Auth,
        'Node': Node,
        'Q': Q,
        'models': models,
        'run_tests': test,
        'rget': requests.get,
        'rpost': requests.post,
        'rdelete': requests.delete,
        'rput': requests.put,
        'settings': settings,
    }
    try:  # Add a fake factory for generating fake names, emails, etc.
        from faker import Factory
        fake = Factory.create()
        context['fake'] = fake
    except ImportError:
        pass
    if auto_transact:
        commands.begin()
    return context
Example #10
0
 def test_after_request_uncaught_exception(self):
     commands.begin()
     key = 'test_after_request_uncaught_exception'
     database['txn'].insert({'_id': key})
     response = make_response('ack!', 500)
     handlers.transaction_after_request(response)
     transactions = database.command('showLiveTransactions')
     assert_equal(len(transactions['transactions']), 0)
     assert_equal(
         database['txn'].find({'_id': key}).count(),
         0,
     )
Example #11
0
def transaction_before_request():
    """Setup transaction before handling the request.
    """
    if view_has_annotation(NO_AUTO_TRANSACTION_ATTR):
        return None
    try:
        commands.rollback()
        logger.error('Transaction already in progress; rolling back.')
    except OperationFailure as error:
        message = utils.get_error_message(error)
        if messages.NO_TRANSACTION_ERROR not in message:
            raise
    commands.begin()
Example #12
0
def transaction_before_request():
    """Setup transaction before handling the request.
    """
    if view_has_annotation(NO_AUTO_TRANSACTION_ATTR):
        return None
    try:
        commands.rollback()
        logger.error('Transaction already in progress; rolling back.')
    except OperationFailure as error:
        message = utils.get_error_message(error)
        if messages.NO_TRANSACTION_ERROR not in message:
            raise
    commands.begin()
Example #13
0
 def test_after_request_uncaught_exception(self):
     commands.begin()
     key = 'test_after_request_uncaught_exception'
     database['txn'].insert({'_id': key})
     response = make_response('ack!', 500)
     handlers.transaction_after_request(response)
     transactions = database.command('showLiveTransactions')
     assert_equal(len(transactions['transactions']), 0)
     assert_equal(
         database['txn'].find({
             '_id': key
         }).count(),
         0,
     )
Example #14
0
 def test_after_request_lock_error(self):
     commands.begin()
     key = 'test_after_request_lock_error'
     database['txn'].insert({'_id': key})
     with app.test_request_context(content_type='application/json'):
         response = make_response('bob', 200)
         with mock.patch('framework.transactions.commands.commit') as mock_commit:
             mock_commit.side_effect = OperationFailure(messages.LOCK_ERROR)
             handlers.transaction_after_request(response)
     transactions = database.command('showLiveTransactions')
     assert_equal(len(transactions['transactions']), 0)
     assert_equal(
         database['txn'].find({'_id': key}).count(),
         0,
     )
Example #15
0
 def test_after_request_lock_error(self):
     commands.begin()
     key = 'test_after_request_lock_error'
     database['txn'].insert({'_id': key})
     with app.test_request_context(content_type='application/json'):
         response = make_response('bob', 200)
         with mock.patch(
                 'framework.transactions.commands.commit') as mock_commit:
             mock_commit.side_effect = OperationFailure(messages.LOCK_ERROR)
             handlers.transaction_after_request(response)
     transactions = database.command('showLiveTransactions')
     assert_equal(len(transactions['transactions']), 0)
     assert_equal(
         database['txn'].find({
             '_id': key
         }).count(),
         0,
     )
Example #16
0
 def rollback():
     commands.rollback()
     print('Transaction rolled back.')
     if auto_transact:
         commands.begin()
         print('New transaction opened.')
Example #17
0
 def commit():
     commands.commit()
     print('Transaction committed.')
     if auto_transact:
         commands.begin()
         print('New transaction opened.')
Example #18
0
 def rollback():
     commands.rollback()
     print('Transaction rolled back.')
     if auto_transact:
         commands.begin()
         print('New transaction opened.')
Example #19
0
 def commit():
     commands.commit()
     print('Transaction committed.')
     if auto_transact:
         commands.begin()
         print('New transaction opened.')