<commit_before># -*- coding: utf-8 -*-

#    Copyright (C) 2012 Yahoo! Inc. All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from taskflow.engines.action_engine import executor as ex
from taskflow import retry as r
from taskflow import states as st
from taskflow import task
from taskflow.utils import misc


_WAITING_TIMEOUT = 60  # in seconds


class FutureGraphAction(object):
    """Graph action build around futures returned by task action.

    This graph action schedules all task it can for execution and than
    waits on returned futures. If task executor is able to execute tasks
    in parallel, this enables parallel flow run and reversion.
    """

    def __init__(self, analyzer, storage, task_action, retry_action):
        self._analyzer = analyzer
        self._storage = storage
        self._task_action = task_action
        self._retry_action = retry_action

    def is_running(self):
        return self._storage.get_flow_state() == st.RUNNING

    def execute(self):

        def schedule(nodes, not_done):
            for node in nodes:
                # Returns schedule function for current atom and
                # executes scheduling
                if isinstance(node, task.BaseTask):
                    future = self._schedule_task(node)
                elif isinstance(node, r.Retry):
                    future = self._schedule_retry(node)
                else:
                    raise TypeError("Unknown how to schedule node %s" % node)
                if future is not None:
                    not_done.append(future)
                else:
                    schedule(self._analyzer.get_next_nodes(node), not_done)

        not_done = []
        # Prepare flow to be resumed
        next_nodes = self._prepare_flow_for_resume()
        next_nodes.update(self._analyzer.get_next_nodes())
        schedule(next_nodes, not_done)

        failures = []
        while not_done:
            # NOTE(imelnikov): if timeout occurs before any of futures
            # completes, done list will be empty and we'll just go
            # for next iteration.
            done, not_done = self._task_action.wait_for_any(
                not_done, _WAITING_TIMEOUT)

            next_nodes = set()
            for future in done:
                node, event, result = future.result()
                if isinstance(node, task.BaseTask):
                    self._complete_task(node, event, result)
                if isinstance(result, misc.Failure):
                    if event == ex.EXECUTED:
                        self._process_atom_failure(node, result)
                    else:
                        failures.append(result)
                next_nodes.update(self._analyzer.get_next_nodes(node))

            if next_nodes and not failures and self.is_running():
                schedule(next_nodes, not_done)

        if failures:
            misc.Failure.reraise_if_any(failures)

        if self._analyzer.get_next_nodes():
            return st.SUSPENDED
        elif self._analyzer.is_success():
            return st.SUCCESS<commit_msg>Make schedule a proper method of GraphAction<commit_after># -*- coding: utf-8 -*-

#    Copyright (C) 2012 Yahoo! Inc. All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from taskflow.engines.action_engine import executor as ex
from taskflow import retry as r
from taskflow import states as st
from taskflow import task
from taskflow.utils import misc


_WAITING_TIMEOUT = 60  # in seconds


class FutureGraphAction(object):
    """Graph action build around futures returned by task action.

    This graph action schedules all task it can for execution and than
    waits on returned futures. If task executor is able to execute tasks
    in parallel, this enables parallel flow run and reversion.
    """

    def __init__(self, analyzer, storage, task_action, retry_action):
        self._analyzer = analyzer
        self._storage = storage
        self._task_action = task_action
        self._retry_action = retry_action

    def is_running(self):
        return self._storage.get_flow_state() == st.RUNNING

    def _schedule(self, nodes):
        """Schedule nodes for execution.

        Returns list of futures.
        """
        futures = []
        for node in nodes:
            if isinstance(node, task.BaseTask):
                future = self._schedule_task(node)
            elif isinstance(node, r.Retry):
                future = self._schedule_retry(node)
            else:
                raise TypeError("Unknown how to schedule node %s" % node)
            if future is not None:
                futures.append(future)
            else:
                next_nodes = self._analyzer.get_next_nodes(node)
                futures.extend(self._schedule(next_nodes))
        return futures

    def execute(self):
        # Prepare flow to be resumed
        next_nodes = self._prepare_flow_for_resume()
        next_nodes.update(self._analyzer.get_next_nodes())
        not_done = self._schedule(next_nodes)

        failures = []
        while not_done:
            # NOTE(imelnikov): if timeout occurs before any of futures
            # completes, done list will be empty and we'll just go
            # for next iteration.
            done, not_done = self._task_action.wait_for_any(
                not_done, _WAITING_TIMEOUT)

            next_nodes = set()
            for future in done:
                node, event, result = future.result()
                if isinstance(node, task.BaseTask):
                    self._complete_task(node, event, result)
                if isinstance(result, misc.Failure):
                    if event == ex.EXECUTED:
                        self._process_atom_failure(node, result)
                    else:
                        failures.append(result)
                next_nodes.update(self._analyzer.get_next_nodes(node))

            if next_nodes and not failures and self.is_running():
                not_done.extend(self._schedule(next_nodes))

        if failures:
            misc.Failure.reraise_if_any(failures)

        if self._analyzer.get_next_nodes():
            return st.SUSPENDED
        elif self._analyzer.is_success():
            return st.SUCCESS
{'fraction_non_alphanumeric': 0.07035313441007392, 'fraction_numerical': 0.003011223651793047, 'mean_word_length': 2.1146632566069905, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '926275', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1845, 'n_words': 636}