1 | # Licensed to the StackStorm, Inc ('StackStorm') 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 |
||
7 | # |
||
8 | # http://www.apache.org/licenses/LICENSE-2.0 |
||
9 | # |
||
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. |
||
15 | |||
16 | from __future__ import absolute_import |
||
17 | from st2common import log as logging |
||
18 | from st2common import transport |
||
19 | from st2common.exceptions.db import StackStormDBObjectNotFoundError |
||
20 | from st2common.models.db.trigger import triggertype_access, trigger_access, triggerinstance_access |
||
21 | from st2common.persistence.base import (Access, ContentPackResource) |
||
22 | from st2common.transport import utils as transport_utils |
||
23 | |||
24 | LOG = logging.getLogger(__name__) |
||
25 | |||
26 | |||
27 | class TriggerType(ContentPackResource): |
||
28 | impl = triggertype_access |
||
29 | |||
30 | @classmethod |
||
31 | def _get_impl(cls): |
||
32 | return cls.impl |
||
33 | |||
34 | |||
35 | class Trigger(ContentPackResource): |
||
36 | impl = trigger_access |
||
37 | publisher = None |
||
38 | |||
39 | @classmethod |
||
40 | def _get_impl(cls): |
||
41 | return cls.impl |
||
42 | |||
43 | @classmethod |
||
44 | def _get_publisher(cls): |
||
45 | if not cls.publisher: |
||
46 | cls.publisher = transport.reactor.TriggerCUDPublisher( |
||
47 | urls=transport_utils.get_messaging_urls()) |
||
48 | return cls.publisher |
||
49 | |||
50 | @classmethod |
||
51 | def delete_if_unreferenced(cls, model_object, publish=True, dispatch_trigger=True): |
||
52 | # Found in the innards of mongoengine. |
||
53 | # e.g. {'pk': ObjectId('5609e91832ed356d04a93cc0')} |
||
54 | delete_query = model_object._object_key |
||
0 ignored issues
–
show
|
|||
55 | delete_query['ref_count__lte'] = 0 |
||
56 | cls._get_impl().delete_by_query(**delete_query) |
||
57 | |||
58 | # Since delete_by_query cannot tell if teh delete actually happened check with a get call |
||
59 | # if the trigger was deleted. Unfortuantely, this opens up to races on delete. |
||
60 | confirmed_delete = False |
||
61 | try: |
||
62 | cls.get_by_id(model_object.id) |
||
63 | except (StackStormDBObjectNotFoundError, ValueError): |
||
64 | confirmed_delete = True |
||
65 | |||
66 | # Publish internal event on the message bus |
||
67 | if confirmed_delete and publish: |
||
68 | try: |
||
69 | cls.publish_delete(model_object) |
||
70 | except Exception: |
||
71 | LOG.exception('Publish failed.') |
||
72 | |||
73 | # Dispatch trigger |
||
74 | if confirmed_delete and dispatch_trigger: |
||
75 | try: |
||
76 | cls.dispatch_delete_trigger(model_object) |
||
77 | except Exception: |
||
78 | LOG.exception('Trigger dispatch failed.') |
||
79 | |||
80 | return model_object |
||
81 | |||
82 | |||
83 | class TriggerInstance(Access): |
||
84 | impl = triggerinstance_access |
||
85 | |||
86 | @classmethod |
||
87 | def _get_impl(cls): |
||
88 | return cls.impl |
||
89 | |||
90 | @classmethod |
||
91 | def delete_by_query(cls, *args, **query): |
||
92 | return cls._get_impl().delete_by_query(*args, **query) |
||
93 |
Prefixing a member variable
_
is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class: