Files
deb-python-taskflow/taskflow/flow.py
Joshua Harlow 23dfff4105 Engine, task, linear_flow unification
In order to move away from the existing flows having their
own implementation of running, start moving the existing
flows to be  patterns that only structure tasks (and impose
constraints about how the group of tasks can run) in useful
ways.

Let the concept of running those patterns be handled by an
engine instead of being handled by the flow itself. This
will allow for varying engines to be able to run flows in
whichever way the engine chooses (as long as the constraints
set up by the flow are observed).

Currently threaded flow and graph flow are broken by this
commit, since they have not been converted to being a
structure of tasks + constraints. The existing engine has
not yet been modified to run those structures either, work
is underway  to remediate this.

Part of: blueprint patterns-and-engines

Followup bugs that must be addressed:
  Bug: 1221448
  Bug: 1221505

Change-Id: I3a8b96179f336d1defe269728ebae0caa3d832d7
2013-09-05 19:26:36 -07:00

84 lines
2.4 KiB
Python

# -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# 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.
import abc
from taskflow.openstack.common import uuidutils
from taskflow import task
from taskflow import utils
def _class_name(obj):
return ".".join([obj.__class__.__module__, obj.__class__.__name__])
class Flow(object):
"""The base abstract class of all flow implementations.
It provides a name and an identifier (uuid or other) to the flow so that
it can be uniquely identifed among many flows.
Flows are expected to provide the following methods:
- add
- __len__
"""
__metaclass__ = abc.ABCMeta
def __init__(self, name, uuid=None):
self._name = str(name)
if uuid:
self._id = str(uuid)
else:
self._id = uuidutils.generate_uuid()
@property
def name(self):
"""A non-unique name for this flow (human readable)"""
return self._name
@property
def uuid(self):
return self._id
@abc.abstractmethod
def __len__(self):
"""Returns how many items are in this flow."""
raise NotImplementedError()
def __str__(self):
lines = ["%s: %s" % (_class_name(self), self.name)]
lines.append("%s" % (self.uuid))
lines.append("%s" % (len(self)))
return "; ".join(lines)
def _extract_item(self, item):
if isinstance(item, (task.BaseTask, Flow)):
return item
if issubclass(item, task.BaseTask):
return item()
task_factory = getattr(item, utils.TASK_FACTORY_ATTRIBUTE, None)
if task_factory:
return self._extract_item(task_factory(item))
raise TypeError("Invalid item %r: it's not task and not flow" % item)
@abc.abstractmethod
def add(self, *items):
"""Adds a given item/items to this flow."""
raise NotImplementedError()