瀏覽代碼

巡检删除任务并发处理

pudongliang 3 月之前
父節點
當前提交
90af2d2b0a

+ 45 - 2
src/main/java/com/moka/gdtauto/task/AdAutoTask.java

@@ -2,6 +2,10 @@ package com.moka.gdtauto.task;
 
 import java.time.LocalDateTime;
 import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Semaphore;
 
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
@@ -48,12 +52,51 @@ public class AdAutoTask {
     @SchedulerLock(name = "ad-auto-delete-task", lockAtMostFor = "9m", lockAtLeastFor = "2m")
     public void adAutoDeleteTask() {
         log.info("AdAutoDeleteTask execute");
-        //3天前
+        // 3天前
         LocalDateTime threeDaysAgo = LocalDateTime.now().minusDays(3);
         List<AdPlanConf> confList = 
             adPlanConfService.lambdaQuery()
             .lt(AdPlanConf::getCreateTime, threeDaysAgo)
+            .orderByDesc(AdPlanConf::getId)
             .list();
-        confList.forEach(conf -> adPlanConfService.gdtAdgroupPatrolDelete(conf));
+        
+        if (confList.isEmpty()) {
+            log.info("没有需要删除的广告计划配置");
+            return;
+        }
+        
+        // 使用虚拟线程并发执行,Semaphore限制最大并发数为10
+        Semaphore semaphore = new Semaphore(10);
+        CountDownLatch latch = new CountDownLatch(confList.size());
+        ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
+        
+        for (AdPlanConf conf : confList) {
+            executor.submit(() -> {
+                try {
+                    semaphore.acquire();
+                    adPlanConfService.gdtAdgroupPatrolDelete(conf);
+                } catch (InterruptedException e) {
+                    log.error("获取信号量被中断,confId={}", conf.getId(), e);
+                    Thread.currentThread().interrupt();
+                } catch (Exception e) {
+                    log.error("删除广告计划配置失败,confId={}", conf.getId(), e);
+                } finally {
+                    semaphore.release();
+                    latch.countDown();
+                }
+            });
+        }
+        
+        // 等待所有任务完成
+        try {
+            latch.await();
+        } catch (InterruptedException e) {
+            log.error("等待虚拟线程任务完成被中断", e);
+            Thread.currentThread().interrupt();
+        } finally {
+            executor.shutdown();
+        }
+        
+        log.info("AdAutoDeleteTask 完成,共处理 {} 个配置", confList.size());
     }
 }

+ 14 - 0
src/test/java/com/moka/gdtauto/service/AdAutoDeleteTest.java

@@ -1,5 +1,6 @@
 package com.moka.gdtauto.service;
 
+import java.time.LocalDateTime;
 import java.util.List;
 
 import org.junit.jupiter.api.Test;
@@ -24,4 +25,17 @@ public class AdAutoDeleteTest {
             adPlanConfService.gdtAdgroupPatrolDelete(adPlanConf);
         }
     }
+
+
+    @Test
+    public void testPatrolDelete() {
+        //3天前
+        LocalDateTime threeDaysAgo = LocalDateTime.now().minusDays(3);
+        List<AdPlanConf> confList = 
+            adPlanConfService.lambdaQuery()
+            .lt(AdPlanConf::getCreateTime, threeDaysAgo)
+            .orderByDesc(AdPlanConf::getId)
+            .list();
+        confList.forEach(conf -> adPlanConfService.gdtAdgroupPatrolDelete(conf));
+    }
 }

+ 1 - 2
src/test/java/com/moka/gdtauto/service/AdAutoTest.java

@@ -1,7 +1,6 @@
 package com.moka.gdtauto.service;
 
 import java.util.List;
-import java.util.Optional;
 
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -13,7 +12,6 @@ import com.moka.gdtauto.GdtAutoApplication;
 import com.moka.gdtauto.common.AutoPlanRule;
 import com.moka.gdtauto.entity.AdAutoConf;
 import com.moka.gdtauto.entity.AdPlan;
-import com.tencent.ads.anno.AuthInfoAppend;
 
 @SpringBootTest(classes = GdtAutoApplication.class)
 @ActiveProfiles("test")
@@ -46,4 +44,5 @@ public class AdAutoTest {
     }
 
 
+
 }