<commit_before>from __future__ import absolute_import

from flask_restful import reqparse

from ds.config import db, redis
from ds.api.base import ApiView
from ds.models import App, Task, TaskStatus
from ds.utils.redis import lock


class TaskIndexApiView(ApiView):
    post_parser = reqparse.RequestParser()
    post_parser.add_argument('app')
    post_parser.add_argument('env', default='production')
    post_parser.add_argument('ref')

    def _has_active_task(self, app, env):
        return db.session.query(
            Task.query.filter(
                Task.status.in_([TaskStatus.pending, TaskStatus.in_progress]),
                Task.app_id == app.id,
                Task.environment == env,
            ).exists(),
        ).scalar()

    def post(self):
        """
        Given any constraints for a task are within acceptable bounds, create
        a new task and enqueue it.
        """
        args = self.post_parser.parse_args()

        app = App.query.filter(App.name == args.app).first()
        if not app:
            return self.error('Invalid app')

        with lock(redis, 'task:create:{}'.format(app.id), timeout=5):
            if self._has_active_task(app, args.env):
                return self.error(
                    message='Another task is already in progress for this app',
                    name='locked',
                )

            task = Task(
                app_id=app.id,
                environment=args.env,
                # TODO(dcramer): ref should default based on app config
                ref=args.ref,
            )
            db.session.add(task)
            db.session.flush()

        return self.respond({})
<commit_msg>Commit and fire task<commit_after>from __future__ import absolute_import

from flask_restful import reqparse

from ds.config import db, redis
from ds.api.base import ApiView
from ds.models import App, Task, TaskStatus
from ds.tasks import execute_task
from ds.utils.redis import lock


class TaskIndexApiView(ApiView):
    post_parser = reqparse.RequestParser()
    post_parser.add_argument('app')
    post_parser.add_argument('env', default='production')
    post_parser.add_argument('ref')

    def _has_active_task(self, app, env):
        return db.session.query(
            Task.query.filter(
                Task.status.in_([TaskStatus.pending, TaskStatus.in_progress]),
                Task.app_id == app.id,
                Task.environment == env,
            ).exists(),
        ).scalar()

    def post(self):
        """
        Given any constraints for a task are within acceptable bounds, create
        a new task and enqueue it.
        """
        args = self.post_parser.parse_args()

        app = App.query.filter(App.name == args.app).first()
        if not app:
            return self.error('Invalid app')

        with lock(redis, 'task:create:{}'.format(app.id), timeout=5):
            if self._has_active_task(app, args.env):
                return self.error(
                    message='Another task is already in progress for this app',
                    name='locked',
                )

            task = Task(
                app_id=app.id,
                environment=args.env,
                # TODO(dcramer): ref should default based on app config
                ref=args.ref,
                status=TaskStatus.pending,
            )
            db.session.add(task)
            db.session.commit()

            execute_task.delay(task_id=task.id)

        return self.respond({})

{'fraction_non_alphanumeric': 0.1010244735344337, 'fraction_numerical': 0.0005691519635742744, 'mean_word_length': 1.9463537300922045, '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': '6095823', 'n_tokens_mistral': 986, 'n_tokens_neox': 944, 'n_words': 241}