Skip to content

Commit

Permalink
refactor NomadService to rely on Builder classes
Browse files Browse the repository at this point in the history
Signed-off-by: Abhinav Sharma <[email protected]>
  • Loading branch information
abhi18av committed Sep 16, 2024
1 parent e8f28ab commit 0157d7d
Show file tree
Hide file tree
Showing 15 changed files with 517 additions and 206 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2023-, Stellenbosch University, South Africa
* Copyright 2024-, Evaluacion y Desarrollo de Negocios, Spain
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package nextflow.nomad.builders

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import io.nomadproject.client.model.Job
import io.nomadproject.client.model.TaskGroup

/**
*
* @author Abhinav Sharma <[email protected]>
*/

@Slf4j
@CompileStatic
class JobBuilder {
private Job job = new Job()

JobBuilder id(String id) {
job.ID = id
return this
}

JobBuilder name(String name) {
job.name = name
return this
}

JobBuilder type(String type) {
job.type = type
return this
}

JobBuilder datacenters(List<String> datacenters) {
job.datacenters = datacenters
return this
}

JobBuilder namespace(String namespace) {
job.namespace = namespace
return this
}

JobBuilder taskGroups(List<TaskGroup> taskGroups) {
job.taskGroups = taskGroups
return this
}

Job build() {
return job
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
package nextflow.nomad.models
/*
* Copyright 2023-, Stellenbosch University, South Africa
* Copyright 2024-, Evaluacion y Desarrollo de Negocios, Spain
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nextflow.nomad.builders

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import io.nomadproject.client.model.Constraint
import nextflow.nomad.models.JobConstraintsAttr
import nextflow.nomad.models.JobConstraintsNode
import nextflow.nomad.models.JobConstraints
/**
*
* @author Abhinav Sharma <[email protected]>
**/

class ConstraintsBuilder {
@Slf4j
@CompileStatic
class JobConstraintsBuilder {

static List<Constraint> constraintsSpecToList(JobConstraints spec){
def constraints = [] as List<Constraint>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2023-, Stellenbosch University, South Africa
* Copyright 2024-, Evaluacion y Desarrollo de Negocios, Spain
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nextflow.nomad.builders

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import io.nomadproject.client.model.Spread
import io.nomadproject.client.model.SpreadTarget
import nextflow.nomad.models.JobSpreads

/**
* Nomad Job Spread Spec Builder
*
* @author Jorge Aguilera <[email protected]>
*/

@Slf4j
@CompileStatic
class JobSpreadsBuilder {

static List<Spread> spreadsSpecToList(JobSpreads spreads){
def ret = [] as List<Spread>

spreads.raws.each{raw->
def targets = [] as List<SpreadTarget>
raw.right.each {
targets.add( new SpreadTarget(value: it.left, percent: it.right) )
}
ret.add new Spread(attribute: raw.left, weight: raw.middle, spreadTarget: targets)
}

return ret
}


static JobSpreads parseJobSpreads(Map nomadJobOpts){
if( nomadJobOpts.spreads && nomadJobOpts.spreads instanceof Closure){
def spec = new JobSpreads()
def closure = (nomadJobOpts.spreads as Closure)
def clone = closure.rehydrate(spec, closure.owner, closure.thisObject)
clone.resolveStrategy = Closure.DELEGATE_FIRST
clone()
spec.validate()
spec
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2023-, Stellenbosch University, South Africa
* Copyright 2024-, Evaluacion y Desarrollo de Negocios, Spain
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nextflow.nomad.builders

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import io.nomadproject.client.model.Task
import io.nomadproject.client.model.Resources

@Slf4j
@CompileStatic
class TaskBuilder {
private Task task = new Task()

TaskBuilder name(String name) {
task.name = name
return this
}

TaskBuilder driver(String driver) {
task.driver = driver
return this
}

TaskBuilder resources(Resources resources) {
task.resources = resources
return this
}

TaskBuilder config(Map<String, Object> config) {
task.config = config
return this
}

TaskBuilder env(Map<String, String> env) {
task.env = env
return this
}

Task build() {
return task
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2023-, Stellenbosch University, South Africa
* Copyright 2024-, Evaluacion y Desarrollo de Negocios, Spain
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package nextflow.nomad.builders

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import io.nomadproject.client.model.TaskGroup
import io.nomadproject.client.model.ReschedulePolicy
import io.nomadproject.client.model.RestartPolicy
import io.nomadproject.client.model.Task

@Slf4j
@CompileStatic
class TaskGroupBuilder {
private TaskGroup taskGroup = new TaskGroup()

TaskGroupBuilder name(String name) {
taskGroup.name = name
return this
}

TaskGroupBuilder tasks(List<Task> tasks) {
taskGroup.tasks = tasks
return this
}

TaskGroupBuilder reschedulePolicy(ReschedulePolicy policy) {
taskGroup.reschedulePolicy = policy
return this
}

TaskGroupBuilder restartPolicy(RestartPolicy policy) {
taskGroup.restartPolicy = policy
return this
}

TaskGroup build() {
return taskGroup
}
}
Loading

0 comments on commit 0157d7d

Please sign in to comment.