@@ -45,14 +45,14 @@ class NomadService implements Closeable{
4545 ApiClient apiClient
4646 JobsApi jobsApi
4747 VariablesApi variablesApi
48+ FailsafeExecutor safeExecutor
4849
4950 NomadService (NomadConfig config ) {
5051 this . config = config
5152
52- // TODO: Accommodate these connection level options in clientOpts()
53- final CONNECTION_TIMEOUT_MILLISECONDS = 60000
54- final READ_TIMEOUT_MILLISECONDS = 60000
55- final WRITE_TIMEOUT_MILLISECONDS = 60000
53+ final CONNECTION_TIMEOUT_MILLISECONDS = config. clientOpts(). connectionTimeout
54+ final READ_TIMEOUT_MILLISECONDS = config. clientOpts(). readTimeout
55+ final WRITE_TIMEOUT_MILLISECONDS = config. clientOpts(). writeTimeout
5656
5757 apiClient = new ApiClient ( connectTimeout : CONNECTION_TIMEOUT_MILLISECONDS , readTimeout : READ_TIMEOUT_MILLISECONDS , writeTimeout : WRITE_TIMEOUT_MILLISECONDS )
5858 apiClient. basePath = config. clientOpts(). address
@@ -64,6 +64,8 @@ class NomadService implements Closeable{
6464 }
6565 this . jobsApi = new JobsApi (apiClient)
6666 this . variablesApi = new VariablesApi (apiClient)
67+
68+ this . safeExecutor = new FailsafeExecutor (config. clientOpts(). retryConfig)
6769 }
6870
6971
@@ -96,8 +98,12 @@ class NomadService implements Closeable{
9698 }
9799
98100 try {
99- JobRegisterResponse jobRegisterResponse = jobsApi. registerJob(jobRegisterRequest, config. jobOpts(). region, config. jobOpts(). namespace, null , null )
100- return jobRegisterResponse. evalID
101+ safeExecutor. apply {
102+ JobRegisterResponse jobRegisterResponse = jobsApi. registerJob(jobRegisterRequest,
103+ config. jobOpts(). region, config. jobOpts(). namespace,
104+ null , null )
105+ jobRegisterResponse. evalID
106+ }
101107 } catch (ApiException apiException) {
102108 log. debug(" [NOMAD] Failed to submit ${ job.name} -- Cause: ${ apiException.responseBody ?: apiException} " , apiException)
103109 throw new ProcessSubmitException (" [NOMAD] Failed to submit ${ job.name} -- Cause: ${ apiException.responseBody ?: apiException} " , apiException)
@@ -110,7 +116,11 @@ class NomadService implements Closeable{
110116
111117 String getJobState (String jobId ){
112118 try {
113- List<AllocationListStub > allocations = jobsApi. getJobAllocations(jobId, config. jobOpts(). region, config. jobOpts(). namespace, null , null , null , null , null , null , null , null )
119+ List<AllocationListStub > allocations = safeExecutor. apply {
120+ jobsApi. getJobAllocations(jobId, config. jobOpts(). region, config. jobOpts(). namespace,
121+ null , null , null , null , null , null ,
122+ null , null )
123+ }
114124 AllocationListStub last = allocations?. sort {
115125 it. modifyIndex
116126 }?. last()
@@ -127,7 +137,10 @@ class NomadService implements Closeable{
127137
128138 boolean checkIfRunning (String jobId ){
129139 try {
130- Job job = jobsApi. getJob(jobId, config. jobOpts(). region, config. jobOpts(). namespace, null , null , null , null , null , null , null )
140+ Job job = safeExecutor. apply {
141+ jobsApi. getJob(jobId, config. jobOpts(). region, config. jobOpts(). namespace,
142+ null , null , null , null , null , null , null )
143+ }
131144 log. debug " [NOMAD] checkIfRunning jobID=$job . ID ; status=$job . status "
132145 job. status == " running"
133146 }catch (Exception e){
@@ -138,7 +151,10 @@ class NomadService implements Closeable{
138151
139152 boolean checkIfDead (String jobId ){
140153 try {
141- Job job = jobsApi. getJob(jobId, config. jobOpts(). region, config. jobOpts(). namespace, null , null , null , null , null , null , null )
154+ Job job = safeExecutor. apply {
155+ jobsApi. getJob(jobId, config. jobOpts(). region, config. jobOpts(). namespace,
156+ null , null , null , null , null , null , null )
157+ }
142158 log. debug " [NOMAD] checkIfDead jobID=$job . ID ; status=$job . status "
143159 job. status == " dead"
144160 }catch (Exception e){
@@ -158,15 +174,22 @@ class NomadService implements Closeable{
158174 protected void purgeJob (String jobId , boolean purge ){
159175 log. debug " [NOMAD] purgeJob with jobId=${ jobId} "
160176 try {
161- jobsApi. deleteJob(jobId, config. jobOpts(). region, config. jobOpts(). namespace, null , null , purge, true )
177+ safeExecutor. apply {
178+ jobsApi. deleteJob(jobId, config. jobOpts(). region, config. jobOpts(). namespace,
179+ null , null , purge, true )
180+ }
162181 }catch (Exception e){
163182 log. debug(" [NOMAD] Failed to delete job ${ jobId} -- Cause: ${ e.message ?: e} " , e)
164183 }
165184 }
166185
167186 String getClientOfJob (String jobId ) {
168187 try {
169- List<AllocationListStub > allocations = jobsApi. getJobAllocations(jobId, config. jobOpts(). region, config. jobOpts(). namespace, null , null , null , null , null , null , null , null )
188+ List<AllocationListStub > allocations = safeExecutor. apply {
189+ jobsApi. getJobAllocations(jobId, config. jobOpts(). region, config. jobOpts(). namespace,
190+ null , null , null , null , null , null ,
191+ null , null )
192+ }
170193 if ( ! allocations ){
171194 return null
172195 }
@@ -183,10 +206,12 @@ class NomadService implements Closeable{
183206 }
184207
185208 String getVariableValue (String path , String key ){
186- var variable = variablesApi. getVariableQuery(" $path /$key " ,
187- config. jobOpts(). region,
188- config. jobOpts(). namespace,
189- null , null , null , null , null , null , null )
209+ var variable = safeExecutor. apply {
210+ variablesApi. getVariableQuery(" $path /$key " ,
211+ config. jobOpts(). region,
212+ config. jobOpts(). namespace,
213+ null , null , null , null , null , null , null )
214+ }
190215 variable?. items?. find{ it. key == key }?. value
191216 }
192217
@@ -197,17 +222,22 @@ class NomadService implements Closeable{
197222 void setVariableValue (String path , String key , String value ){
198223 var content = Map . of(key,value)
199224 var variable = new Variable (path : path, items : content)
200- variablesApi. postVariable(" $path /$key " , variable,
201- config. jobOpts(). region,
202- config. jobOpts(). namespace,
203- null , null , null )
225+ safeExecutor. apply {
226+ variablesApi. postVariable(" $path /$key " , variable,
227+ config. jobOpts(). region,
228+ config. jobOpts(). namespace,
229+ null , null , null )
230+ }
204231 }
205232
206233 List<String > getVariablesList (){
207- var listRequest = variablesApi. getVariablesListRequest(
208- config. jobOpts(). region,
209- config. jobOpts(). namespace,
210- null , null , null , null , null , null , null )
234+ var listRequest = safeExecutor. apply {
235+ variablesApi. getVariablesListRequest(
236+ config. jobOpts(). region,
237+ config. jobOpts(). namespace,
238+ null , null , null , null ,
239+ null , null , null )
240+ }
211241 String path = (config. jobOpts(). secretOpts?. path ?: ' ' )+ " /"
212242 listRequest. collect{ it. path - path}
213243 }
@@ -218,9 +248,11 @@ class NomadService implements Closeable{
218248
219249 void deleteVariable (String path , String key ){
220250 var variable = new Variable ( items : Map . of(key, " " ))
221- variablesApi. deleteVariable(" $path /$key " , variable,
222- config. jobOpts(). region,
223- config. jobOpts(). namespace,
224- null , null , null )
251+ safeExecutor. apply {
252+ variablesApi. deleteVariable(" $path /$key " , variable,
253+ config. jobOpts(). region,
254+ config. jobOpts(). namespace,
255+ null , null , null )
256+ }
225257 }
226258}
0 commit comments