LCOV - code coverage report
Current view: top level - server/change - ChangeCleanupRunner.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 17 29 58.6 %
Date: 2022-11-19 15:00:39 Functions: 7 11 63.6 %

          Line data    Source code
       1             : // Copyright (C) 2015 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.change;
      16             : 
      17             : import com.google.common.flogger.FluentLogger;
      18             : import com.google.gerrit.extensions.events.LifecycleListener;
      19             : import com.google.gerrit.extensions.restapi.RestApiException;
      20             : import com.google.gerrit.lifecycle.LifecycleModule;
      21             : import com.google.gerrit.server.config.ChangeCleanupConfig;
      22             : import com.google.gerrit.server.git.WorkQueue;
      23             : import com.google.gerrit.server.update.RetryHelper;
      24             : import com.google.gerrit.server.update.UpdateException;
      25             : import com.google.gerrit.server.util.ManualRequestContext;
      26             : import com.google.gerrit.server.util.OneOffRequestContext;
      27             : import com.google.inject.Inject;
      28             : 
      29             : /** Runnable to enable scheduling change cleanups to run periodically */
      30             : public class ChangeCleanupRunner implements Runnable {
      31         138 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      32             : 
      33         138 :   public static class ChangeCleanupRunnerModule extends LifecycleModule {
      34             :     @Override
      35             :     protected void configure() {
      36         138 :       listener().to(Lifecycle.class);
      37         138 :     }
      38             :   }
      39             : 
      40             :   static class Lifecycle implements LifecycleListener {
      41             :     private final WorkQueue queue;
      42             :     private final ChangeCleanupRunner runner;
      43             :     private final ChangeCleanupConfig cfg;
      44             : 
      45             :     @Inject
      46         138 :     Lifecycle(WorkQueue queue, ChangeCleanupRunner runner, ChangeCleanupConfig cfg) {
      47         138 :       this.queue = queue;
      48         138 :       this.runner = runner;
      49         138 :       this.cfg = cfg;
      50         138 :     }
      51             : 
      52             :     @Override
      53             :     public void start() {
      54         138 :       cfg.getSchedule().ifPresent(s -> queue.scheduleAtFixedRate(runner, s));
      55         138 :     }
      56             : 
      57             :     @Override
      58             :     public void stop() {
      59             :       // handled by WorkQueue.stop() already
      60         138 :     }
      61             :   }
      62             : 
      63             :   private final OneOffRequestContext oneOffRequestContext;
      64             :   private final AbandonUtil abandonUtil;
      65             :   private final RetryHelper retryHelper;
      66             : 
      67             :   @Inject
      68             :   ChangeCleanupRunner(
      69         138 :       OneOffRequestContext oneOffRequestContext, AbandonUtil abandonUtil, RetryHelper retryHelper) {
      70         138 :     this.oneOffRequestContext = oneOffRequestContext;
      71         138 :     this.abandonUtil = abandonUtil;
      72         138 :     this.retryHelper = retryHelper;
      73         138 :   }
      74             : 
      75             :   @Override
      76             :   public void run() {
      77           0 :     logger.atInfo().log("Running change cleanups.");
      78           0 :     try (ManualRequestContext ctx = oneOffRequestContext.open()) {
      79             :       // abandonInactiveOpenChanges skips failures instead of throwing, so retrying will never
      80             :       // actually happen. For the purposes of this class that is fine: they'll get tried again the
      81             :       // next time the scheduled task is run.
      82           0 :       retryHelper
      83           0 :           .changeUpdate(
      84             :               "abandonInactiveOpenChanges",
      85             :               updateFactory -> {
      86           0 :                 abandonUtil.abandonInactiveOpenChanges(updateFactory);
      87           0 :                 return null;
      88             :               })
      89           0 :           .call();
      90           0 :     } catch (RestApiException | UpdateException e) {
      91           0 :       logger.atSevere().withCause(e).log("Failed to cleanup changes.");
      92           0 :     }
      93           0 :   }
      94             : 
      95             :   @Override
      96             :   public String toString() {
      97           0 :     return "change cleanup runner";
      98             :   }
      99             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750