Skip to content

Commit ef2fa5c

Browse files
committed
#64 Created triggers-project
1 parent 81540ad commit ef2fa5c

File tree

8 files changed

+91
-49
lines changed

8 files changed

+91
-49
lines changed

examples/sample-project/build.gradle

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -158,39 +158,6 @@ ext {
158158
}
159159

160160

161-
/*
162-
* As of ML 8.0-3, triggers can not yet be managed via the Management REST API, so we still rely on a custom Gradle task
163-
* that uses XCC to create the trigger. With 8.0-4, you should not use this approach - triggers can instead be defined under src/main/ml-config/triggers.
164-
*/
165-
task createSampleTrigger(type: com.marklogic.gradle.task.trigger.CreateTriggersTask) {
166-
xccUrl = contentXccUrl
167-
triggerName = "sample-project-trigger"
168-
description = "This is an example of creating a trigger"
169-
dataEventCommit = "post"
170-
dataEventScopeArgs = ["some-collection"]
171-
dataEventContentArgs = ["create", "modify", "delete"]
172-
moduleRoot = "/"
173-
modulePath = "/ext/sample-project/trigger/sample-trigger.xqy"
174-
}
175-
176-
177-
/*
178-
* Example of attaching the creation of our trigger to the end of the mlDeploy process. mlPostDeploy is an empty task
179-
* that is called by mlDeploy after mlAppDeploy runs; mlPostDeploy thus allows for any additional tasks to be added to
180-
* the deployment process.
181-
*/
182-
mlPostDeploy.dependsOn createSampleTrigger
183-
184-
185-
/*
186-
* Example of including a custom command in the deployment process. The order in which it's invoked is determined by
187-
* the command's "getExecuteSortOrder" method.
188-
*/
189-
ext {
190-
mlAppDeployer.commands.add(new ClearTriggersDatabaseCommand())
191-
}
192-
193-
194161
/*
195162
* ml-gradle does not blindly process every file in the ml-config/databases directory. It checks for specific files
196163
* for a content database, a schemas database, and a triggers database. There are specific commands for each file, and
@@ -214,38 +181,54 @@ ext {
214181

215182

216183
/*
217-
* Below is an example of a custom command, which also fixes a bug in ML that should be fixed in the 8.0-4 release. The
218-
* bug is - when a pipeline is updated via the Management REST API, pipeline states are dropped. To address this, we
219-
* insert a command to clear the triggers database before any CPF resources are created, thus forcing a create instead
220-
* of an update, thereby bypassing the bug. This example also shows how the ManageClient instance in CommandContext can
221-
* be used to easily make any call to the Management REST API. Note that this class could also be defined in the Gradle
222-
* buildSrc directory, which is really a better option for keeping your build.gradle file slim.
184+
* Example of a custom command, which in this case performs a merge on a database. The benefit of doing this in a
185+
* command (as opposed to a task) is that a command can be included in the mlDeploy and mlUndeploy tasks by adding it to
186+
* the mlAppDeployer object, as shown below.
187+
*
188+
* This example also shows how the ManageClient instance in CommandContext can be used to easily make any call to the
189+
* Management REST API. Note that this class could also be defined in the Gradle buildSrc directory, which is really a
190+
* better option for keeping your build.gradle file slim.
223191
*/
224192
import com.marklogic.appdeployer.command.AbstractCommand;
225193
import com.marklogic.appdeployer.command.CommandContext;
226-
import com.marklogic.appdeployer.command.SortOrderConstants;
227194

