Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Configration for scheduling to clean up task execution #5944

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2024 the original author or authors.
*
* 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
*
* https://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 org.springframework.cloud.dataflow.server.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.dataflow.server.service.TaskDeleteService;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Arrays;
import java.util.HashSet;

import static org.springframework.cloud.dataflow.server.config.CleanupTaskExecutionProperties.CLEANUP_TASK_EXECUTION_PROPS_PREFIX;

/**
* Configuration for task executions cleanup schedule.
* @author Ganghun Cho
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({ CleanupTaskExecutionProperties.class })
@ConditionalOnProperty(prefix = CLEANUP_TASK_EXECUTION_PROPS_PREFIX, name = "enabled", havingValue = "true")
@ConditionalOnBean({ EnableDataFlowServerConfiguration.Marker.class })
@EnableScheduling
public class CleanupTaskExecutionConfiguration {
private final TaskDeleteService taskDeleteService;

private final CleanupTaskExecutionProperties cleanupTaskExecutionProperties;

public CleanupTaskExecutionConfiguration(TaskDeleteService taskDeleteService,
CleanupTaskExecutionProperties cleanupTaskExecutionProperties) {
this.taskDeleteService = taskDeleteService;
this.cleanupTaskExecutionProperties = cleanupTaskExecutionProperties;
}

@Scheduled(cron = "${spring.cloud.dataflow.task.execution.cleanup.schedule:0 0 * * * *}")
public void getAsyncExecutor() {
this.taskDeleteService.cleanupExecutions(
new HashSet<>(Arrays.asList(cleanupTaskExecutionProperties.getAction())),
cleanupTaskExecutionProperties.getTaskName(),
cleanupTaskExecutionProperties.isCompleted(),
cleanupTaskExecutionProperties.getDays());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2024 the original author or authors.
*
* 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
*
* https://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 org.springframework.cloud.dataflow.server.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.dataflow.core.DataFlowPropertyKeys;
import org.springframework.cloud.dataflow.server.controller.support.TaskExecutionControllerDeleteAction;

/**
* @author Ganghun Cho
*/
@ConfigurationProperties(prefix = CleanupTaskExecutionProperties.CLEANUP_TASK_EXECUTION_PROPS_PREFIX)
public class CleanupTaskExecutionProperties {

public static final String CLEANUP_TASK_EXECUTION_PROPS_PREFIX = DataFlowPropertyKeys.PREFIX + "task.execution.cleanup";

private boolean enabled = false;

private TaskExecutionControllerDeleteAction[] action = new TaskExecutionControllerDeleteAction[] {
TaskExecutionControllerDeleteAction.CLEANUP };

private String taskName = "";

private boolean completed = false;

private String schedule = "0 0 * * * *";

private Integer days;

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public TaskExecutionControllerDeleteAction[] getAction() {
return action;
}

public void setAction(TaskExecutionControllerDeleteAction[] action) {
this.action = action;
}

public String getTaskName() {
return taskName;
}

public void setTaskName(String taskName) {
this.taskName = taskName;
}

public boolean isCompleted() {
return completed;
}

public void setCompleted(boolean completed) {
this.completed = completed;
}

public String getSchedule() {
return schedule;
}

public void setSchedule(String schedule) {
this.schedule = schedule;
}

public Integer getDays() {
return days;
}

public void setDays(Integer days) {
this.days = days;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
H2ServerConfiguration.class,
SchemaServiceConfiguration.class,
AggregateTaskConfiguration.class,
AggregateDataFlowTaskConfiguration.class
AggregateDataFlowTaskConfiguration.class,
CleanupTaskExecutionConfiguration.class
})
@EnableConfigurationProperties({ BatchProperties.class, CommonApplicationProperties.class })
@ComponentScan(basePackages = {"org.springframework.cloud.dataflow.schema.service", "org.springframework.cloud.dataflow.aggregate.task"})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2024 the original author or authors.
*
* 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
*
* https://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 org.springframework.cloud.dataflow.server.config;

import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.cloud.dataflow.server.config.CleanupTaskExecutionProperties.CLEANUP_TASK_EXECUTION_PROPS_PREFIX;

/**
* @author Ganghun Cho
*/
public class CleanupTaskExecutionConfigurationTests {

@Test
public void enableCleanTaskExecutionScheduleTest() {
ConfigurableApplicationContext context = applicationContext(
String.format("--%s.enabled=true", CLEANUP_TASK_EXECUTION_PROPS_PREFIX));
assertTrue(
context.containsBean("org.springframework.cloud.dataflow.server.config.CleanupTaskExecutionConfiguration"));
}

@Test
public void disableCleanTaskExecutionScheduleTest() {
ConfigurableApplicationContext context = applicationContext(
String.format("--%s.enabled=false", CLEANUP_TASK_EXECUTION_PROPS_PREFIX));
assertFalse(
context.containsBean("org.springframework.cloud.dataflow.server.config.CleanupTaskExecutionConfiguration"));
}

private ConfigurableApplicationContext applicationContext(String... args) {
String[] commonArgs = {
"--server.port=0",
"--spring.main.allow-bean-definition-overriding=true",
"--spring.autoconfigure.exclude=" +
"org.springframework.cloud.deployer.spi.cloudfoundry.CloudFoundryDeployerAutoConfiguration," +
"org.springframework.cloud.deployer.spi.kubernetes.KubernetesAutoConfiguration"
};
String[] allArgs = Stream.of(commonArgs, args)
.flatMap(Stream::of)
.toArray(String[]::new);
return SpringApplication.run(EmptyDefaultTestApplication.class, allArgs);
}
}