| 1 |
# Copyright (C) 2014 Yahoo! Inc. All Rights Reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import weakref |
| 16 |
|
| 17 |
from taskflow import exceptions as excp |
| 18 |
from taskflow import states as st |
| 19 |
from taskflow.types import failure |
| 20 |
|
| 21 |
|
| 22 |
class RetryScheduler: |
| 23 |
"""Schedules retry atoms.""" |
| 24 |
|
| 25 |
def __init__(self, runtime): |
| 26 |
self._runtime = weakref.proxy(runtime) |
| 27 |
self._retry_action = runtime.retry_action |
| 28 |
self._storage = runtime.storage |
| 29 |
|
| 30 |
def schedule(self, retry): |
| 31 |
"""Schedules the given retry atom for *future* completion. |
| 32 |
|
| 33 |
Depending on the atoms stored intention this may schedule the retry |
| 34 |
atom for reversion or execution. |
| 35 |
""" |
| 36 |
intention = self._storage.get_atom_intention(retry.name) |
| 37 |
if intention == st.EXECUTE: |
| 38 |
return self._retry_action.schedule_execution(retry) |
| 39 |
elif intention == st.REVERT: |
| 40 |
return self._retry_action.schedule_reversion(retry) |
| 41 |
elif intention == st.RETRY: |
| 42 |
self._retry_action.change_state(retry, st.RETRYING) |
| 43 |
# This will force the subflow to start processing right *after* |
| 44 |
# this retry atom executes (since they will be blocked on their |
| 45 |
# predecessor getting out of the RETRYING/RUNNING state). |
| 46 |
self._runtime.retry_subflow(retry) |
| 47 |
return self._retry_action.schedule_execution(retry) |
| 48 |
else: |
| 49 |
raise excp.ExecutionFailure("Unknown how to schedule retry with" |
| 50 |
" intention: %s" % intention) |
| 51 |
|
| 52 |
|
| 53 |
class TaskScheduler: |
| 54 |
"""Schedules task atoms.""" |
| 55 |
|
| 56 |
def __init__(self, runtime): |
| 57 |
self._storage = runtime.storage |
| 58 |
self._task_action = runtime.task_action |
| 59 |
|
| 60 |
def schedule(self, task): |
| 61 |
"""Schedules the given task atom for *future* completion. |
| 62 |
|
| 63 |
Depending on the atoms stored intention this may schedule the task |
| 64 |
atom for reversion or execution. |
| 65 |
""" |
| 66 |
intention = self._storage.get_atom_intention(task.name) |
| 67 |
if intention == st.EXECUTE: |
| 68 |
return self._task_action.schedule_execution(task) |
| 69 |
elif intention == st.REVERT: |
| 70 |
return self._task_action.schedule_reversion(task) |
| 71 |
else: |
| 72 |
raise excp.ExecutionFailure("Unknown how to schedule task with" |
| 73 |
" intention: %s" % intention) |
| 74 |
|
| 75 |
|
| 76 |
class Scheduler: |
| 77 |
"""Safely schedules atoms using a runtime ``fetch_scheduler`` routine.""" |
| 78 |
|
| 79 |
def __init__(self, runtime): |
| 80 |
self._runtime = weakref.proxy(runtime) |
| 81 |
|
| 82 |
def schedule(self, atoms): |
| 83 |
"""Schedules the provided atoms for *future* completion. |
| 84 |
|
| 85 |
This method should schedule a future for each atom provided and return |
| 86 |
a set of those futures to be waited on (or used for other similar |
| 87 |
purposes). It should also return any failure objects that represented |
| 88 |
scheduling failures that may have occurred during this scheduling |
| 89 |
process. |
| 90 |
""" |
| 91 |
futures = set() |
| 92 |
for atom in atoms: |
| 93 |
scheduler = self._runtime.fetch_scheduler(atom) |
| 94 |
try: |
| 95 |
futures.add(scheduler.schedule(atom)) |
| 96 |
except Exception: |
| 97 |
# Immediately stop scheduling future work so that we can |
| 98 |
# exit execution early (rather than later) if a single atom |
| 99 |
# fails to schedule correctly. |
| 100 |
return (futures, [failure.Failure()]) |
| 101 |
return (futures, []) |