diff --git a/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt b/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt index bb8cf7829..5a80230a5 100644 --- a/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt +++ b/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt @@ -438,16 +438,7 @@ object ManagedIndexRunner : var executedManagedIndexMetaData = startingManagedIndexMetaData.getCompletedManagedIndexMetaData(action, step) if (executedManagedIndexMetaData.isFailed) { - try { - // if the policy has no error_notification this will do nothing otherwise it will try to send the configured error message - publishErrorNotification(policy, executedManagedIndexMetaData) - } catch (e: Exception) { - logger.error("Failed to publish error notification", e) - val errorMessage = e.message ?: "Failed to publish error notification" - val mutableInfo = executedManagedIndexMetaData.info?.toMutableMap() ?: mutableMapOf() - mutableInfo["errorNotificationFailure"] = errorMessage - executedManagedIndexMetaData = executedManagedIndexMetaData.copy(info = mutableInfo.toMap()) - } + executedManagedIndexMetaData = publishErrorNotification(policy, executedManagedIndexMetaData) } if (executedManagedIndexMetaData.isSuccessfulDelete) { @@ -816,13 +807,27 @@ object ManagedIndexRunner : } } - private suspend fun publishErrorNotification(policy: Policy, managedIndexMetaData: ManagedIndexMetaData) { - policy.errorNotification?.run { - errorNotificationRetryPolicy.retry(logger) { - val compiledMessage = compileTemplate(messageTemplate, managedIndexMetaData) - destination?.buildLegacyBaseMessage(null, compiledMessage)?.publishLegacyNotification(client) - channel?.sendNotification(client, ErrorNotification.CHANNEL_TITLE, managedIndexMetaData, compiledMessage, policy.user) + private suspend fun publishErrorNotification(policy: Policy, metadata: ManagedIndexMetaData): ManagedIndexMetaData { + return try { + // if the policy has no error_notification this will do nothing otherwise it will try to send the configured error message + policy.errorNotification?.run { + errorNotificationRetryPolicy.retry(logger) { + val compiledMessage = compileTemplate(messageTemplate, metadata) + destination?.buildLegacyBaseMessage(null, compiledMessage)?.publishLegacyNotification(client) + channel?.sendNotification(client, ErrorNotification.CHANNEL_TITLE, metadata, compiledMessage, policy.user) + } } + val message = "Successfully published error notification [index = ${metadata.index}]" + logger.info(message) + val mutableInfo = metadata.info?.toMutableMap() ?: mutableMapOf() + mutableInfo["error_notification"] = message + metadata.copy(info = mutableInfo.toMap()) + } catch (e: Exception) { + logger.error("Failed to publish error notification", e) + val errorMessage = e.message ?: "Failed to publish error notification" + val mutableInfo = metadata.info?.toMutableMap() ?: mutableMapOf() + mutableInfo["error_notification"] = errorMessage + metadata.copy(info = mutableInfo.toMap()) } }