in core/controller/src/main/scala/org/apache/openwhisk/core/controller/Rules.scala [132:188]
override def activate(user: Identity, entityName: FullyQualifiedEntityName, env: Option[Parameters])(
implicit transid: TransactionId) = {
extractStatusRequest { requestedState =>
val docid = entityName.toDocId
getEntity(WhiskRule.get(entityStore, docid), Some {
rule: WhiskRule =>
val ruleName = rule.fullyQualifiedName(false)
val changeStatus = getTrigger(rule.trigger) map { trigger =>
getStatus(trigger, ruleName)
} flatMap {
oldStatus =>
if (requestedState != oldStatus) {
logging.debug(this, s"[POST] rule state change initiated: ${oldStatus} -> $requestedState")
Future successful requestedState
} else {
logging.debug(
this,
s"[POST] rule state will not be changed, the requested state is the same as the old state: ${oldStatus} -> $requestedState")
Future failed { IgnoredRuleActivation(requestedState == oldStatus) }
}
} flatMap {
case (newStatus) =>
logging.debug(this, s"[POST] attempting to set rule state to: ${newStatus}")
WhiskTrigger.get(entityStore, rule.trigger.toDocId) flatMap { trigger =>
val newTrigger = trigger.removeRule(ruleName)
val triggerLink = ReducedRule(rule.action, newStatus)
WhiskTrigger.put(entityStore, newTrigger.addRule(ruleName, triggerLink), Some(trigger))
}
}
onComplete(changeStatus) {
case Success(response) =>
complete(OK)
case Failure(t) =>
t match {
case _: DocumentConflictException =>
logging.debug(this, s"[POST] rule update conflict")
terminate(Conflict, conflictMessage)
case IgnoredRuleActivation(ok) =>
logging.debug(this, s"[POST] rule update ignored")
if (ok) complete(OK) else terminate(Conflict)
case _: NoDocumentException =>
logging.debug(this, s"[POST] the trigger attached to the rule doesn't exist")
terminate(NotFound, "Only rules with existing triggers can be activated")
case _: DeserializationException =>
logging.error(this, s"[POST] rule update failed: ${t.getMessage}")
terminate(InternalServerError, corruptedEntity)
case _: Throwable =>
logging.error(this, s"[POST] rule update failed: ${t.getMessage}")
terminate(InternalServerError)
}
}
})
}
}