Skip to content

Commit

Permalink
refactor the package organization to create a separate package for mo…
Browse files Browse the repository at this point in the history
…dels
  • Loading branch information
abhi18av committed Jul 15, 2024
1 parent 3e30508 commit 9d5734f
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 88 deletions.
34 changes: 19 additions & 15 deletions plugins/nf-nomad/src/main/nextflow/nomad/config/NomadJobOpts.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ package nextflow.nomad.config

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import nextflow.nomad.models.JobAffinity
import nextflow.nomad.models.JobConstraint
import nextflow.nomad.models.JobConstraints
import nextflow.nomad.models.JobVolume


/**
Expand All @@ -37,11 +41,11 @@ class NomadJobOpts{
String region
String namespace
String dockerVolume
VolumeSpec[] volumeSpec
AffinitySpec affinitySpec
ConstraintSpec constraintSpec
JobVolume[] volumeSpec
JobAffinity affinitySpec
JobConstraint constraintSpec

ConstraintsSpec constraintsSpec
JobConstraints constraintsSpec

NomadJobOpts(Map nomadJobOpts, Map<String,String> env=null){
assert nomadJobOpts!=null
Expand Down Expand Up @@ -74,10 +78,10 @@ class NomadJobOpts{
this.constraintsSpec = parseConstraints(nomadJobOpts)
}

VolumeSpec[] parseVolumes(Map nomadJobOpts){
List<VolumeSpec> ret = []
JobVolume[] parseVolumes(Map nomadJobOpts){
List<JobVolume> ret = []
if( nomadJobOpts.volume && nomadJobOpts.volume instanceof Closure){
def volumeSpec = new VolumeSpec()
def volumeSpec = new JobVolume()
def closure = (nomadJobOpts.volume as Closure)
def clone = closure.rehydrate(volumeSpec, closure.owner, closure.thisObject)
clone.resolveStrategy = Closure.DELEGATE_FIRST
Expand All @@ -89,7 +93,7 @@ class NomadJobOpts{
if( nomadJobOpts.volumes && nomadJobOpts.volumes instanceof List){
nomadJobOpts.volumes.each{ closure ->
if( closure instanceof Closure){
def volumeSpec = new VolumeSpec()
def volumeSpec = new JobVolume()
def clone = closure.rehydrate(volumeSpec, closure.owner, closure.thisObject)
clone.resolveStrategy = Closure.DELEGATE_FIRST
clone()
Expand All @@ -108,13 +112,13 @@ class NomadJobOpts{
throw new IllegalArgumentException("No more than a workdir volume allowed")
}

return ret as VolumeSpec[]
return ret as JobVolume[]
}

AffinitySpec parseAffinity(Map nomadJobOpts) {
JobAffinity parseAffinity(Map nomadJobOpts) {
if (nomadJobOpts.affinity && nomadJobOpts.affinity instanceof Closure) {
log.info "affinity config will be deprecated, use affinities closure instead"
def affinitySpec = new AffinitySpec()
def affinitySpec = new JobAffinity()
def closure = (nomadJobOpts.affinity as Closure)
def clone = closure.rehydrate(affinitySpec, closure.owner, closure.thisObject)
clone.resolveStrategy = Closure.DELEGATE_FIRST
Expand All @@ -126,10 +130,10 @@ class NomadJobOpts{
}
}

ConstraintSpec parseConstraint(Map nomadJobOpts){
JobConstraint parseConstraint(Map nomadJobOpts){
if (nomadJobOpts.constraint && nomadJobOpts.constraint instanceof Closure) {
log.info "constraint config will be deprecated, use constraints closure instead"
def constraintSpec = new ConstraintSpec()
def constraintSpec = new JobConstraint()
def closure = (nomadJobOpts.constraint as Closure)
def clone = closure.rehydrate(constraintSpec, closure.owner, closure.thisObject)
clone.resolveStrategy = Closure.DELEGATE_FIRST
Expand All @@ -141,9 +145,9 @@ class NomadJobOpts{
}
}

ConstraintsSpec parseConstraints(Map nomadJobOpts){
JobConstraints parseConstraints(Map nomadJobOpts){
if (nomadJobOpts.constraints && nomadJobOpts.constraints instanceof Closure) {
def constraintsSpec = new ConstraintsSpec()
def constraintsSpec = new JobConstraints()
def closure = (nomadJobOpts.constraints as Closure)
def clone = closure.rehydrate(constraintsSpec, closure.owner, closure.thisObject)
clone.resolveStrategy = Closure.DELEGATE_FIRST
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package nextflow.nomad.executor

import io.nomadproject.client.model.Constraint
import nextflow.nomad.config.ConstraintAttrSpec
import nextflow.nomad.config.ConstraintNodeSpec
import nextflow.nomad.config.ConstraintsSpec
import nextflow.nomad.models.JobConstraintsAttr
import nextflow.nomad.models.JobConstraintsNode
import nextflow.nomad.models.JobConstraints

class ConstraintsBuilder {

protected static List<Constraint> constraintsSpecToList(ConstraintsSpec spec){
protected static List<Constraint> constraintsSpecToList(JobConstraints spec){
def constraints = [] as List<Constraint>
if( spec?.nodeSpecs ){
def nodes = spec.nodeSpecs
Expand All @@ -24,7 +24,7 @@ class ConstraintsBuilder {
return constraints
}

protected static List<Constraint> nodeConstraints(ConstraintNodeSpec nodeSpec){
protected static List<Constraint> nodeConstraints(JobConstraintsNode nodeSpec){
def ret = [] as List<Constraint>
if( nodeSpec.id ){
ret.add new Constraint()
Expand Down Expand Up @@ -65,7 +65,7 @@ class ConstraintsBuilder {
ret
}

protected static List<Constraint> attrConstraints(ConstraintAttrSpec nodeSpec) {
protected static List<Constraint> attrConstraints(JobConstraintsAttr nodeSpec) {
def ret = [] as List<Constraint>
if (nodeSpec.arch) {
ret.add new Constraint()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import groovy.util.logging.Slf4j
import io.nomadproject.client.ApiClient
import io.nomadproject.client.api.JobsApi
import io.nomadproject.client.model.*
import nextflow.nomad.config.ConstraintsSpec
import nextflow.nomad.models.JobConstraints
import nextflow.nomad.config.NomadConfig
import nextflow.nomad.config.VolumeSpec
import nextflow.nomad.models.JobVolume
import nextflow.processor.TaskRun
import nextflow.util.MemoryUnit
import nextflow.exception.ProcessSubmitException
Expand Down Expand Up @@ -136,7 +136,7 @@ class NomadService implements Closeable{
if( config.jobOpts().volumeSpec ) {
taskGroup.volumes = [:]
config.jobOpts().volumeSpec.eachWithIndex { volumeSpec , idx->
if (volumeSpec && volumeSpec.type == VolumeSpec.VOLUME_CSI_TYPE) {
if (volumeSpec && volumeSpec.type == JobVolume.VOLUME_CSI_TYPE) {
taskGroup.volumes["vol_${idx}".toString()] = new VolumeRequest(
type: volumeSpec.type,
source: volumeSpec.name,
Expand All @@ -146,7 +146,7 @@ class NomadService implements Closeable{
)
}

if (volumeSpec && volumeSpec.type == VolumeSpec.VOLUME_HOST_TYPE) {
if (volumeSpec && volumeSpec.type == JobVolume.VOLUME_HOST_TYPE) {
taskGroup.volumes["vol_${idx}".toString()] = new VolumeRequest(
type: volumeSpec.type,
source: volumeSpec.name,
Expand Down Expand Up @@ -183,7 +183,7 @@ class NomadService implements Closeable{

volumes(task, taskDef, workingDir)
affinity(task, taskDef)
constrain(task, taskDef)
constraint(task, taskDef)
constraints(task, taskDef)

return taskDef
Expand Down Expand Up @@ -235,7 +235,7 @@ class NomadService implements Closeable{
taskDef
}

protected Task constrain(TaskRun task, Task taskDef){
protected Task constraint(TaskRun task, Task taskDef){
if( config.jobOpts().constraintSpec ){
def constraint = new Constraint()
if(config.jobOpts().constraintSpec.attribute){
Expand Down Expand Up @@ -264,7 +264,7 @@ class NomadService implements Closeable{
if( task.processor?.config?.get(TaskDirectives.CONSTRAINTS) &&
task.processor?.config?.get(TaskDirectives.CONSTRAINTS) instanceof Closure) {
Closure closure = task.processor?.config?.get(TaskDirectives.CONSTRAINTS) as Closure
ConstraintsSpec constraintsSpec = ConstraintsSpec.parse(closure)
JobConstraints constraintsSpec = JobConstraints.parse(closure)
def list = ConstraintsBuilder.constraintsSpecToList(constraintsSpec)
constraints.addAll(list)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
* limitations under the License.
*/

