Commit e5a442ad by Dudás Ádám

common, vm: extract activity code composition functionality

parent 299383c0
...@@ -36,6 +36,15 @@ def activitycontextimpl(act, on_abort=None, on_commit=None): ...@@ -36,6 +36,15 @@ def activitycontextimpl(act, on_abort=None, on_commit=None):
activity_context = contextmanager(activitycontextimpl) activity_context = contextmanager(activitycontextimpl)
activity_code_separator = '.'
def join_activity_code(*args):
"""Join the specified parts into an activity code.
"""
return activity_code_separator.join(args)
class ActivityModel(TimeStampedModel): class ActivityModel(TimeStampedModel):
activity_code = CharField(max_length=100, verbose_name=_('activity code')) activity_code = CharField(max_length=100, verbose_name=_('activity code'))
parent = ForeignKey('self', blank=True, null=True, related_name='children') parent = ForeignKey('self', blank=True, null=True, related_name='children')
......
...@@ -6,7 +6,11 @@ from django.db.models import CharField, ForeignKey ...@@ -6,7 +6,11 @@ from django.db.models import CharField, ForeignKey
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from common.models import ActivityModel, activitycontextimpl from common.models import (
ActivityModel, activitycontextimpl, join_activity_code,
)
logger = getLogger(__name__) logger = getLogger(__name__)
...@@ -23,6 +27,7 @@ class ActivityInProgressError(Exception): ...@@ -23,6 +27,7 @@ class ActivityInProgressError(Exception):
class InstanceActivity(ActivityModel): class InstanceActivity(ActivityModel):
ACTIVITY_CODE_BASE = join_activity_code('vm', 'Instance')
instance = ForeignKey('Instance', related_name='activity_log', instance = ForeignKey('Instance', related_name='activity_log',
help_text=_('Instance this activity works on.'), help_text=_('Instance this activity works on.'),
verbose_name=_('instance')) verbose_name=_('instance'))
...@@ -53,9 +58,10 @@ class InstanceActivity(ActivityModel): ...@@ -53,9 +58,10 @@ class InstanceActivity(ActivityModel):
if concurrency_check and active_activities.exists(): if concurrency_check and active_activities.exists():
raise ActivityInProgressError(active_activities[0]) raise ActivityInProgressError(active_activities[0])
act = cls(activity_code='vm.Instance.' + code_suffix, activity_code = join_activity_code(cls.ACTIVITY_CODE_BASE, code_suffix)
instance=instance, parent=None, resultant_state=None, act = cls(activity_code=activity_code, instance=instance, parent=None,
started=timezone.now(), task_uuid=task_uuid, user=user) resultant_state=None, started=timezone.now(),
task_uuid=task_uuid, user=user)
act.save() act.save()
return act return act
...@@ -66,7 +72,7 @@ class InstanceActivity(ActivityModel): ...@@ -66,7 +72,7 @@ class InstanceActivity(ActivityModel):
raise ActivityInProgressError(active_children[0]) raise ActivityInProgressError(active_children[0])
act = InstanceActivity( act = InstanceActivity(
activity_code=self.activity_code + '.' + code_suffix, activity_code=join_activity_code(self.activity_code, code_suffix),
instance=self.instance, parent=self, resultant_state=None, instance=self.instance, parent=self, resultant_state=None,
started=timezone.now(), task_uuid=task_uuid, user=self.user) started=timezone.now(), task_uuid=task_uuid, user=self.user)
act.save() act.save()
...@@ -97,6 +103,7 @@ def instance_activity(code_suffix, instance, on_abort=None, on_commit=None, ...@@ -97,6 +103,7 @@ def instance_activity(code_suffix, instance, on_abort=None, on_commit=None,
class NodeActivity(ActivityModel): class NodeActivity(ActivityModel):
ACTIVITY_CODE_BASE = join_activity_code('vm', 'Node')
node = ForeignKey('Node', related_name='activity_log', node = ForeignKey('Node', related_name='activity_log',
help_text=_('Node this activity works on.'), help_text=_('Node this activity works on.'),
verbose_name=_('node')) verbose_name=_('node'))
...@@ -119,15 +126,15 @@ class NodeActivity(ActivityModel): ...@@ -119,15 +126,15 @@ class NodeActivity(ActivityModel):
@classmethod @classmethod
def create(cls, code_suffix, node, task_uuid=None, user=None): def create(cls, code_suffix, node, task_uuid=None, user=None):
act = cls(activity_code='vm.Node.' + code_suffix, activity_code = join_activity_code(cls.ACTIVITY_CODE_BASE, code_suffix)
node=node, parent=None, started=timezone.now(), act = cls(activity_code=activity_code, node=node, parent=None,
task_uuid=task_uuid, user=user) started=timezone.now(), task_uuid=task_uuid, user=user)
act.save() act.save()
return act return act
def create_sub(self, code_suffix, task_uuid=None): def create_sub(self, code_suffix, task_uuid=None):
act = NodeActivity( act = NodeActivity(
activity_code=self.activity_code + '.' + code_suffix, activity_code=join_activity_code(self.activity_code, code_suffix),
node=self.node, parent=self, started=timezone.now(), node=self.node, parent=self, started=timezone.now(),
task_uuid=task_uuid, user=self.user) task_uuid=task_uuid, user=self.user)
act.save() act.save()
......
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