view mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialCleaner.java @ 991:f342d25311c1 Indore-2017.2.x

TW-50054 disable custom hg config on server by default
author Dmitry Neverov <dmitry.neverov@gmail.com>
date Mon, 29 Jan 2018 09:38:17 +0100
parents 7bf4d943d5bb
children
line wrap: on
line source
/*
 * Copyright 2000-2018 JetBrains s.r.o.
 *
 * 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
 *
 * http://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 jetbrains.buildServer.buildTriggers.vcs.mercurial;

import jetbrains.buildServer.serverSide.executors.ExecutorServices;
import org.jetbrains.annotations.NotNull;
import org.quartz.CronExpression;

import java.util.Date;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


public class MercurialCleaner {

  private final MirrorManager myMirrorManager;
  private final ServerPluginConfig myConfig;
  private final ScheduledExecutorService myExecutor;

  public MercurialCleaner(@NotNull final ServerPluginConfig config,
                          @NotNull final MirrorManager mirrorManager,
                          @NotNull final ExecutorServices executors) {
    myMirrorManager = mirrorManager;
    myConfig = config;
    myExecutor = executors.getNormalExecutorService();
    myExecutor.submit(new RunCleanup());
  }


  private class RunCleanup implements Runnable {
    private volatile boolean myFirstRun = true;

    public void run() {
      if (myFirstRun) {
        myFirstRun = false;
      } else {
        new Cleanup(myMirrorManager, myConfig).run();
      }
      schedule();
    }

    private void schedule() {
      CronExpression cron = myConfig.getCleanupCronExpression();
      if (cron != null) {
        Date now = new Date();
        Date next = cron.getNextValidTimeAfter(now);
        myExecutor.schedule(this, next.getTime() - now.getTime(), TimeUnit.MILLISECONDS);
      } else {
        //schedule ourselves to check if the cron expression specified
        myExecutor.schedule(this, 10, TimeUnit.MINUTES);
        //do not run cleanup next time, just schedule
        myFirstRun = true;
      }
    }
  }
}