Beispiel #1
0
 def refuse(self, caused_by):
     """
     :param caused_by: upon which node, the task flow is refused
     """
     self.status = constants.WORK_FLOW_REFUSED
     do_commit(self)
     self._refuse_tree(self.root_node, caused_by)
Beispiel #2
0
    def refuse(self):
        """
        refuse the node, and the whole work flow

        :raise yawf.exceptions.WorkFlowRefused: if work flow already refused
        """
        if self.work_flow.status == yawf.constants.WORK_FLOW_REFUSED:
            raise yawf.exceptions.WorkFlowRefused()
        self.approved = False
        self.handle_time = datetime.now()
        do_commit(self)
        self.work_flow.refuse(self)
Beispiel #3
0
    def approve(self):
        """
        approve the node, and retry the whole work flow to test if it is done

        :raise yawf.exceptions.NodeAlreadyApproved: if node already approved
        """
        if self.approved:
            raise yawf.exceptions.NodeAlreadyApproved()
        self.approved = True
        self.handle_time = datetime.now()
        do_commit(self)
        self.on_approved()
        self.work_flow.retry(self)
Beispiel #4
0
    def retry(self, last_operated_node):
        """
        test if all the nodes are approved, if they are, execute the nodes from
        LEAF to ROOT

        :raise: WorkFlowRefused when the node flow has been refused
        :raise: WorkFlowDelayed when there exists node that hasn't been
        approved
        """
        if self.status == constants.WORK_FLOW_REFUSED:
            raise WorkFlowRefused()
        # then we test if all the (indirect) depenecies of ROOT are met
        unmet_node = self._find_next_unmet_node(self.root_node)
        if unmet_node:
            last_operated_node.on_delayed(unmet_node)
            self.current_node = unmet_node
            do_commit(self)
            raise WorkFlowDelayed(unmet_node,
                                  "node %s is not met" % unicode(unmet_node))
        self.status = constants.WORK_FLOW_APPROVED
        do_commit(self)

        # execute all the nodes
        try:
            self.root_node.execute()
            self.failed = False
            self.status = constants.WORK_FLOW_EXECUTED
            do_commit(self)
        except:
            self.failed = True
            do_commit(self)
            raise
Beispiel #5
0
 def dependencies(self):
     ret = []
     from yawf.models import WorkFlow
     for policy, node_kwargs in self.policy.dependencies:
         try:
             node = self.query.filter(
                 yawf.WorkFlowEngine.instance.node_model.work_flow_id ==
                 self.work_flow_id).filter(
                     self.__class__.policy_name == policy).one()
         except NoResultFound:
             node_kwargs.update(policy_name=policy)
             node = self.__class__(work_flow=self.work_flow, **node_kwargs)
             do_commit(node)
         ret.append(node)
     return ret
Beispiel #6
0
def node_view():
    class _Form(Form):
        destination = TextField('destination', validators=[DataRequired()])
        contact = TextField('contact', validators=[DataRequired()])

    form = _Form()

    if form.validate_on_submit():
        work_flow = yawf.new_work_flow(
            'travel application', lambda work_flow: models.Node(
                name='Submit Travel Application',
                policy_name="Travel",
                work_flow=work_flow,
                handler_group=models.Group.query.filter(models.Group.name ==
                                                        'Customers').one()),
            lambda work_flow: do_commit(
                models.Application(username=current_user.username,
                                   destination=form.destination.data,
                                   contact=form.contact.data,
                                   work_flow=work_flow)).id)
        try:
            work_flow.start()
        except yawf.exceptions.WorkFlowDelayed:
            flash('You have just submitted an travel application')
            return redirect(url_for('node_list_view'))
    return render_template('/request.html', form=form)
Beispiel #7
0
    def execute(self):
        """
        execute the policy, all the dependent policy will be executed at first
        """
        for node in self.node.dependencies:
            node.execute()

        if not self.work_flow.status == constants.WORK_FLOW_EXECUTED or self.node.failed:
            try:
                self()
                self.node.failed = False
                do_commit(self.node)
                self.after_executed()
            except:
                self.node.failed = True
                do_commit(self.node)
                raise
Beispiel #8
0
def new_work_flow(tag,
                  node_creator,
                  tag_creator=None,
                  annotation=None,
                  token=None):
    """
    create a work flow

    :param tag: tag of the work flow
    :param node_creator: a function to create root node, its argument is
    "work_flow"
    :param tag_creator: a function to create tag, its argument is "work_flow"
    :param annotation: annotation of the work flow
    :param token: token of the work flow
    """
    work_flow = do_commit(
        yawf.models.WorkFlow(tag=tag, annotation=annotation, token=token))
    work_flow.root_node = do_commit(node_creator(work_flow))
    work_flow.root_node.tag = tag_creator and tag_creator(work_flow)
    return do_commit(work_flow)
Beispiel #9
0
    def execute(self):
        """
        execute the work flow
        you must guarantee each work is a transaction

        :raise WorkFlowRefused: if task flow is refused
        :raise WorkFlowProcessing: if task flow is processing
        :raise Exception: any exceptions raised when executing tasks
        """
        if self.status == constants.WORK_FLOW_REFUSED:
            raise WorkFlowRefused()
        elif self.status == constants.WORK_FLOW_PROCESSING:
            raise WorkFlowProcessing()
        try:
            self.root_node.execute()
            self.failed = False
            self.status == constants.WORK_FLOW_EXECUTED
            do_commit(self)
        except:
            self.failed = True
            do_commit(self)
            raise
Beispiel #10
0
 def bind(self, token):
     self.token = token
     do_commit(self)
Beispiel #11
0
#! /usr/bin/env python
import types

from basemain import app
from database import sa_db, init_db
from models import Group, User
from yawf.utils import do_commit

sa_db.drop_all()
init_db()

customers = do_commit(Group(name='Customers'))
clerks = do_commit(Group(name='Clerks'))
do_commit(User(username='******', group=customers))
do_commit(User(username='******', group=clerks))