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(CentralDogmaConfig config) {
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                                                                      context.internalProjectInitializer());
58          purgeSchedulingService.start(context.commandExecutor(), metadataService);
59          return CompletableFuture.completedFuture(null);
60      }
61  
62      @Override
63      public synchronized CompletionStage<Void> stop(PluginContext context) {
64          final PurgeSchedulingService purgeSchedulingService = this.purgeSchedulingService;
65          if (purgeSchedulingService != null && purgeSchedulingService.isStarted()) {
66              purgeSchedulingService.stop();
67          }
68          return CompletableFuture.completedFuture(null);
69      }
70  
71      @Override
72      public boolean isEnabled(CentralDogmaConfig config) {
73          return requireNonNull(config, "config").maxRemovedRepositoryAgeMillis() > 0;
74      }
75  
76      @Override
77      public Class<?> configType() {
78          // Return the plugin class itself because it does not have a configuration.
79          return getClass();
80      }
81  
82      @Nullable
83      public PurgeSchedulingService scheduledPurgingService() {
84          return purgeSchedulingService;
85      }
86  
87      @Override
88      public String toString() {
89          return MoreObjects.toStringHelper(this)
90                            .omitNullValues()
91                            .add("target", PluginTarget.LEADER_ONLY)
92                            .add("purgeSchedulingService", purgeSchedulingService)
93                            .toString();
94      }
95  }