# -*- coding: utf-8 -*-

#    Copyright (C) 2014 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 import exceptions as excp
from taskflow import retry as retry_atom
from taskflow import states as st
from taskflow import task as task_atom
from taskflow.types import failure


class Scheduler(object):
    """Schedules atoms using actions to schedule."""

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

    def _schedule_node(self, node):
        """Schedule a single node for execution."""
        # TODO(harlowja): we need to rework this so that we aren't doing type
        # checking here, type checking usually means something isn't done right
        # and usually will limit extensibility in the future.
        if isinstance(node, task_atom.BaseTask):
            return self._schedule_task(node)
        elif isinstance(node, retry_atom.Retry):
            return self._schedule_retry(node)
        else:
            raise TypeError("Unknown how to schedule atom '%s' (%s)"
                            % (node, type(node)))

    def _schedule_retry(self, retry):
        """Schedules the given retry atom for *future* completion.

        Depending on the atoms stored intention this may schedule the retry
        atom for reversion or execution.
        """
        intention = self._storage.get_atom_intention(retry.name)
        if intention == st.EXECUTE:
            return self._retry_action.execute(retry)
        elif intention == st.REVERT:
            return self._retry_action.revert(retry)
        elif intention == st.RETRY:
            self._retry_action.change_state(retry, st.RETRYING)
            self._runtime.retry_subflow(retry)
            return self._retry_action.execute(retry)
        else:
            raise excp.ExecutionFailure("Unknown how to schedule retry with"
                                        " intention: %s" % intention)

    def _schedule_task(self, task):
        """Schedules the given task atom for *future* completion.

        Depending on the atoms stored intention this may schedule the task
        atom for reversion or execution.
        """
        intention = self._storage.get_atom_intention(task.name)
        if intention == st.EXECUTE:
            return self._task_action.schedule_execution(task)
        elif intention == st.REVERT:
            return self._task_action.schedule_reversion(task)
        else:
            raise excp.ExecutionFailure("Unknown how to schedule task with"
                                        " intention: %s" % intention)

    def schedule(self, nodes):
        """Schedules the provided nodes for *future* completion.

        This method should schedule a future for each node provided and return
        a set of those futures to be waited on (or used for other similar
        purposes). It should also return any failure objects that represented
        scheduling failures that may have occurred during this scheduling
        process.
        """
        futures = set()
        for node in nodes:
            try:
                futures.add(self._schedule_node(node))
            except Exception:
                # Immediately stop scheduling future work so that we can
                # exit execution early (rather than later) if a single task
                # fails to schedule correctly.
                return (futures, [failure.Failure()])
        return (futures, [])

@@ -847,16 +847,22 @@
 %0A%0Aclass 
+_Retry
 Schedule
@@ -876,62 +876,8 @@
 t):%0A
-    %22%22%22Schedules atoms using actions to schedule.%22%22%22%0A%0A
     
@@ -941,50 +941,8 @@
 ime%0A
-        self._analyzer = runtime.analyzer%0A
     
@@ -991,40 +991,8 @@
 ion%0A
-        self._runtime = runtime%0A
     
@@ -1031,411 +1031,47 @@
 age%0A
-        self._task_action = runtime.task_action%0A%0A    def _schedule_node(self, node):%0A        %22%22%22Schedule a single node for execution.%22%22%22%0A        # TODO(harlowja): we need to rework this so that we aren't doing type%0A        # checking here, type checking usually means something isn't done right%0A        # and usually will limit extensibility in the future.%0A        if isinstance(node, task_atom.BaseTask
+%0A    @staticmethod%0A    def handles(atom
 ):%0A 
@@ -1081,58 +1081,15 @@
     
-    
 return 
-self._schedule_task(node)%0A        elif 
 isin
@@ -1095,20 +1095,20 @@
 nstance(
-node
+atom
 , retry_
@@ -1122,188 +1122,8 @@
 try)
-:%0A            return self._schedule_retry(node)%0A        else:%0A            raise TypeError(%22Unknown how to schedule atom '%25s' (%25s)%22%0A                            %25 (node, type(node)))
 %0A%0A  
@@ -1128,25 +1128,24 @@
     def 
-_
 schedule
 _retry(s
@@ -1136,22 +1136,16 @@
 schedule
-_retry
 (self, r
@@ -1957,30 +1957,271 @@
 n)%0A%0A
-    def _schedule_task
+%0Aclass _TaskScheduler(object):%0A    def __init__(self, runtime):%0A        self._storage = runtime.storage%0A        self._task_action = runtime.task_action%0A%0A    @staticmethod%0A    def handles(atom):%0A        return isinstance(atom, task_atom.BaseTask)%0A%0A    def schedule
 (sel
@@ -2847,16 +2847,533 @@
 ntion)%0A%0A
+%0Aclass Scheduler(object):%0A    %22%22%22Schedules atoms using actions to schedule.%22%22%22%0A%0A    def __init__(self, runtime):%0A        self._schedulers = %5B%0A            _RetryScheduler(runtime),%0A            _TaskScheduler(runtime),%0A        %5D%0A%0A    def _schedule_node(self, node):%0A        %22%22%22Schedule a single node for execution.%22%22%22%0A        for sched in self._schedulers:%0A            if sched.handles(node):%0A                return sched.schedule(node)%0A        else:%0A            raise TypeError(%22Unknown how to schedule '%25s'%22 %25 node)%0A%0A
     def 

Split the scheduler into sub-schedulers