package nextflow.nomad.config
package nextflow.nomad.models
/**
* Nomad Job Affinity Spec
*
* @author Jorge Aguilera <[email protected]>
*/
class AffinitySpec{
class JobAffinity {

private String attribute
private String operator
Expand All @@ -44,22 +44,22 @@ class AffinitySpec{
return weight
}

AffinitySpec attribute(String attribute){
JobAffinity attribute(String attribute){
this.attribute=attribute
this
}

AffinitySpec operator(String operator){
JobAffinity operator(String operator){
this.operator = operator
this
}

AffinitySpec value(String value){
JobAffinity value(String value){
this.value = value
this
}

AffinitySpec weight(int weight){
JobAffinity weight(int weight){
this.weight = weight
this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
* limitations under the License.
*/

package nextflow.nomad.config
package nextflow.nomad.models
/**
* Nomad Job Constraint Spec
*
* @author Jorge Aguilera <[email protected]>
*/

class ConstraintSpec {
class JobConstraint {

private String attribute
private String operator
Expand All @@ -40,17 +40,17 @@ class ConstraintSpec {
return value
}

ConstraintSpec attribute(String attribute){
JobConstraint attribute(String attribute){
this.attribute=attribute
this
}

ConstraintSpec operator(String operator){
JobConstraint operator(String operator){
this.operator = operator
this
}

ConstraintSpec value(String value){
JobConstraint value(String value){
this.value = value
this
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package nextflow.nomad.config
package nextflow.nomad.models

class ConstraintsSpec {
class JobConstraints {

List<ConstraintNodeSpec> nodeSpecs = []
List<ConstraintAttrSpec> attrSpecs = []
List<JobConstraintsNode> nodeSpecs = []
List<JobConstraintsAttr> attrSpecs = []

ConstraintsSpec node( @DelegatesTo(ConstraintNodeSpec)Closure closure){
ConstraintNodeSpec constraintSpec = new ConstraintNodeSpec()
JobConstraints node(@DelegatesTo(JobConstraintsNode)Closure closure){
JobConstraintsNode constraintSpec = new JobConstraintsNode()
def clone = closure.rehydrate(constraintSpec, closure.owner, closure.thisObject)
clone.resolveStrategy = Closure.DELEGATE_FIRST
clone()
nodeSpecs << constraintSpec
this
}

ConstraintsSpec attr( @DelegatesTo(ConstraintAttrSpec)Closure closure){
ConstraintAttrSpec constraintSpec = new ConstraintAttrSpec()
JobConstraints attr(@DelegatesTo(JobConstraintsAttr)Closure closure){
JobConstraintsAttr constraintSpec = new JobConstraintsAttr()
def clone = closure.rehydrate(constraintSpec, closure.owner, closure.thisObject)
clone.resolveStrategy = Closure.DELEGATE_FIRST
clone()
Expand All @@ -27,8 +27,8 @@ class ConstraintsSpec {

}

static ConstraintsSpec parse(@DelegatesTo(ConstraintsSpec)Closure closure){
ConstraintsSpec constraintsSpec = new ConstraintsSpec()
static JobConstraints parse(@DelegatesTo(JobConstraints)Closure closure){
JobConstraints constraintsSpec = new JobConstraints()
def clone = closure.rehydrate(constraintsSpec, closure.owner, closure.thisObject)
clone.resolveStrategy = Closure.DELEGATE_FIRST
clone()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package nextflow.nomad.config
package nextflow.nomad.models

class ConstraintAttrSpec {
class JobConstraintsAttr {

private String arch = null
private Integer numcores= null
Expand All @@ -23,11 +23,11 @@ class ConstraintAttrSpec {
return totalcompute
}

ConstraintAttrSpec setCpu(Map map){
JobConstraintsAttr setCpu(Map map){
cpu(map)
}

ConstraintAttrSpec cpu(Map map){
JobConstraintsAttr cpu(Map map){
this.arch = map.containsKey("arch") ? map["arch"].toString() : null
this.numcores = map.containsKey("numcores") ? map["numcores"] as int : null
this.reservablecores = map.containsKey("reservablecores") ? map["reservablecores"] as int : null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package nextflow.nomad.config
package nextflow.nomad.models

class ConstraintNodeSpec {
class JobConstraintsNode {

private String id = null
private String name = null
Expand Down Expand Up @@ -33,48 +33,48 @@ class ConstraintNodeSpec {
return region
}

ConstraintNodeSpec setUnique(Map map){
JobConstraintsNode setUnique(Map map){
unique(map)
}

ConstraintNodeSpec unique(Map map){
JobConstraintsNode unique(Map map){
this.id = map.containsKey("id") ? map["id"].toString() : null
this.name = map.containsKey("name") ? map["name"].toString() : null
this
}

ConstraintNodeSpec setClientClass(Object map){
JobConstraintsNode setClientClass(Object map){
clientClass(map)
}

ConstraintNodeSpec clientClass(Object clientClass){
JobConstraintsNode clientClass(Object clientClass){
this.clientClass = clientClass.toString()
this
}

ConstraintNodeSpec setPool(Object map){
JobConstraintsNode setPool(Object map){
pool(map)
}

ConstraintNodeSpec pool(Object pool){
JobConstraintsNode pool(Object pool){
this.pool = pool.toString()
this
}

ConstraintNodeSpec setDataCenter(Object map){
JobConstraintsNode setDataCenter(Object map){
dataCenter(map)
}

ConstraintNodeSpec dataCenter(Object dataCenter){
JobConstraintsNode dataCenter(Object dataCenter){
this.dataCenter = dataCenter.toString()
this
}

ConstraintNodeSpec setRegion(Object map){
JobConstraintsNode setRegion(Object map){
region(map)
}

ConstraintNodeSpec region(Object region){
JobConstraintsNode region(Object region){
this.region = region.toString()
this
}
Expand Down
Loading

0 comments on commit 9d5734f

Please sign in to comment.