|
| 1 | +/* |
| 2 | + * Copyright 2026 NAVER Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.navercorp.pinpoint.batch.config; |
| 18 | + |
| 19 | +import com.navercorp.pinpoint.batch.common.BatchProperties; |
| 20 | +import com.navercorp.pinpoint.batch.job.AgentIdCleanupTasklet; |
| 21 | +import com.navercorp.pinpoint.batch.job.ApplicationCleanupTasklet; |
| 22 | +import com.navercorp.pinpoint.web.applicationmap.dao.MapAgentResponseDao; |
| 23 | +import com.navercorp.pinpoint.web.dao.AgentIdDao; |
| 24 | +import com.navercorp.pinpoint.web.dao.ApplicationDao; |
| 25 | +import org.springframework.batch.core.Job; |
| 26 | +import org.springframework.batch.core.Step; |
| 27 | +import org.springframework.batch.core.StepExecutionListener; |
| 28 | +import org.springframework.batch.core.configuration.annotation.StepScope; |
| 29 | +import org.springframework.batch.core.job.builder.JobBuilder; |
| 30 | +import org.springframework.batch.core.repository.JobRepository; |
| 31 | +import org.springframework.batch.core.step.builder.StepBuilder; |
| 32 | +import org.springframework.batch.core.step.builder.TaskletStepBuilder; |
| 33 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 34 | +import org.springframework.beans.factory.annotation.Value; |
| 35 | +import org.springframework.context.annotation.Bean; |
| 36 | +import org.springframework.context.annotation.Configuration; |
| 37 | +import org.springframework.transaction.PlatformTransactionManager; |
| 38 | + |
| 39 | +import java.util.List; |
| 40 | +import java.util.Objects; |
| 41 | +import java.util.Optional; |
| 42 | +import java.util.Set; |
| 43 | + |
| 44 | +@Configuration(proxyBeanMethods = false) |
| 45 | +public class CleanupAgentAndApplicationJobConfig { |
| 46 | + public static final String JOB_NAME = "cleanupAgentAndApplicationJob"; |
| 47 | + |
| 48 | + private final JobRepository jobRepository; |
| 49 | + private final PlatformTransactionManager transactionManager; |
| 50 | + |
| 51 | + private final BatchProperties batchProperties; |
| 52 | + |
| 53 | + public CleanupAgentAndApplicationJobConfig(JobRepository jobRepository, PlatformTransactionManager transactionManager, |
| 54 | + BatchProperties batchProperties) { |
| 55 | + this.jobRepository = Objects.requireNonNull(jobRepository, "jobRepository"); |
| 56 | + this.transactionManager = Objects.requireNonNull(transactionManager, "transactionManager"); |
| 57 | + this.batchProperties = Objects.requireNonNull(batchProperties, "batchProperties"); |
| 58 | + } |
| 59 | + |
| 60 | + @Bean |
| 61 | + public Job agentIdCleanupJob( |
| 62 | + Step agentIdCleanupStep, |
| 63 | + Step applicationCleanupStep |
| 64 | + ) { |
| 65 | + return new JobBuilder(JOB_NAME, jobRepository) |
| 66 | + .start(agentIdCleanupStep) |
| 67 | + .next(applicationCleanupStep) |
| 68 | + .build(); |
| 69 | + } |
| 70 | + |
| 71 | + @Bean |
| 72 | + public Step agentIdCleanupStep( |
| 73 | + AgentIdCleanupTasklet agentIdCleanupTasklet, |
| 74 | + @Qualifier("cleanupStepCustomListener") Optional<StepExecutionListener> stepExecutionListeners |
| 75 | + ) { |
| 76 | + TaskletStepBuilder builder = new StepBuilder("agentIdCleanupStep", jobRepository) |
| 77 | + .tasklet(agentIdCleanupTasklet, transactionManager); |
| 78 | + stepExecutionListeners.ifPresent(builder::listener); |
| 79 | + |
| 80 | + return builder.build(); |
| 81 | + } |
| 82 | + |
| 83 | + @Bean |
| 84 | + public Step applicationCleanupStep( |
| 85 | + ApplicationCleanupTasklet applicationCleanupTasklet, |
| 86 | + @Qualifier("cleanupStepCustomListener") Optional<StepExecutionListener> stepExecutionListeners |
| 87 | + ) { |
| 88 | + TaskletStepBuilder builder = new StepBuilder("applicationCleanupStep", jobRepository) |
| 89 | + .tasklet(applicationCleanupTasklet, transactionManager); |
| 90 | + stepExecutionListeners.ifPresent(builder::listener); |
| 91 | + |
| 92 | + return builder.build(); |
| 93 | + } |
| 94 | + |
| 95 | + @Bean |
| 96 | + @StepScope |
| 97 | + public AgentIdCleanupTasklet agentIdCleanupTasklet( |
| 98 | + @Value("#{jobParameters['dryRun']}") String dryRunParameter, |
| 99 | + @Value("${job.cleanup.inactive.agent.fetch-size:1000}") int fetchSize, |
| 100 | + @Value("${job.cleanup.inactive.agent.max-iteration:100000}") int maxIteration, |
| 101 | + @Value("${job.cleanup.inactive.agent.trace-based-check-service-type-codes:1220,1400,1700}") Set<Integer> statisticsCheckServiceTypeCodes, |
| 102 | + AgentIdDao agentIdDao, |
| 103 | + MapAgentResponseDao mapAgentResponseDao |
| 104 | + ) { |
| 105 | + boolean dryRun = dryRunParameter == null || Boolean.parseBoolean(dryRunParameter); |
| 106 | + return new AgentIdCleanupTasklet( |
| 107 | + agentIdDao, |
| 108 | + mapAgentResponseDao, |
| 109 | + dryRun, |
| 110 | + Math.max(batchProperties.getCleanupAgentInactiveThresholdDays(), batchProperties.getCleanupAgentAndApplicationGraceDays()), |
| 111 | + fetchSize, |
| 112 | + maxIteration, |
| 113 | + statisticsCheckServiceTypeCodes |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + @Bean |
| 118 | + @StepScope |
| 119 | + public ApplicationCleanupTasklet applicationCleanupTasklet( |
| 120 | + @Value("#{jobParameters['dryRun']}") String dryRunParameter, |
| 121 | + @Value("${job.cleanup.inactive.application.serviceUidList:0}") List<Integer> serviceUidList, |
| 122 | + @Value("${job.cleanup.inactive.application.agent-count-threshold:2147483647}") int agentCountThreshold, |
| 123 | + @Value("${job.cleanup.inactive.agent.trace-based-check-service-type-codes:1220,1400,1700}") Set<Integer> statisticsCheckServiceTypeCodes, |
| 124 | + ApplicationDao applicationDao, |
| 125 | + AgentIdDao agentIdDao, |
| 126 | + MapAgentResponseDao mapAgentResponseDao |
| 127 | + ) { |
| 128 | + boolean dryRun = dryRunParameter == null || Boolean.parseBoolean(dryRunParameter); |
| 129 | + return new ApplicationCleanupTasklet( |
| 130 | + applicationDao, |
| 131 | + agentIdDao, |
| 132 | + mapAgentResponseDao, |
| 133 | + dryRun, |
| 134 | + serviceUidList, |
| 135 | + agentCountThreshold, |
| 136 | + batchProperties.getCleanupAgentInactiveThresholdDays(), |
| 137 | + batchProperties.getCleanupAgentAndApplicationGraceDays(), |
| 138 | + statisticsCheckServiceTypeCodes |
| 139 | + ); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | + |
0 commit comments