Skip to content

Commit 4a946eb

Browse files
author
Derek Smart
committed
Merge branch 'feature/check-for-job-container-conflicts'
2 parents 72ac03a + 1fd1b06 commit 4a946eb

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

src/main/kotlin/com/delphix/yamlparser/Parser.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.delphix.yamlparser
22

33
import com.github.ajalt.clikt.core.CliktCommand
44
import com.github.ajalt.clikt.parameters.options.*
5+
import com.github.ajalt.clikt.parameters.types.int
6+
57
import io.github.cdimascio.dotenv.dotenv as Dotenv
68

79
import com.delphix.yamlparser.sdk.Delphix as Delphix
@@ -73,6 +75,8 @@ object Parser {
7375
class Parse : CliktCommand() {
7476
val env: String by option(help="Path to env file.").default(".env")
7577
val bookmark: String by option(help="Bookmark name.").default("")
78+
val retryLimit: Int by option(help="Number of times to wait for a busy job.").int().default(5)
79+
val waitTime: Int by option(help="Seconds to wait for a busy job before retrying.").int().default(30)
7680

7781
override fun run(){
7882
val file = File("delphix.yaml")
@@ -94,7 +98,7 @@ object Parser {
9498
val env: Map<String, String> = loadEnvs(env)
9599
val delphix: Delphix = Delphix(Http(env["delphixEngine"]?: ""))
96100
val yaml: Yaml = Mapper().mapYaml(contents)
97-
val runner: Runner = Runner(yaml, env, delphix, bookmark)
101+
val runner: Runner = Runner(yaml, env, delphix, bookmark, retryLimit, waitTime)
98102

99103
try {
100104
runner.run()

src/main/kotlin/com/delphix/yamlparser/Runner.kt

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ class Runner (
99
val yaml: Yaml,
1010
val env: Map<String, String>,
1111
val delphix: Delphix,
12-
val bookmark: String
12+
val bookmark: String,
13+
val retryLimit: Int,
14+
val waitTime: Int
1315
) {
1416
var currentAction: JSONObject = JSONObject()
1517

@@ -22,7 +24,6 @@ class Runner (
2224
}
2325

2426
fun callDelphix(datapod: String, environment: String, event: String) {
25-
delphix.login(env["delphixUser"]?: "", env["delphixPass"]?: "")
2627
when (event){
2728
"bookmark.create" -> currentAction = delphix.selfServiceBookmark().create(getBuildTag(environment), datapod)
2829
"bookmark.share" -> currentAction = delphix.selfServiceBookmark().share(getBuildTag(environment))
@@ -54,9 +55,29 @@ class Runner (
5455
}
5556
}
5657

58+
fun jobConflictExists(datapod: String): Boolean {
59+
val container = delphix.selfServiceContainer().getRefByName(datapod)
60+
var jobs = delphix.job().getWhereRunning()
61+
for(job in jobs) {
62+
if (job.target == container.reference) return true
63+
}
64+
return false
65+
}
66+
5767
fun execActionPhase(environment: Environment) {
58-
for (action in environment.actions) {
68+
loop@ for (action in environment.actions) {
5969
if (action.event == env["gitEvent"]) {
70+
var tries = 1
71+
while(jobConflictExists(environment.datapod)) {
72+
if (tries > retryLimit) {
73+
println("Retry Limit Exceeded for Job Conflict.")
74+
break@loop
75+
}
76+
println("Job Conflict Exists. Waiting $waitTime seconds to try again.")
77+
val waitMil = waitTime * 1000
78+
Thread.sleep(waitMil.toLong())
79+
tries++
80+
}
6081
callDelphix(environment.datapod, environment.name, action.action)
6182
outputStatus(environment.name, action.event, action.action)
6283
} else {
@@ -66,6 +87,7 @@ class Runner (
6687
}
6788

6889
fun run() {
90+
delphix.login(env["delphixUser"]?: "", env["delphixPass"]?: "")
6991
for(environment in yaml.environments) {
7092
if (environment.branch == env["gitBranch"]) execActionPhase(environment)
7193
}

src/main/kotlin/com/delphix/yamlparser/sdk/repos/Job.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,26 @@ class Job (
1313
) {
1414
val resource: String = "/resources/json/delphix/job"
1515

16+
fun list(): List<JobObj> {
17+
var jobs = mutableListOf<JobObj>()
18+
val response = http.handleGet("$resource").getJSONArray("result")
19+
for (i in 0 until response.length()) {
20+
val job = response.getJSONObject(i);
21+
jobs.add(JobObj.fromJson(job))
22+
}
23+
return jobs
24+
}
25+
26+
fun getWhereRunning(): List<JobObj> {
27+
var jobs = mutableListOf<JobObj>()
28+
val response = http.handleGet("$resource?jobState=RUNNING").getJSONArray("result")
29+
for (i in 0 until response.length()) {
30+
val job = response.getJSONObject(i);
31+
jobs.add(JobObj.fromJson(job))
32+
}
33+
return jobs
34+
}
35+
1636
fun get(ref: String): JobObj {
1737
val response = http.handleGet("$resource/$ref")
1838
val job = JobObj.fromJson(response.getJSONObject("result"))

src/test/kotlin/com/delphix/yamlparser/RunnerTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class RunnerTest {
2424
val yaml: Yaml = Mapper().mapYaml(mapJsonToNode(jsonString))
2525
val env: Map<String, String> = mapOf("gitBranch" to "origin/develop", "gitCommit" to "0bb822091eed2ae15d67ed91f3ba8591b39e6c4e", "gitEvent" to "push", "delphixEngine" to "delphixEngine", "delphixUser" to "delphixUser", "delphixPass" to "delphixPass", "delphixRepository" to "Postgres vFiles (9.6.8)")
2626
val delphix : Delphix = mock()
27-
val runner: Runner = Runner(yaml, env, delphix, "bookmark")
27+
val runner: Runner = Runner(yaml, env, delphix, "bookmark", 5, 30)
2828

2929
@Test fun `can call Delphix`() : Unit {
3030
/*

0 commit comments

Comments
 (0)