mdeuser commented on a change in pull request #3187: reduce rule activation
records
URL:
https://github.com/apache/incubator-openwhisk/pull/3187#discussion_r161860254
##########
File path: core/controller/src/main/scala/whisk/core/controller/Triggers.scala
##########
@@ -122,72 +126,135 @@ trait WhiskTriggersApi extends WhiskCollectionAPI {
version = trigger.version,
duration = None)
- logging.info(this, s"[POST] trigger activated, writing activation
record to datastore: $triggerActivationId")
- WhiskActivation.put(activationStore, triggerActivation) recover {
- case t =>
- logging.error(this, s"[POST] storing trigger activation
$triggerActivationId failed: ${t.getMessage}")
- }
-
- val url = Uri(s"http://localhost:${whiskConfig.servicePort}")
-
- trigger.rules.map {
- _.filter {
- case (ruleName, rule) => rule.status == Status.ACTIVE
- } foreach {
- case (ruleName, rule) =>
- val ruleActivationId = activationIdFactory.make()
- val ruleActivation = WhiskActivation(
- namespace = user.namespace.toPath, // all activations should
end up in the one space regardless trigger.namespace,
- ruleName.name,
- user.subject,
- ruleActivationId,
- Instant.now(Clock.systemUTC()),
- Instant.EPOCH,
- cause = Some(triggerActivationId),
- response = ActivationResponse.success(),
- version = trigger.version,
- duration = None)
- WhiskActivation.put(activationStore, ruleActivation) recover {
- case t =>
- logging.error(this, s"[POST] storing rule activation
$ruleActivationId failed: ${t.getMessage}")
- }
-
- val actionNamespace = rule.action.path.root.asString
- val actionPath = {
- rule.action.path.relativePath.map { pkg =>
- (Path.SingleSlash + pkg.namespace) /
rule.action.name.asString
- } getOrElse {
- Path.SingleSlash + rule.action.name.asString
- }
- }.toString
-
- val actionUrl = Path("/api/v1") / "namespaces" /
actionNamespace / "actions"
- val request = HttpRequest(
- method = POST,
- uri = url.withPath(actionUrl + actionPath),
- headers =
-
List(Authorization(BasicHttpCredentials(user.authkey.uuid.asString,
user.authkey.key.asString))),
- entity = HttpEntity(MediaTypes.`application/json`,
args.getOrElse(JsObject()).compactPrint))
-
- Http().singleRequest(request).map {
- response =>
- response.status match {
- case OK | Accepted =>
- Unmarshal(response.entity).to[JsObject].map { a =>
- logging.info(this, s"${rule.action} activated
${a.fields("activationId")}")
- }
- case NotFound =>
- response.discardEntityBytes()
- logging.info(this, s"${rule.action} failed, action not
found")
- case _ =>
- Unmarshal(response.entity).to[String].map { error =>
- logging.warn(this, s"${rule.action} failed due to
$error")
+ // List of active rules associated with the trigger
+ val activeRules = trigger.rules map { _.filter((r) => r._2.status ==
Status.ACTIVE) } getOrElse Map.empty
+
+ if (activeRules.nonEmpty) {
+ val args = trigger.parameters.merge(payload)
+
+ // Iterate through each active rule; invoking each mapped action
+ val actionLogList = activeRules
+ .map {
+ case (ruleName, rule) =>
+ // Build the url to invoke an action mapped to the rule
+ val actionNamespace = rule.action.path.root.asString
+ val actionPath = {
+ rule.action.path.relativePath.map { pkg =>
+ (Path.SingleSlash + pkg.namespace) /
rule.action.name.asString
+ } getOrElse {
+ Path.SingleSlash + rule.action.name.asString
+ }
+ }.toString
+
+ val actionUrl = Path("/api/v1") / "namespaces" /
actionNamespace / "actions"
+ val request = HttpRequest(
+ method = POST,
+ uri = url.withPath(actionUrl + actionPath),
+ headers =
+
List(Authorization(BasicHttpCredentials(user.authkey.uuid.asString,
user.authkey.key.asString))),
+ entity = HttpEntity(MediaTypes.`application/json`,
args.getOrElse(JsObject()).compactPrint))
+
+ // Invoke the action. Retain action results for inclusion in
the trigger activation record
+ Http()
+ .singleRequest(request)
+ .flatMap {
+ response =>
+ response.status match {
+ case OK | Accepted =>
+ Unmarshal(response.entity).to[JsObject].map { a =>
+ logging.info(
+ this,
+ s"trigger-fired rule '${rule.action}' invoked
with activation ${a.fields("activationId")}")
+ (ruleName.asString -> triggerActivationLogMsg(
+ entityName.asString,
+ ruleName.asString,
+ rule.action.asString,
+ InfoLevel,
Review comment:
i was thinking it would be good to flag invocation failures with a tag that
can eventually be consumed by a logging service. here's some sample output
```
"logs": [
"[2018-01-16T16:23:17.174Z] [INFO] [guest/wtrig] [guest/wrule]
[guest/weather] 'guest/weather' activated \"3c2aaf37e07549cbaaaf37e07519cb19\"",
"[2018-01-16T16:23:17.175Z] [INFO] [guest/wtrig] [guest/wrule2]
[guest/echo-env-web] 'guest/echo-env-web' activated
\"8e2b7c0f04084c53ab7c0f04087c5386\"",
"[2018-01-16T16:23:16.948Z] [ERROR] [guest/wtrig] [guest/wrule3]
[guest/badrule] 'guest/badrule' failed, action not found"
],
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services