Skip to content

Commit

Permalink
#423 Add pipeline info to be available from Pramen object.
Browse files Browse the repository at this point in the history
  • Loading branch information
yruslan committed Jun 17, 2024
1 parent e7446d8 commit b2b50ce
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pramen/api/src/main/scala/za/co/absa/pramen/api/Pramen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ trait Pramen {
/** This gives access to the current workflow configuration. */
def workflowConfig: Config

/** General information about the running pipeline. */
def pipelineInfo: PipelineInfo

/** Gets the notification builder that you can use to add custom information to email notifications. */
def notificationBuilder: NotificationBuilder

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ package za.co.absa.pramen.api.mocks

import com.typesafe.config.Config
import za.co.absa.pramen.api.common.BuildPropertiesRetriever
import za.co.absa.pramen.api.{MetadataManager, NotificationBuilder, Pramen, TaskNotification}
import za.co.absa.pramen.api.{MetadataManager, NotificationBuilder, PipelineInfo, Pramen, TaskNotification}

class DummyPramen extends Pramen {
override def buildProperties: BuildPropertiesRetriever = null

override def workflowConfig: Config = null

override def pipelineInfo: PipelineInfo = null

override def notificationBuilder: NotificationBuilder = null

override def metadataManager: MetadataManager = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package za.co.absa.pramen.core
import com.typesafe.config.Config
import za.co.absa.pramen.api.app.PramenFactory
import za.co.absa.pramen.api.common.BuildPropertiesRetriever
import za.co.absa.pramen.api.{MetadataManager, NotificationBuilder, Pramen, TaskNotification}
import za.co.absa.pramen.api.{MetadataManager, NotificationBuilder, PipelineInfo, Pramen, TaskNotification}
import za.co.absa.pramen.core.state.{NotificationBuilderImpl, PipelineState, PipelineStateImpl, PipelineStateSnapshot}
import za.co.absa.pramen.core.utils.BuildPropertyUtils

Expand All @@ -38,6 +38,14 @@ class PramenImpl extends Pramen {
throw new IllegalStateException("Workflow configuration is not available at the context.")
)

override def pipelineInfo: PipelineInfo = {
val pipelineState = _pipelineState.getOrElse(
throw new IllegalStateException("Pipeline state is not available at the context.")
)

pipelineState.getState().pipelineInfo
}

override def notificationBuilder: NotificationBuilder = notificationBuilderImpl

override def metadataManager: MetadataManager = _metadataManager.getOrElse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,40 @@ class PramenImplSuite extends AnyWordSpec {
}
}

"pipelineInfo" should {
"return pipeline info when it is available" in {
val pramen = PramenImpl.instance.asInstanceOf[PramenImpl]

val taskResults = Seq(
TaskResultFactory.getDummyTaskResult(),
TaskResultFactory.getDummyTaskResult(runInfo = None),
TaskResultFactory.getDummyTaskResult(runStatus = RunStatus.NotRan)
)

val pipelineState = mock(classOf[PipelineState])
when(pipelineState.getState()).thenReturn(PipelineStateSnapshotFactory.getDummyPipelineStateSnapshot(taskResults = taskResults))

pramen.setPipelineState(pipelineState)

val pipelineInfo = PramenImpl.instance.pipelineInfo

assert(pipelineInfo.pipelineName == "Dummy Pipeline")
assert(pipelineInfo.environment == "DEV")

pramen.setPipelineState(null)
}

"throw an exception if pipeline state is not available" in {
val pramen = PramenImpl.instance.asInstanceOf[PramenImpl]

pramen.setPipelineState(null)

assertThrows[IllegalStateException] {
PramenImpl.instance.pipelineInfo
}
}
}

"getCompletedTasks()" should {
"return the metadata manager if it is available" in {
val pramen = PramenImpl.instance.asInstanceOf[PramenImpl]
Expand Down

0 comments on commit b2b50ce

Please sign in to comment.