@@ -135,45 +135,68 @@ class MongogeeService {
135
135
}
136
136
}
137
137
138
+ /**
139
+ * Apply change with given changeSet method and changeLog object.
140
+ *
141
+ * @param changeSetMethod {@link java.lang.reflect.Method} instance whose definition is {@link ChangeSet} annotated
142
+ * @param changeLogInstance changeLog instance whose definition is {@link ChangeLog} annotated
143
+ * @param changeAgent {@link ChangeAgent} instance
144
+ */
138
145
protected applyChangeSet (Method changeSetMethod , changeLogInstance , ChangeAgent changeAgent ) {
139
146
ChangeEntry changeEntry = buildChangeEntry(changeSetMethod)
140
147
if (! changeEntry) {
141
- return // return from current iteration
148
+ return
142
149
}
143
150
144
151
ChangeEntry existingChangeEntry = ChangeEntry . findByChangeSetId(changeEntry. changeSetId)
145
152
146
- // todo: try-catch the invocation so the error/exp msg can be saved to changeEntryLog domain record
147
-
148
153
if (! existingChangeEntry) {
149
154
log. info " applying new changeSet: $changeEntry "
150
- invokeChangeSetMethod(changeSetMethod, changeLogInstance)
151
-
152
- ChangeEntry . withNewTransaction {
153
- changeEntry. save(failOnError : true , flush : true )
154
- buildChangeEntryLog(changeEntry). save(failOnError : true , flush : true )
155
- }
156
- log. info " ... changeSet applied"
155
+ invokeChangeSetWithEntry(changeSetMethod, changeEntry, changeLogInstance)
157
156
158
157
} else if (changeAgent. isRunAlwaysChangeSet(changeSetMethod)) {
159
- log. info " reapplying changeSet: $changeEntry "
160
- invokeChangeSetMethod(changeSetMethod, changeLogInstance)
161
158
162
- ChangeEntry . withNewTransaction {
163
- if (changeEntry. author) {
164
- existingChangeEntry. author = changeEntry. author
165
- }
166
- existingChangeEntry. runCount + = 1
167
- existingChangeEntry. save(failOnError : true , flush : true )
168
- buildChangeEntryLog(existingChangeEntry). save(failOnError : true , flush : true )
159
+ // merge changeEntry into existing changeEntry and update other attributes
160
+ if (changeEntry. author) {
161
+ existingChangeEntry. author = changeEntry. author
169
162
}
170
- log. info " ... changeSet reapplied"
163
+ existingChangeEntry. runCount + = 1
164
+ changeEntry = existingChangeEntry
165
+
166
+ log. info " reapplying existing changeSet: $changeEntry "
167
+ invokeChangeSetWithEntry(changeSetMethod, changeEntry, changeLogInstance)
171
168
172
169
} else {
173
170
log. info " changeSet skipped: $changeEntry "
174
171
}
175
172
}
176
173
174
+ /**
175
+ * Invoke the chagneSet, save changeEntry info, and log success or failure, when there's exception, the exception
176
+ * is saved to change entry log, and then bubbled up to main execution flow.
177
+ */
178
+ protected invokeChangeSetWithEntry (Method changeSetMethod , ChangeEntry changeEntry , changeLogInstance ) {
179
+ try {
180
+ invokeChangeSetMethod(changeSetMethod, changeLogInstance)
181
+ log. info " changeSet invoked"
182
+
183
+ ChangeEntry . withNewTransaction {
184
+ changeEntry. save(failOnError : true , flush : true )
185
+ def changeEntryLog = buildChangeEntryLog(changeEntry). save(failOnError : true )
186
+ log. debug " changeEntry saved [id: ${ changeEntry.id} ], and logged [id: ${ changeEntryLog.id} ]"
187
+ }
188
+ } catch (ex) {
189
+ log. error " changeSet invocation error: ${ ex.message ?: ex.toString().take(255)} "
190
+ // save error information to change entry log
191
+ ChangeEntryLog . withNewTransaction {
192
+ def changeEntryLog = buildChangeEntryErrorLog(changeEntry, ex). save(failOnError : true )
193
+ log. debug " changeSet invocation error logged [id: ${ changeEntryLog.id} ]"
194
+ }
195
+ // bubble exp to main flow
196
+ throw ex
197
+ }
198
+ }
199
+
177
200
/**
178
201
* Invoke changeSet method based on method argument specification.
179
202
*/
@@ -202,6 +225,13 @@ class MongogeeService {
202
225
}
203
226
}
204
227
228
+ /**
229
+ * Builds a change entry object from the input annotated {@link ChangeSet} method.
230
+ * If the input method is not valid, return null.
231
+ *
232
+ * @param changeSetMethod {@link ChangeSet} annotated {@link java.lang.reflect.Method} object
233
+ * @return {@link ChangeEntry} object, or null
234
+ */
205
235
protected ChangeEntry buildChangeEntry (Method changeSetMethod ) {
206
236
if (changeSetMethod. isAnnotationPresent(ChangeSet . class)) {
207
237
ChangeSet annotation = changeSetMethod. getAnnotation(ChangeSet . class)
@@ -227,4 +257,11 @@ class MongogeeService {
227
257
return cel
228
258
}
229
259
260
+ protected ChangeEntryLog buildChangeEntryErrorLog (ChangeEntry changeEntry , Exception exp ) {
261
+ ChangeEntryLog cel = buildChangeEntryLog(changeEntry)
262
+ cel. status = ' fail'
263
+ cel. message = exp. message ?: exp. toString(). take(255 )
264
+ return cel
265
+ }
266
+
230
267
}
0 commit comments