228-
class ClearTriggersDatabaseCommand extends AbstractCommand {
195+
class MergeContentDatabaseCommand extends AbstractCommand {
229196
public Integer getExecuteSortOrder() {
230-
return SortOrderConstants.DEPLOY_DEFAULT_PIPELINES - 1;
197+
return Integer.MAX_VALUE;
231198
}
232199
public void execute(CommandContext context) {
233-
String dbName = context.getAppConfig().getTriggersDatabaseName();
234-
logger.info("Clearing database: " + dbName);
235-
context.getManageClient().postJson("/manage/v2/databases/" + dbName, '{"operation":"clear-database"}');
236-
logger.info("Cleared database: " + dbName)
200+
String dbName = context.getAppConfig().getContentDatabaseName();
201+
logger.info("Merging database: " + dbName);
202+
context.getManageClient().postJson("/manage/v2/databases/" + dbName, '{"operation":"merge-database"}');
203+
logger.info("Merged database: " + dbName)
237204
}
238205
}
239206

240207

241208
/*
242-
* Sometimes, you need a custom step, but you want it to be a separate task - not a command that's included when
243-
* mlDeploy is invoked. Below is a task that extends MarkLogicTask, which provides access to a number of helpful
244-
* objects for implementing custom functionality.
209+
* Example of including our custom command in the deployment process. The order in which it's invoked is determined by
210+
* the command's "getExecuteSortOrder" method.
211+
*/
212+
ext {
213+
//mlAppDeployer.commands.add(new MergeContentDatabaseCommand())
214+
}
215+
216+
217+
/*
218+
* Below is an example of a custom task that merges a database - just like the command above. The benefit of creating a
219+
* task versus a command is that we can invoke this without running a full mlDeploy. The MarkLogicTask class provides
220+
* access to a number of helpful objects via methods like getAppConfig() and getManageClient().
245221
*/
246222
task mergeContentDatabase(type: com.marklogic.gradle.task.MarkLogicTask) {
247223
description = "Call this task to initiate a merge on the content database"
248224
doLast {
249225
getManageClient().postJson("/manage/v2/databases/" + getAppConfig().getContentDatabaseName(), '{"operation":"merge-database"}')
250226
}
251227
}
228+
229+
/*
230+
* Example of attaching our custom task to the end of the mlDeploy process. mlPostDeploy is an empty task
231+
* that is called by mlDeploy after mlAppDeploy runs; mlPostDeploy thus allows for any additional tasks to be added to
232+
* the deployment process.
233+
*/
234+
//mlPostDeploy.dependsOn mergeContentDatabase
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/bin
2+
.classpath
3+
.project
4+
/build
5+
.gradle
6+
.settings
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
plugins {
2+
id "com.marklogic.ml-gradle" version "2.0rc3"
3+
}
4+
5+
/**
6+
* Starting with MarkLogic 8.0-4, triggers can be added via configuration files in the src/main/ml-config/triggers
7+
* directory. Prior to that, you'll need to use a task like the one below, as previous version of MarkLogic don't
8+
* support creating triggers via the Management REST API.
9+
*/
10+
task createSampleTrigger(type: com.marklogic.gradle.task.trigger.CreateTriggersTask) {
11+
description = "This is an example of creating a trigger via XCC as opposed to the Management REST API"
12+
xccUrl = "xcc://${mlUsername}:${mlPassword}@${mlHost}:${mlRestPort}"
13+
triggerName = "sample-trigger"
14+
dataEventCommit = "post"
15+
dataEventScopeArgs = ["sample"]
16+
dataEventContentArgs = ["create", "modify", "delete"]
17+
moduleRoot = "/"
18+
modulePath = "/ext/sample-trigger.xqy"
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mlGradleDependency=com.marklogic:ml-gradle:2.0rc+
2+
3+
mlHost=localhost
4+
mlAppName=triggers-project
5+
mlRestPort=8135
6+
mlUsername=admin
7+
mlPassword=admin
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
While ml-gradle will create a triggers database by default, the content-database.json file still needs to exist to associate
2+
the content database with the triggers database.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"database-name": "%%DATABASE%%",
3+
"triggers-database": "%%TRIGGERS_DATABASE%%"
4+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "my-trigger",
3+
"description": "my trigger",
4+
"event": {
5+
"data-event": {
6+
"collection-scope": {
7+
"uri": "sample"
8+
},
9+
"document-content": {
10+
"update-kind": "create"
11+
},
12+
"when": "post-commit"
13+
}
14+
},
15+
"module": "ext/sample-trigger.xqy",
16+
"module-db": "%%MODULES_DATABASE%%",
17+
"module-root": "/",
18+
"enabled": true,
19+
"recursive": true,
20+
"task-priority": "normal"
21+
}

0 commit comments

Comments
 (0)