Commit c86086a5 by Dudás Ádám

vm: check instance for concurrent activities

parent 9c3f8b1d
......@@ -27,7 +27,7 @@ def activitycontextimpl(act, on_abort=None, on_commit=None):
class ActivityModel(TimeStampedModel):
activity_code = CharField(max_length=100, verbose_name=_('activity code'))
parent = ForeignKey('self', blank=True, null=True)
parent = ForeignKey('self', blank=True, null=True, related_name='children')
task_uuid = CharField(blank=True, max_length=50, null=True, unique=True,
help_text=_('Celery task unique identifier.'),
verbose_name=_('task_uuid'))
......
......@@ -10,6 +10,18 @@ from common.models import ActivityModel, activitycontextimpl
logger = getLogger(__name__)
class ActivityInProgressError(Exception):
def __init__(self, activity, message=None):
if message is None:
message = ("Another activity is currently in progress: '%s'."
% activity.activity_code)
Exception.__init__(self, message)
self.activity = activity
class InstanceActivity(ActivityModel):
instance = ForeignKey('Instance', related_name='activity_log',
help_text=_('Instance this activity works on.'),
......@@ -18,7 +30,7 @@ class InstanceActivity(ActivityModel):
class Meta:
app_label = 'vm'
db_table = 'vm_instanceactivity'
ordering = ['-started', 'instance', '-id']
ordering = ['-finished', '-started', 'instance', '-id']
def __unicode__(self):
if self.parent:
......@@ -50,12 +62,24 @@ class InstanceActivity(ActivityModel):
@contextmanager
def sub_activity(self, code_suffix, task_uuid=None):
# Check for concurrent activities
active_children = self.children.filter(finished__isnull=True)
if active_children.exists():
raise ActivityInProgressError(active_children[0])
act = self.create_sub(code_suffix, task_uuid)
return activitycontextimpl(act)
@contextmanager
def instance_activity(code_suffix, instance, task_uuid=None, user=None):
# Check for concurrent activities
active_activities = instance.activity_log.filter(finished__isnull=True)
if active_activities.exists():
raise ActivityInProgressError(active_activities[0])
act = InstanceActivity.create(code_suffix, instance, task_uuid, user)
return activitycontextimpl(act)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment