-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIngestionManagerContext.cls
More file actions
193 lines (170 loc) · 4.93 KB
/
IngestionManagerContext.cls
File metadata and controls
193 lines (170 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
Class DataPipe.Ingestion.BP.IngestionManagerContext Extends Ens.BP.Context
{
/// !IMPORTANT. BP must fill this property
/// Model object
Property Model As DataPipe.Model;
/// !IMPORTANT. BP must fill this property
/// Attributes to get/create a related Inbox record
Property InboxAttributes As DataPipe.Data.InboxAttributes;
/// Current Ingestion record
Property Ingestion As DataPipe.Data.Ingestion;
/// Related Inbox record
Property Inbox As DataPipe.Data.Inbox;
/// Staging request that will be sent to Staging layer
Property StagingReq As DataPipe.Msg.StagingReq;
/// Error text (in case of exception)
Property ErrorText As %String(MAXLEN = "");
/// IngestionManager
/// This method must be called from BP after filling Ingestion and Model properties
Method IngestionManager() As %Status
{
set ret = $$$OK
try {
$$$ThrowOnError(..InitInbox())
// avoid ingesting ignored inboxes
if '..Inbox.Ignored {
$$$ThrowOnError(..InitIngestion())
$$$ThrowOnError(..SerializeModel())
$$$ThrowOnError(..SaveData())
$$$ThrowOnError(..BuildStagingReq())
} else {
$$$LOGWARNING("Ignoring Inbox "_..Inbox.%Id()_" because has been set as ignored before")
}
} catch ex {
set ret = ex.AsStatus()
}
quit ret
}
/// Init Inbox. Create or Open an Inbox record
Method InitInbox() As %Status
{
set ret = $$$OK
try {
$$$ThrowOnError(##class(DataPipe.Data.Inbox).GetByKeyAttributes(..InboxAttributes, .inboxObj))
if '$isobject(inboxObj) {
// return new inbox
set inboxObj = ##class(DataPipe.Data.Inbox).%New()
set inboxObj.CreatedTS = $zdatetime($horolog,3)
set inboxObj.UpdatedTS = inboxObj.CreatedTS
} else {
// return existing inbox
set inboxObj.UpdatedTS = $zdatetime($horolog,3)
}
$$$ThrowOnError(inboxObj.PopulateAttributes(..InboxAttributes))
set ..Inbox = inboxObj
} catch ex {
set ret = ex.AsStatus()
}
quit ret
}
/// Init Ingestion. Create a new Ingestion record
Method InitIngestion() As %Status
{
set ret = $$$OK
try {
set ..Ingestion = ##class(DataPipe.Data.Ingestion).%New()
set ..Ingestion.SessionId = ..%Process.%SessionId
set ..Ingestion.HeaderId = ..%Process.%PrimaryRequestHeader.%Id()
set ..Ingestion.CreatedTS = $zdatetime($horolog, 3)
do ..Inbox.Ingestions.Insert(..Ingestion)
set ..Inbox.LastIngestion = ..Ingestion
set ..Inbox.LastStaging = ""
set ..Inbox.LastOper = ""
set ..Inbox.Status = "INGESTING"
set ..Inbox.UpdatedTS = ..Ingestion.CreatedTS
// check model object
if '$isobject(..Model) {
$$$ThrowStatus($$$ERROR($$$GeneralError, "Model object is required"))
}
if '..Model.%Extends("DataPipe.Model") {
$$$ThrowStatus($$$ERROR($$$GeneralError, "Model object must extend DataPipe.Model"))
}
} catch ex {
set ret = ex.AsStatus()
}
quit ret
}
/// Serialize model into Ingestion record
Method SerializeModel() As %Status
{
set ret = $$$OK
try {
$$$ThrowOnError(..Model.Serialize(.stream))
set ..Ingestion.ModelName = $classname(..Model)
set ..Ingestion.ModelData = stream
} catch ex {
set ret = ex.AsStatus()
}
quit ret
}
/// Save all Inbox related data (deep)
Method SaveData() As %Status
{
quit ..Inbox.%Save(1)
}
/// Build StagingReq message to send to the next phase
Method BuildStagingReq() As %Status
{
set ret = $$$OK
try {
set ..StagingReq = ##class(DataPipe.Msg.StagingReq).%New()
set ..StagingReq.data = ..Ingestion
} catch ex {
set ret = ex.AsStatus()
}
quit ret
}
/// Error handling in BP
Method GetErrorInfo()
{
try {
// get error
set errorText = $system.Status.GetOneStatusText(..%Context.%LastError)
if ..%Context.%LastFault'="" {
set errorText = ..%Context.%LastFault
}
$$$LOGERROR(errorText)
set ..ErrorText = errorText
// update status
set ..Ingestion.Inbox.Status = "ERROR-INGESTING"
$$$ThrowOnError(..Ingestion.Inbox.%Save())
} catch ex {
$$$LOGERROR("INTERNAL ERROR: "_ex.DisplayString())
}
}
Storage Default
{
<Data name="InboxManagerContextDefaultData">
<Subscript>"InboxManagerContext"</Subscript>
<Value name="1">
<Value>MyName</Value>
</Value>
<Value name="2">
<Value>Inbox</Value>
</Value>
<Value name="3">
<Value>Ingestion</Value>
</Value>
<Value name="4">
<Value>StageInput</Value>
</Value>
<Value name="5">
<Value>StageModel</Value>
</Value>
<Value name="6">
<Value>Model</Value>
</Value>
<Value name="7">
<Value>StagingReq</Value>
</Value>
<Value name="8">
<Value>InboxAttributes</Value>
</Value>
<Value name="9">
<Value>ErrorText</Value>
</Value>
</Data>
<DefaultData>InboxManagerContextDefaultData</DefaultData>
<Type>%Storage.Persistent</Type>
}
}