diff --git a/spring-cloud-dataflow-server-core/src/main/java/org/springframework/cloud/dataflow/server/config/CleanupTaskExecutionConfiguration.java b/spring-cloud-dataflow-server-core/src/main/java/org/springframework/cloud/dataflow/server/config/CleanupTaskExecutionConfiguration.java new file mode 100644 index 0000000000..9fcfd828cb --- /dev/null +++ b/spring-cloud-dataflow-server-core/src/main/java/org/springframework/cloud/dataflow/server/config/CleanupTaskExecutionConfiguration.java @@ -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()); + } + +} diff --git a/spring-cloud-dataflow-server-core/src/main/java/org/springframework/cloud/dataflow/server/config/CleanupTaskExecutionProperties.java b/spring-cloud-dataflow-server-core/src/main/java/org/springframework/cloud/dataflow/server/config/CleanupTaskExecutionProperties.java new file mode 100644 index 0000000000..43940db417 --- /dev/null +++ b/spring-cloud-dataflow-server-core/src/main/java/org/springframework/cloud/dataflow/server/config/CleanupTaskExecutionProperties.java @@ -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; + } +} diff --git a/spring-cloud-dataflow-server-core/src/main/java/org/springframework/cloud/dataflow/server/config/DataFlowServerConfiguration.java b/spring-cloud-dataflow-server-core/src/main/java/org/springframework/cloud/dataflow/server/config/DataFlowServerConfiguration.java index 96bdbbfe6b..c76443d066 100644 --- a/spring-cloud-dataflow-server-core/src/main/java/org/springframework/cloud/dataflow/server/config/DataFlowServerConfiguration.java +++ b/spring-cloud-dataflow-server-core/src/main/java/org/springframework/cloud/dataflow/server/config/DataFlowServerConfiguration.java @@ -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"}) diff --git a/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/config/CleanupTaskExecutionConfigurationTests.java b/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/config/CleanupTaskExecutionConfigurationTests.java new file mode 100644 index 0000000000..5521e9570b --- /dev/null +++ b/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/config/CleanupTaskExecutionConfigurationTests.java @@ -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); + } +}