Skip to content

Commit

Permalink
feat(tasks): Allow max polling of task status to be set (#1546)
Browse files Browse the repository at this point in the history
* feat(tasks): Allow max polling of task status to be set

* fix(typo): Fix typo on service

* feat(service): Switch to config properties and make time between polls also configurable

* fix(spring): Fixed camel case prefix on config properties

* fix(properties): Move properties to a new config file

* fix(duplicate: Remove extraaneous component annotation

* chore(cleanup): remove blank space
  • Loading branch information
jasonmcintosh committed Apr 21, 2022
1 parent decfc35 commit 1b86cb8
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 18 deletions.
@@ -0,0 +1,29 @@
/*
* Copyright 2022 Netflix, Inc.
*
* 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 com.netflix.spinnaker.gate.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties("task-service")
@Data
public class TaskServiceProperties {
private int maxNumberOfPolls = 32;
private int defaultIntervalBetweenPolls = 1000;
}
Expand Up @@ -16,27 +16,35 @@

package com.netflix.spinnaker.gate.services;

import com.netflix.spinnaker.gate.config.TaskServiceProperties;
import com.netflix.spinnaker.gate.services.internal.ClouddriverServiceSelector;
import com.netflix.spinnaker.gate.services.internal.OrcaServiceSelector;
import com.netflix.spinnaker.security.AuthenticatedRequest;
import java.util.*;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Data
public class TaskService {

private final Logger log = LoggerFactory.getLogger(getClass());

private OrcaServiceSelector orcaServiceSelector;
private ClouddriverServiceSelector clouddriverServiceSelector;
private TaskServiceProperties taskServiceProperties;

@Autowired
public TaskService(
OrcaServiceSelector orcaServiceSelector,
ClouddriverServiceSelector clouddriverServiceSelector) {
ClouddriverServiceSelector clouddriverServiceSelector,
TaskServiceProperties taskServiceProperties) {
this.orcaServiceSelector = orcaServiceSelector;
this.clouddriverServiceSelector = clouddriverServiceSelector;
this.taskServiceProperties = taskServiceProperties;
}

public Map create(Map body) {
Expand Down Expand Up @@ -124,7 +132,10 @@ public Map createAndWaitForCompletion(Map body, int maxPolls) {
}

public Map createAndWaitForCompletion(Map body) {
return createAndWaitForCompletion(body, 32, 1000);
return createAndWaitForCompletion(
body,
taskServiceProperties.getMaxNumberOfPolls(),
taskServiceProperties.getDefaultIntervalBetweenPolls());
}

/** @deprecated This pipeline operation does not belong here. */
Expand All @@ -149,20 +160,4 @@ public void setApplicationForTask(String id) {
log.error("Error loading execution {} from orca", id, e);
}
}

public OrcaServiceSelector getOrcaServiceSelector() {
return orcaServiceSelector;
}

public void setOrcaServiceSelector(OrcaServiceSelector orcaServiceSelector) {
this.orcaServiceSelector = orcaServiceSelector;
}

public ClouddriverServiceSelector getClouddriverServiceSelector() {
return clouddriverServiceSelector;
}

public void setClouddriverServiceSelector(ClouddriverServiceSelector clouddriverServiceSelector) {
this.clouddriverServiceSelector = clouddriverServiceSelector;
}
}
@@ -0,0 +1,53 @@
/*
* Copyright 2022 Netflix, Inc.
*
* 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 com.netflix.spinnaker.gate.services;

import static org.mockito.Mockito.*;

import com.netflix.spinnaker.gate.config.TaskServiceProperties;
import com.netflix.spinnaker.gate.services.internal.ClouddriverServiceSelector;
import com.netflix.spinnaker.gate.services.internal.OrcaService;
import com.netflix.spinnaker.gate.services.internal.OrcaServiceSelector;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {TaskService.class, TaskServiceProperties.class})
public class TaskServiceTest {

@MockBean private OrcaServiceSelector selector;
@MockBean private ClouddriverServiceSelector clouddriverServiceSelector;
@MockBean private OrcaService orcaService;

@Autowired TaskService taskService;

@Test
public void callAsManyTimesAsSet() {
Map operation = new LinkedHashMap();

Map task = Map.of("ref", "apps/bob/someRandomId");
when(selector.select()).thenReturn(orcaService);
when(orcaService.doOperation(operation)).thenReturn(task);
taskService.createAndWaitForCompletion(operation, 32, 1);
verify(orcaService, times(32)).getTask("someRandomId");
}
}

0 comments on commit 1b86cb8

Please sign in to comment.