1   /*
2    * Copyright 2019 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package com.linecorp.centraldogma.server.internal.storage;
17  
18  import static java.util.Objects.requireNonNull;
19  
20  import java.util.concurrent.CompletableFuture;
21  import java.util.concurrent.CompletionStage;
22  
23  import javax.annotation.Nullable;
24  
25  import com.google.common.base.MoreObjects;
26  
27  import com.linecorp.centraldogma.server.CentralDogmaConfig;
28  import com.linecorp.centraldogma.server.metadata.MetadataService;
29  import com.linecorp.centraldogma.server.plugin.Plugin;
30  import com.linecorp.centraldogma.server.plugin.PluginContext;
31  import com.linecorp.centraldogma.server.plugin.PluginTarget;
32  
33  public final class PurgeSchedulingServicePlugin implements Plugin {
34  
35      @Nullable
36      private volatile PurgeSchedulingService purgeSchedulingService;
37  
38      @Override
39      public PluginTarget target() {
40          return PluginTarget.LEADER_ONLY;
41      }
42  
43      @Override
44      public synchronized CompletionStage<Void> start(PluginContext context) {
45          requireNonNull(context, "context");
46  
47          PurgeSchedulingService purgeSchedulingService = this.purgeSchedulingService;
48          if (purgeSchedulingService == null) {
49              final CentralDogmaConfig cfg = context.config();
50              purgeSchedulingService = new PurgeSchedulingService(context.projectManager(),
51                                                                  context.purgeWorker(),
52                                                                  cfg.maxRemovedRepositoryAgeMillis());
53              this.purgeSchedulingService = purgeSchedulingService;
54          }
55          final MetadataService metadataService = new MetadataService(context.projectManager(),
56                                                                      context.commandExecutor());
57          purgeSchedulingService.start(context.commandExecutor(), metadataService);
58          return CompletableFuture.completedFuture(null);
59      }
60  
61      @Override
62      public synchronized CompletionStage<Void> stop(PluginContext context) {
63          final PurgeSchedulingService purgeSchedulingService = this.purgeSchedulingService;
64          if (purgeSchedulingService != null && purgeSchedulingService.isStarted()) {
65              purgeSchedulingService.stop();
66          }
67          return CompletableFuture.completedFuture(null);
68      }
69  
70      @Override
71      public boolean isEnabled(CentralDogmaConfig config) {
72          return requireNonNull(config, "config").maxRemovedRepositoryAgeMillis() > 0;
73      }
74  
75      @Nullable
76      public PurgeSchedulingService scheduledPurgingService() {
77          return purgeSchedulingService;
78      }
79  
80      @Override
81      public String toString() {
82          return MoreObjects.toStringHelper(this)
83                            .omitNullValues()
84                            .add("target", target())
85                            .add("purgeSchedulingService", purgeSchedulingService)
86                            .toString();
87      }
88  }