-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#415 Add a pipeline notification target for ECS cleanup.
- Loading branch information
Showing
3 changed files
with
141 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
.../src/main/scala/za/co/absa/pramen/extras/notification/EcsPipelineNotificationTarget.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2022 ABSA Group Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package za.co.absa.pramen.extras.notification | ||
|
||
import com.typesafe.config.Config | ||
import org.apache.http.impl.client.CloseableHttpClient | ||
import org.slf4j.LoggerFactory | ||
import za.co.absa.pramen.api.{PipelineNotificationTarget, TaskNotification, TaskStatus} | ||
import za.co.absa.pramen.extras.utils.ConfigUtils | ||
|
||
import java.time.Instant | ||
|
||
/** | ||
* Runs the ECS cleanup API against the target partition after the job jas completed. | ||
* | ||
* Example usage: | ||
* {{{ | ||
* pramen.ecs.api { | ||
* url = "https://dummy.local" | ||
* key = "aabbcc" | ||
* trust.all.ssl.certificates = false | ||
* } | ||
* | ||
* pramen.pipeline.notification.targets = [ "za.co.absa.pramen.extras.notification.EcsPipelineNotificationTarget" ] | ||
* }}} | ||
*/ | ||
class EcsPipelineNotificationTarget(conf: Config) extends PipelineNotificationTarget { | ||
private val log = LoggerFactory.getLogger(this.getClass) | ||
|
||
override def config: Config = conf | ||
|
||
/** Sends a notification after completion of the pipeline. */ | ||
override def sendNotification(pipelineStarted: Instant, | ||
applicationId: Option[String], | ||
appException: Option[Throwable], | ||
tasksCompleted: Seq[TaskNotification]): Unit = { | ||
val (ecsApiUrl, ecsApiKey, trustAllSslCerts) = EcsPipelineNotificationTarget.getEcsDetails(conf) | ||
|
||
val httpClient = getHttpClient(trustAllSslCerts) | ||
|
||
try { | ||
tasksCompleted.foreach { task => | ||
(task.infoDate, task.status) match { | ||
case (Some(infoDate), _: TaskStatus.Succeeded) => | ||
EcsNotificationTarget.cleanUpS3VersionsForTable(task.tableDef, infoDate, ecsApiUrl, ecsApiKey, httpClient) | ||
case (Some(infoDate), _) => | ||
log.info(s"The task outputting to '${task.tableName}' for '$infoDate' status is not a success - skipping ECS cleanup...") | ||
case (None, status) => | ||
log.info(s"The task outputting to '${task.tableName}' status is not a success - skipping ECS cleanup...") | ||
} | ||
} | ||
} finally { | ||
httpClient.close() | ||
} | ||
} | ||
|
||
protected def getHttpClient(trustAllSslCerts: Boolean): CloseableHttpClient = { | ||
EcsNotificationTarget.getHttpClient(trustAllSslCerts) | ||
} | ||
} | ||
|
||
object EcsPipelineNotificationTarget { | ||
val ECS_API_URL_KEY = "pramen.ecs.api.url" | ||
val ECS_API_KEY_KEY = "pramen.ecs.api.key" | ||
val ECS_API_TRUST_SSL_KEY = "pramen.ecs.api.trust.all.ssl.certificates" | ||
|
||
private[extras] def getEcsDetails(conf: Config): (String, String, Boolean) = { | ||
require(conf.hasPath(ECS_API_URL_KEY), s"The key is not defined: '$ECS_API_URL_KEY'") | ||
require(conf.hasPath(ECS_API_KEY_KEY), s"The key is not defined: '$ECS_API_KEY_KEY'") | ||
|
||
val ecsApiUrl = conf.getString(ECS_API_URL_KEY) | ||
val ecsApiKey = conf.getString(ECS_API_KEY_KEY) | ||
val trustAllSslCerts = ConfigUtils.getOptionBoolean(conf, ECS_API_TRUST_SSL_KEY).getOrElse(false) | ||
|
||
(ecsApiUrl, ecsApiKey, trustAllSslCerts) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters