<commit_before>import threading
import transaction

from pyramid.testing import DummyRequest
from pyramid.threadlocal import (
        get_current_registry, get_current_request, manager)
from substanced.interfaces import IUserLocator
from substanced.principal import DefaultUserLocator
from substanced.util import get_oid

from dace.interfaces import IEntity, IBusinessAction
from dace.processinstance.event import DelayedCallback
from dace.util import find_catalog
from dace.z3 import BaseJob
from dace import log

last_transaction_by_machine = threading.local()
CRAWLERS = []


def _call_action(action):
    transaction.begin()
    try:
        action.action.__parent__.execute()
        log.info("Execute action %s", action)
        transaction.commit()
    except Exception as e:
        transaction.abort()
        log.exception(e)


def _get_cache_key():
    request = get_current_request()
    from dace.objectofcollaboration.principal.util import get_current
    return str(get_oid(get_current()))#request.user))


def run():
    catalog = find_catalog('dace')
    global last_transaction
    cache_key = _get_cache_key()
    last_transaction = getattr(last_transaction_by_machine, cache_key, None)
    last_tid = catalog._p_jar.db().lastTransaction()
    if last_transaction != last_tid:
        setattr(last_transaction_by_machine, cache_key, last_tid)
        transaction.begin()
    #            query = {'object_provides': {'any_of': (IBusinessAction.__identifier__,)}}
    #            results = list(catalog.apply(query))
    #            print "actions to check:", len(results)
    #            continue
        query = catalog['object_provides'].any((IEntity.__identifier__,))
        results = list(query.execute().all())
        #log.info("objects to check: %s", len(results))

        for content in results:
            continue  # TODO remove this line
            # TODO FIXME content.actions does a write in ZODB, wtf.
            for action in content.actions:
                if getattr(action.action, '__parent__', None) is None:
                    # action.action.workitem is a StartWorkitem<commit_msg>fix optimization<commit_after>import transaction

from pyramid.testing import DummyRequest
from pyramid.threadlocal import (
        get_current_registry, get_current_request, manager)
from substanced.interfaces import IUserLocator
from substanced.principal import DefaultUserLocator
from substanced.util import get_oid

from dace.interfaces import IEntity, IBusinessAction
from dace.processinstance.event import DelayedCallback
from dace.util import find_catalog
from dace.z3 import BaseJob
from dace import log

last_transaction_by_machine = {}
CRAWLERS = []


def _call_action(action):
    transaction.begin()
    try:
        action.action.__parent__.execute()
        log.info("Execute action %s", action)
        transaction.commit()
    except Exception as e:
        transaction.abort()
        log.exception(e)


def _get_cache_key():
    request = get_current_request()
    return str(get_oid(request.user))
#    from dace.objectofcollaboration.principal.util import get_current
#    return str(get_oid(get_current()))#request.user))


def run():
    catalog = find_catalog('dace')
    global last_transaction
    cache_key = _get_cache_key()
    last_transaction = last_transaction_by_machine.setdefault(cache_key, '')
    last_tid = catalog._p_jar.db().lastTransaction()
    if last_transaction != last_tid:
        last_transaction_by_machine[cache_key] = last_tid
        transaction.begin()
    #            query = {'object_provides': {'any_of': (IBusinessAction.__identifier__,)}}
    #            results = list(catalog.apply(query))
    #            print "actions to check:", len(results)
    #            continue
        query = catalog['object_provides'].any((IEntity.__identifier__,))
        results = list(query.execute().all())
        log.info("new zodb transactions, objects to check: %s", len(results))

        for content in results:
            continue  # TODO remove this line
            for action in content.actions:
                if getattr(action.action, '__parent__', None) is None:
                    # action.action.workitem is a StartWorkitem
{'fraction_non_alphanumeric': 0.09026297085998579, 'fraction_numerical': 0.00047382136934375743, 'mean_word_length': 3.3125638406537283, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2120555', 'n_tokens_mistral': 1185, 'n_tokens_neox': 1164, 'n_words': 290}