开发者

Django workflow engine using signals and Celery-django

I'm working to implement a workflow engine into my project and the main purpose of my attempt is to create a portable app. Something, I would be able to place into any other project in the future and then attach workfl开发者_运维技巧ows to different models in my projects and get it to work.

I tried to think of an approach but it doesn't seem to be the perfect setup. I thought of creating an workflow app inside my project, attach two kind of models, some will contain the setup of a the workflow (workflow, steps, actions) and other models will contain the instances/transactions.

Below is my workflow/models.py

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
import signals


################################
#   Workflow engine models
################################

class Signal_Listener(models.Model):
    LISTENING_ON = (
    ('I', 'INSERT'),
    ('U', 'UPDATE'),
    ('D', 'DELETE'),
    ('S', 'SELECT'),
    ('A', 'ANY QUERY'),
    ('L', 'ANY DDL'),
    )

    table_name = models.CharField(max_length=100)
    listening_to = models.CharField(max_length=1, choices=LISTENING_ON)

    class Meta:
        unique_together = (("table_name", "listening_to"),)

    def __unicode__(self):
        return '%s - %s' % (self.table_name, self.listening_to)

class Action(models.Model):
    ACTION_TYPE_CHOICES = (
    ('P', 'Python Script'   ),
    ('C', 'Class name'      ),
    )
    name = models.CharField(max_length=100)
    action_type = models.CharField(max_length=1, choices=ACTION_TYPE_CHOICES)
    audit_obj = generic.GenericRelation('core.Audit', editable=False)

class Steps(models.Model):
    sequence = models.IntegerField() 
    Action = models.ForeignKey(Action)
    Signal_Listener = models.ForeignKey(Signal_Listener)

class Process(models.Model):
## TODO: Document
# Processes class is used to store information about the process itself.
# Or in another word, the workflow name.
    WF_TYPE_LIST = (
    ('P', 'Python-API'),
    )

    name = models.CharField(max_length=30)
    is_active = models.BooleanField()
    wf_type = models.CharField(max_length=1, choices=WF_TYPE_LIST)
    audit_obj = generic.GenericRelation('core.Audit', editable=False)
    listening_to = models.ForeignKey(Steps)



################################
#   Workflow transactions models
################################

class Instance(models.Model):
##  TODO: Document
# Re
    INSTANCE_STATUS = (
    ('I', 'In Progress' ),
    ('C', 'Cancelled'   ),
    ('A', 'Archived'     ), # Old completed tasks can be archived
    ('P', 'Pending'     ),
    ('O', 'Completed'   )
    )

    id = models.CharField(max_length=200, primary_key=True)
    status = models.CharField(max_length=1, choices=INSTANCE_STATUS, db_index=True)
    audit_obj = generic.GenericRelation('core.Audit', editable=False)

    def save(self, *args, **kwargs):
    # on new records generate a new uuid
        if self.id is None or self.id.__len__() is 0:
            import uuid

            self.id = uuid.uuid4().__str__()
        super(Instances, self).save(*args, **kwargs)

class Task(models.Model):
    TASK_STATUS = (
    ('S', 'Assigned'    ),
    ('I', 'In Progress' ),
    ('P', 'Pending'     ),
    ('C', 'Cancelled'   ),
    ('A', 'Archived'    ), # Old completed tasks can be archived
    ('O', 'Completed'   )
    )
    name = models.CharField(max_length=100)
    instance = models.ForeignKey(Instance)
    status = models.CharField(max_length=1, choices=TASK_STATUS)
    bio = models.CharField(max_length=100)
    audit_obj = generic.GenericRelation('core.Audit', editable=False)

and I also have a workflow/signals.py

    """
        Workflow Signals
            Will be used to monitor all inserts, update, delete or select statements
            If an action is attached to that particular table, it will be inserted Celery-Tasks distribution.
    """
    from django.db.models.signals import post_save, post_delete
    from django.core.cache import cache


    def workflow_post_init_listener(sender, **kwargs):
        try:

            if cache.get('wf_listner_cache_%s' % kwargs['instance']._meta.db_table):
                pass
            else:
                record = 'Signal_Listener'.objects.get(table_name__exact=kwargs['instance']._meta.db_table)
# am not sure what to do next!
        except 'Signal_Listener'.DoesNotExist:
            # TODO: Error logging
            pass

    post_save.connect(workflow_post_init_listener, dispatch_uid="workflow.models.listener.save")

I think my model design might need to be enhanced as well. I could use in several scenarios and I was thinking to start with the approval cycle, for example, I could insert the table_name/model_name in signal_listener to monitor for new inserts. If so, I will trigger a particular workflow.

As for actions, I do understand that actions will require to be developed. Maybe i will need to create an action folder under workflow app, put each action in a class. Each will do a particular task of sending emails, archiving, update database value..etc

If am reinventing the wheel and if there is such thing that has been developed already, any one can suggest, I would be more than happy to go through it.

Best regards,


For example you can have a look for zope.wfmc (http://pypi.python.org/pypi/zope.wfmc) : this is an implementation of a Workflow-Management Coalition Workflow that can be defined on XPDL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