Skip to content

Commit

Permalink
Merge pull request #44 from nextflow-io/dump-job
Browse files Browse the repository at this point in the history
dump job definition if nomad.debug.json = true
  • Loading branch information
abhi18av authored Jun 21, 2024
2 parents 607b889 + a2823d3 commit f692563
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
13 changes: 13 additions & 0 deletions plugins/nf-nomad/src/main/nextflow/nomad/NomadConfig.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ class NomadConfig {

final NomadClientOpts clientOpts
final NomadJobOpts jobOpts
final NomadDebug debug

NomadConfig(Map nomadConfigMap) {
clientOpts = new NomadClientOpts((nomadConfigMap?.client ?: Collections.emptyMap()) as Map)
jobOpts = new NomadJobOpts((nomadConfigMap?.jobs ?: Collections.emptyMap()) as Map)
debug = new NomadDebug((nomadConfigMap?.debug ?: Collections.emptyMap()) as Map)
}

class NomadClientOpts{
Expand Down Expand Up @@ -129,4 +131,15 @@ class NomadConfig {
}
}

static class NomadDebug {

@Delegate
Map<String,Object> target

NomadDebug(Map<String,Object> debug) {
this.target = debug ?: Collections.<String,Object>emptyMap()
}

boolean getJson() { Boolean.valueOf( target.json as String ) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ import nextflow.nomad.NomadConfig
import nextflow.nomad.config.VolumeSpec
import nextflow.processor.TaskRun
import nextflow.util.MemoryUnit
import org.yaml.snakeyaml.Yaml

import java.nio.file.Path

/**
* Nomad Service
Expand Down Expand Up @@ -90,7 +93,7 @@ class NomadService implements Closeable{
void close() throws IOException {
}

String submitTask(String id, TaskRun task, List<String> args, Map<String, String>env){
String submitTask(String id, TaskRun task, List<String> args, Map<String, String>env, Path saveJsonPath=null){
Job job = new Job();
job.ID = id
job.name = task.name
Expand All @@ -102,6 +105,14 @@ class NomadService implements Closeable{

JobRegisterRequest jobRegisterRequest = new JobRegisterRequest();
jobRegisterRequest.setJob(job);

if( saveJsonPath ) try {
saveJsonPath.text = job.toString()
}
catch( Exception e ) {
log.debug "WARN: unable to save request json -- cause: ${e.message ?: e}"
}

JobRegisterResponse jobRegisterResponse = jobsApi.registerJob(jobRegisterRequest, config.jobOpts.region, config.jobOpts.namespace, null, null)
jobRegisterResponse.evalID
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package nextflow.nomad.executor


import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import io.nomadproject.client.model.Resources
Expand Down Expand Up @@ -131,14 +131,19 @@ class NomadTaskHandler extends TaskHandler implements FusionAwareTask {

final taskLauncher = getSubmitCommand(task)
final taskEnv = getEnv(task)
nomadService.submitTask(this.jobName, task, taskLauncher, taskEnv)
nomadService.submitTask(this.jobName, task, taskLauncher, taskEnv, debugPath())

// submit the task execution
log.debug "[NOMAD] Submitted task ${task.name} with taskId=${this.jobName}"
// update the status
this.status = TaskStatus.SUBMITTED
}

protected Path debugPath() {
boolean debug = config.debug?.getJson()
return debug ? task.workDir.resolve('.job.json') : null
}

protected List<String> getSubmitCommand(TaskRun task) {
return fusionEnabled()
? fusionSubmitCli()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import spock.lang.Specification

import java.nio.file.Files
import java.nio.file.Path

/**
Expand Down Expand Up @@ -504,4 +505,60 @@ class NomadServiceSpec extends Specification{
body.Job.TaskGroups[0].Tasks[0].Constraints[0].LTarget == '${meta.my_custom_value}'
}

void "save the job spec if requested"(){
given:
def config = new NomadConfig(
client:[
address : "http://${mockWebServer.hostName}:${mockWebServer.port}"
],
debug:[
json: true
]
)
def service = new NomadService(config)

String id = "theId"
String name = "theName"
String image = "theImage"
List<String> args = ["theCommand", "theArgs"]
String workingDir = "/a/b/c"
Map<String, String>env = [test:"test"]

def mockTask = Mock(TaskRun){
getName() >> name
getContainer() >> image
getConfig() >> Mock(TaskConfig)
getWorkDirStr() >> workingDir
getContainer() >> "ubuntu"
getProcessor() >> Mock(TaskProcessor){
getExecutor() >> Mock(Executor){
isFusionEnabled() >> false
}
}
getWorkDir() >> Path.of(workingDir)
toTaskBean() >> Mock(TaskBean){
getWorkDir() >> Path.of(workingDir)
getScript() >> "theScript"
getShell() >> ["bash"]
getInputFiles() >> [:]
}
}

mockWebServer.enqueue(new MockResponse()
.setBody(JsonOutput.toJson(["EvalID":"test"]).toString())
.addHeader("Content-Type", "application/json"));

def outputJson = Files.createTempFile("nomad",".json")
when:

def idJob = service.submitTask(id, mockTask, args, env, outputJson)

then:
idJob

and:
outputJson.text.indexOf(" Job {") != -1
}


}

0 comments on commit f692563

Please sign in to comment.