Skip to content

Commit

Permalink
Lock on start flow. allow to connect job nodes directly to start. Close
Browse files Browse the repository at this point in the history
  • Loading branch information
kmmbvnr committed Jul 24, 2016
1 parent 1ed34e9 commit e27f2e2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
5 changes: 4 additions & 1 deletion tests/test_activation.py
@@ -1,6 +1,6 @@
from django.test import TestCase

from viewflow import activation, flow
from viewflow import activation, flow, lock
from viewflow.activation import STATUS
from viewflow.compat import mock

Expand All @@ -16,6 +16,7 @@ def active_tasks(self):
return []

def save(self):
self.pk = 1
return

ProcessStub._default_manager.get.return_value = ProcessStub()
Expand All @@ -34,6 +35,7 @@ def leading(self):
return Task.objects.none()

def save(self):
self.pk = 1
return

class UserStub(object):
Expand All @@ -42,6 +44,7 @@ class UserStub(object):
def init_node(self, node):
class FlowStub(object):
process_cls = Test.ProcessStub
lock_impl = staticmethod(lock.no_lock)
task_cls = Test.TaskStub
instance = None

Expand Down
2 changes: 2 additions & 0 deletions tests/test_flow_func.py
Expand Up @@ -178,6 +178,7 @@ def active_tasks(self):
return []

def save(self):
self.pk = 1
return


Expand All @@ -197,6 +198,7 @@ def leading(self):
return Task.objects.none()

def save(self):
self.pk = 1
return


Expand Down
5 changes: 5 additions & 0 deletions viewflow/activation.py
Expand Up @@ -212,6 +212,7 @@ class StartActivation(Activation):

@Activation.status.super()
def initialize(self, flow_task, task):
self.lock = None
self.flow_task, self.flow_cls = flow_task, flow_task.flow_cls

if task:
Expand Down Expand Up @@ -249,6 +250,10 @@ def done(self):

self.process.save()

lock_impl = self.flow_cls.lock_impl(self.flow_cls.instance)
self.lock = lock_impl(self.flow_cls, self.process.pk)
self.lock.__enter__()

self.task.process = self.process
self.task.finished = now()
self.task.save()
Expand Down
26 changes: 19 additions & 7 deletions viewflow/decorators.py
@@ -1,3 +1,4 @@
import sys
import traceback
import functools

Expand All @@ -10,9 +11,13 @@
def flow_start_func(func):
@functools.wraps(func)
def _wrapper(flow_task, *args, **kwargs):
activation = flow_task.activation_cls()
activation.initialize(flow_task, None)
return func(activation, *args, **kwargs)
try:
activation = flow_task.activation_cls()
activation.initialize(flow_task, None)
return func(activation, *args, **kwargs)
finally:
if activation.lock:
activation.lock.__exit__(*sys.exc_info())
return _wrapper


Expand Down Expand Up @@ -102,9 +107,13 @@ def _wrapper(*args, **kwargs):
def flow_start_signal(handler):
@functools.wraps(handler)
def _wrapper(sender, flow_task=None, **signal_kwargs):
activation = flow_task.activation_cls()
activation.initialize(flow_task, None)
return handler(sender=sender, activation=activation, **signal_kwargs)
try:
activation = flow_task.activation_cls()
activation.initialize(flow_task, None)
return handler(sender=sender, activation=activation, **signal_kwargs)
finally:
if activation.lock:
activation.lock.__exit__(*sys.exc_info())
return _wrapper


Expand Down Expand Up @@ -136,14 +145,17 @@ def flow_start_view(view):

@functools.wraps(view)
def _wrapper(request, flow_cls, flow_task, **kwargs):
try:
activation = flow_task.activation_cls()
activation.initialize(flow_task, None)

request.activation = activation
request.process = activation.process
request.task = activation.task

return view(request, **kwargs)
finally:
if activation.lock:
activation.lock.__exit__(*sys.exc_info())
return _wrapper


Expand Down

0 comments on commit e27f2e2

Please sign in to comment.