1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
21 from contextlib import contextmanager
24 from aria.utils import file
28 class BaseOperationContext(common.BaseContext):
30 Base class for contexts used during operation creation and execution.
33 def __init__(self, task_id, actor_id, **kwargs):
34 self._task_id = task_id
35 self._actor_id = actor_id
36 self._thread_local = threading.local()
37 self._destroy_session = kwargs.pop('destroy_session', False)
38 logger_level = kwargs.pop('logger_level', None)
39 super(BaseOperationContext, self).__init__(**kwargs)
40 self._register_logger(task_id=self.task.id, level=logger_level)
43 details = 'function={task.function}; ' \
44 'operation_arguments={task.arguments}'\
45 .format(task=self.task)
46 return '{name}({0})'.format(details, name=self.name)
51 The task in the model storage.
53 # SQLAlchemy prevents from accessing an object which was created on a different thread.
54 # So we retrieve the object from the storage if the current thread isn't the same as the
57 if not hasattr(self._thread_local, 'task'):
58 self._thread_local.task = self.model.task.get(self._task_id)
59 return self._thread_local.task
62 def plugin_workdir(self):
64 A work directory that is unique to the plugin and the service ID.
66 if self.task.plugin is None:
68 plugin_workdir = '{0}/plugins/{1}/{2}'.format(self._workdir,
70 self.task.plugin.name)
71 file.makedirs(plugin_workdir)
75 def serialization_dict(self):
78 'service_id': self._service_id,
79 'task_id': self._task_id,
80 'actor_id': self._actor_id,
81 'workdir': self._workdir,
82 'model_storage': self.model.serialization_dict if self.model else None,
83 'resource_storage': self.resource.serialization_dict if self.resource else None,
84 'execution_id': self._execution_id,
85 'logger_level': self.logger.level
88 'context_cls': self.__class__,
89 'context': context_dict
93 def instantiate_from_dict(cls, model_storage=None, resource_storage=None, **kwargs):
95 model_storage = aria.application_model_storage(**model_storage)
97 resource_storage = aria.application_resource_storage(**resource_storage)
99 return cls(model_storage=model_storage,
100 resource_storage=resource_storage,
101 destroy_session=True,
105 if self._destroy_session:
106 self.model.log._session.remove()
107 self.model.log._engine.dispose()
111 def persist_changes(self):
113 self.model.task.update(self.task)
116 class NodeOperationContext(BaseOperationContext):
118 Context for node operations.
124 The node of the current operation.
126 return self.model.node.get(self._actor_id)
129 def node_template(self):
131 The node template of the current operation.
133 return self.node.node_template
136 class RelationshipOperationContext(BaseOperationContext):
138 Context for relationship operations.
142 def relationship(self):
144 The relationship instance of the current operation.
146 return self.model.relationship.get(self._actor_id)
149 def source_node(self):
151 The relationship source node.
153 return self.relationship.source_node
156 def source_node_template(self):
158 The relationship source node template.
160 return self.source_node.node_template
163 def target_node(self):
165 The relationship target node.
167 return self.relationship.target_node
170 def target_node_template(self):
172 The relationship target node template.
174 return self.target_node.node_template