Skip to content

Commit

Permalink
♻️ : migrate backend for job
Browse files Browse the repository at this point in the history
  • Loading branch information
cdubuisson committed Apr 9, 2020
1 parent 6b6664d commit 23495b9
Show file tree
Hide file tree
Showing 12 changed files with 456 additions and 596 deletions.
34 changes: 15 additions & 19 deletions src/main/java/io/codeka/gaia/runner/StackRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.IntConsumer;
import java.util.function.Supplier;
Expand Down Expand Up @@ -119,41 +120,36 @@ private void treatJob(JobWorkflow jobWorkflow, Consumer<JobWorkflow> jobActionFn
@Async
public void plan(JobWorkflow jobWorkflow, TerraformModule module, Stack stack) {
treatJob(
jobWorkflow,
JobWorkflow::plan,
() -> managePlanScript(jobWorkflow.getJob(), stack, module),
result -> managePlanResult(result, jobWorkflow, stack)
jobWorkflow,
JobWorkflow::plan,
() -> managePlanScript(jobWorkflow.getJob(), stack, module),
result -> managePlanResult(result, jobWorkflow, stack)
);
}

@Async
public void apply(JobWorkflow jobWorkflow, TerraformModule module, Stack stack) {
treatJob(
jobWorkflow,
JobWorkflow::apply,
() -> manageApplyScript(jobWorkflow.getJob(), stack, module),
result -> manageApplyResult(result, jobWorkflow, stack)
jobWorkflow,
JobWorkflow::apply,
() -> manageApplyScript(jobWorkflow.getJob(), stack, module),
result -> manageApplyResult(result, jobWorkflow, stack)
);
}

@Async
public void retry(JobWorkflow jobWorkflow, TerraformModule module, Stack stack) {
stepRepository.deleteByJobId(jobWorkflow.getJob().getId());
treatJob(
jobWorkflow,
JobWorkflow::retry,
() -> managePlanScript(jobWorkflow.getJob(), stack, module),
result -> managePlanResult(result, jobWorkflow, stack)
jobWorkflow,
JobWorkflow::retry,
() -> managePlanScript(jobWorkflow.getJob(), stack, module),
result -> managePlanResult(result, jobWorkflow, stack)
);
}

public Job getJob(String jobId) {
if (this.jobs.containsKey(jobId)) {
// try in memory
return this.jobs.get(jobId);
}
// or find in repository
return this.jobRepository.findById(jobId).orElseThrow(() -> new RuntimeException("job not found"));
public Optional<Job> getJob(String jobId) {
return Optional.ofNullable(this.jobs.get(jobId));
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.codeka.gaia.stacks.controller

import io.codeka.gaia.modules.repository.TerraformModuleRepository
import io.codeka.gaia.runner.StackRunner
import io.codeka.gaia.stacks.bo.Job
import io.codeka.gaia.stacks.repository.JobRepository
import io.codeka.gaia.stacks.repository.StackRepository
import io.codeka.gaia.stacks.repository.StepRepository
import io.codeka.gaia.stacks.workflow.JobWorkflow
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/api/jobs")
class JobRestController(
private val jobRepository: JobRepository,
private val stackRepository: StackRepository,
private val moduleRepository: TerraformModuleRepository,
private val stackRunner: StackRunner,
private val stepRepository: StepRepository) {

@GetMapping(params = ["stackId"])
fun jobs(@RequestParam stackId: String) = jobRepository.findAllByStackId(stackId)

@GetMapping("/{id}")
fun job(@PathVariable id: String): Job {
val runningJob = stackRunner.getJob(id);
if (runningJob.isPresent) {
return runningJob.get();
}
return jobRepository.findById(id).orElseThrow { JobNotFoundException() }
}

@PostMapping("/{id}/plan")
fun plan(@PathVariable id: String) {
val job = jobRepository.findById(id).orElseThrow { JobNotFoundException() }
val stack = stackRepository.findById(job.stackId).orElseThrow()
val module = moduleRepository.findById(stack.moduleId).orElseThrow()
stackRunner.plan(JobWorkflow(job), module, stack)
}

@PostMapping("/{id}/apply")
fun apply(@PathVariable id: String) {
val job = jobRepository.findById(id).orElseThrow { JobNotFoundException() }
val stack = stackRepository.findById(job.stackId).orElseThrow()
val module = moduleRepository.findById(stack.moduleId).orElseThrow()
stackRunner.apply(JobWorkflow(job), module, stack)
}

@PostMapping("/{id}/retry")
fun retry(@PathVariable id: String) {
val job = jobRepository.findById(id).orElseThrow { JobNotFoundException() }
val stack = stackRepository.findById(job.stackId).orElseThrow()
val module = moduleRepository.findById(stack.moduleId).orElseThrow()
stackRunner.retry(JobWorkflow(job), module, stack)
}

@DeleteMapping("/{id}")
fun delete(@PathVariable id: String) {
stepRepository.deleteByJobId(id)
jobRepository.deleteById(id)
}

}

@ResponseStatus(HttpStatus.NOT_FOUND)
internal class JobNotFoundException : RuntimeException()

This file was deleted.

Loading

0 comments on commit 23495b9

Please sign in to comment.