mdeuser commented on a change in pull request #3187: reduce rule activation 
records
URL: 
https://github.com/apache/incubator-openwhisk/pull/3187#discussion_r164198480
 
 

 ##########
 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:
   just to wrap up this topic.  @csantanapr, @rabbah and i spoke and decided to 
write out an array of json strings instead of the above log entries.  each json 
string would be a single json object representing the status/result of the 
async rule/action activation.  for example
   ```d
       "logs": [
           
"{\"statusCode\":0,\"success\":true,\"activationId\":\"ae2d98c6b49b4a32ad98c6b49bba32c5\",\"rule\":\"guest/wrule\",\"action\":\"guest/echo-env-web\"}",
           
"{\"statusCode\":0,\"success\":true,\"activationId\":\"32e3afff8a7f4c31a3afff8a7fbc319b\",\"rule\":\"guest/wrule2\",\"action\":\"guest/echo-env-web\"}",
           
"{\"statusCode\":1,\"success\":false,\"rule\":\"guest/wrule3\",\"error\":\"The 
requested resource does not exist.\",\"action\":\"guest/nonexistent-action\"}"
       ]
   ```

----------------------------------------------------------------
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

Reply via email to