<commit_before>from __future__ import absolute_import

import logging

from datetime import datetime, timedelta
from functools import wraps
from threading import local, Lock, Timer
from uuid import uuid4
from contextlib import contextmanager

from changes.config import db, queue, statsreporter
from changes.constants import Result, Status
from changes.db.utils import get_or_create
from changes.models.task import Task
from changes.utils.locking import lock


BASE_RETRY_COUNTDOWN = 60
CONTINUE_COUNTDOWN = 5

# Number of seconds to delay before starting tasks.
# It isn't actually known whether this is useful or why we even have this delay;
# this just gives it a name so this fact is explicit and hopefully we can remove it more
# easily.
_DEFAULT_COUNTDOWN = 3

RUN_TIMEOUT = timedelta(minutes=60)
EXPIRE_TIMEOUT = timedelta(minutes=120)
HARD_TIMEOUT = timedelta(hours=12)

MAX_RETRIES = 10

# How many seconds we let tasks run before warning that they're slow.
_SLOW_RUN_THRESHOLD = 5 * 60


class NotFinished(Exception):
    def __init__(self, message=None, retry_after=None):
        super(NotFinished, self).__init__(message)
        self.retry_after = retry_after or CONTINUE_COUNTDOWN


class TooManyRetries(Exception):
    pass


class TrackedTask(local):
    """
    Tracks the state of the given Task and it's children.
<commit_msg>Reduce task start delay to 1s<commit_after>from __future__ import absolute_import

import logging

from datetime import datetime, timedelta
from functools import wraps
from threading import local, Lock, Timer
from uuid import uuid4
from contextlib import contextmanager

from changes.config import db, queue, statsreporter
from changes.constants import Result, Status
from changes.db.utils import get_or_create
from changes.models.task import Task
from changes.utils.locking import lock


BASE_RETRY_COUNTDOWN = 60
CONTINUE_COUNTDOWN = 5

# Number of seconds to delay before starting tasks.
# It isn't actually known whether this is useful or why we even have this delay;
# this just gives it a name so this fact is explicit and hopefully we can remove it more
# easily.
_DEFAULT_COUNTDOWN = 1

RUN_TIMEOUT = timedelta(minutes=60)
EXPIRE_TIMEOUT = timedelta(minutes=120)
HARD_TIMEOUT = timedelta(hours=12)

MAX_RETRIES = 10

# How many seconds we let tasks run before warning that they're slow.
_SLOW_RUN_THRESHOLD = 5 * 60


class NotFinished(Exception):
    def __init__(self, message=None, retry_after=None):
        super(NotFinished, self).__init__(message)
        self.retry_after = retry_after or CONTINUE_COUNTDOWN


class TooManyRetries(Exception):
    pass


class TrackedTask(local):
    """
    Tracks the state of the given Task and it's children.

{'fraction_non_alphanumeric': 0.055391432791728215, 'fraction_numerical': 0.01292466765140325, 'mean_word_length': 4.889130434782609, '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': '2091110', 'n_tokens_mistral': 827, 'n_tokens_neox': 777, 'n_words': 305}