Skip to content

Commit

Permalink
feat(bakery): use bakerySelector for monitorBakeTask (spinnaker#3103)
Browse files Browse the repository at this point in the history
  • Loading branch information
asher authored Aug 15, 2019
1 parent cda9fe7 commit be35c24
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.netflix.spinnaker.orca.bakery.tasks
import com.netflix.spinnaker.orca.ExecutionStatus
import com.netflix.spinnaker.orca.OverridableTimeoutRetryableTask
import com.netflix.spinnaker.orca.TaskResult
import com.netflix.spinnaker.orca.bakery.BakerySelector
import com.netflix.spinnaker.orca.bakery.api.BakeStatus
import com.netflix.spinnaker.orca.bakery.api.BakeryService
import com.netflix.spinnaker.orca.pipeline.model.Stage
Expand All @@ -36,19 +37,25 @@ class MonitorBakeTask implements OverridableTimeoutRetryableTask {
long backoffPeriod = 30000
long timeout = 3600000 // 1hr

@Autowired
BakeryService bakery
@Autowired(required = false)
BakerySelector bakerySelector

@Autowired
CreateBakeTask createBakeTask

@Override
TaskResult execute(Stage stage) {
if (!bakerySelector) {
throw new UnsupportedOperationException(
"You have not enabled baking for this orca instance. Set bakery.enabled: true")
}

def region = stage.context.region as String
def previousStatus = stage.context.status as BakeStatus

try {
def newStatus = bakery.lookupStatus(region, previousStatus.id).toBlocking().single()
def bakery = bakerySelector.select(stage)
def newStatus = bakery.service.lookupStatus(region, previousStatus.id).toBlocking().single()
if (isCanceled(newStatus.state) && previousStatus.state == BakeStatus.State.PENDING) {
log.info("Original bake was 'canceled', re-baking (executionId: ${stage.execution.id}, previousStatus: ${previousStatus.state})")
def rebakeResult = createBakeTask.execute(stage)
Expand All @@ -68,7 +75,7 @@ class MonitorBakeTask implements OverridableTimeoutRetryableTask {
return [BakeStatus.State.CANCELED, BakeStatus.State.CANCELLED].contains(state)
}

private ExecutionStatus mapStatus(BakeStatus newStatus) {
private static ExecutionStatus mapStatus(BakeStatus newStatus) {
switch (newStatus.state) {
case BakeStatus.State.COMPLETED:
return newStatus.result == BakeStatus.Result.SUCCESS ? ExecutionStatus.SUCCEEDED : ExecutionStatus.TERMINAL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package com.netflix.spinnaker.orca.bakery.tasks

import com.netflix.spinnaker.kork.web.selector.v2.SelectableService
import com.netflix.spinnaker.orca.ExecutionStatus
import com.netflix.spinnaker.orca.TaskResult
import com.netflix.spinnaker.orca.bakery.BakerySelector
import com.netflix.spinnaker.orca.bakery.api.BakeStatus
import com.netflix.spinnaker.orca.bakery.api.BakeryService
import com.netflix.spinnaker.orca.pipeline.model.Stage
Expand All @@ -42,10 +44,22 @@ class MonitorBakeTaskSpec extends Specification {
given:
def previousStatus = new BakeStatus(id: id, state: BakeStatus.State.PENDING)
def stage = new Stage(pipeline, "bake", [region: "us-west-1", status: previousStatus])
def bakery = Mock(BakeryService) {
lookupStatus(stage.context.region, id) >> Observable.from(new BakeStatus(id: id, state: bakeState, result: bakeResult))
}

and:
task.bakery = Stub(BakeryService) {
lookupStatus(stage.context.region, id) >> Observable.from(new BakeStatus(id: id, state: bakeState, result: bakeResult))
def selectedBakeryService = Stub(SelectableService.SelectedService) {
getService() >> bakery
getConfig() >> [
extractBuildDetails: false,
allowMissingPackageInstallation: false,
roscoApisEnabled: true
]
}

task.bakerySelector = Mock(BakerySelector) {
select(_) >> selectedBakeryService
}

expect:
Expand All @@ -69,13 +83,26 @@ class MonitorBakeTaskSpec extends Specification {
def id = randomUUID().toString()
def previousStatus = new BakeStatus(id: id, state: BakeStatus.State.PENDING)
def stage = new Stage(pipeline, "bake", [region: "us-west-1", status: previousStatus])

and:
task.bakery = Stub(BakeryService) {
def bakery = Mock(BakeryService) {
lookupStatus(stage.context.region, id) >> Observable.from(
new BakeStatus(id: id, state: state, result: null)
)
}

and:
def selectedBakeryService = Stub(SelectableService.SelectedService) {
getService() >> bakery
getConfig() >> [
extractBuildDetails: false,
allowMissingPackageInstallation: false,
roscoApisEnabled: true
]
}

task.bakerySelector = Mock(BakerySelector) {
select(_) >> selectedBakeryService
}

task.createBakeTask = Mock(CreateBakeTask) {
1 * execute(_) >> { return TaskResult.builder(ExecutionStatus.SUCCEEDED).context([stage: 1]).outputs([global: 2]).build() }
}
Expand All @@ -96,10 +123,22 @@ class MonitorBakeTaskSpec extends Specification {
given:
def previousStatus = new BakeStatus(id: id, state: BakeStatus.State.PENDING)
def stage = new Stage(pipeline, "bake", [region: "us-west-1", status: previousStatus])
def bakery = Mock(BakeryService) {
lookupStatus(stage.context.region, id) >> Observable.from(new BakeStatus(id: id, state: BakeStatus.State.COMPLETED))
}

and:
task.bakery = Stub(BakeryService) {
lookupStatus(stage.context.region, id) >> Observable.from(new BakeStatus(id: id, state: BakeStatus.State.COMPLETED))
def selectedBakeryService = Stub(SelectableService.SelectedService) {
getService() >> bakery
getConfig() >> [
extractBuildDetails: false,
allowMissingPackageInstallation: false,
roscoApisEnabled: true
]
}

task.bakerySelector = Mock(BakerySelector) {
select(_) >> selectedBakeryService
}

when:
Expand Down

0 comments on commit be35c24

Please sign in to comment.