View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.waarp.vitam.ingest;
22  
23  import fr.gouv.vitam.access.external.client.AdminExternalClient;
24  import fr.gouv.vitam.access.external.client.AdminExternalClientFactory;
25  import fr.gouv.vitam.ingest.external.client.IngestExternalClient;
26  import org.apache.commons.cli.CommandLine;
27  import org.apache.commons.cli.CommandLineParser;
28  import org.apache.commons.cli.DefaultParser;
29  import org.apache.commons.cli.HelpFormatter;
30  import org.apache.commons.cli.Options;
31  import org.apache.commons.cli.ParseException;
32  import org.waarp.common.logging.WaarpLogger;
33  import org.waarp.common.logging.WaarpLoggerFactory;
34  import org.waarp.common.utility.Version;
35  import org.waarp.common.utility.WaarpShutdownHook;
36  import org.waarp.openr66.configuration.FileBasedConfiguration;
37  import org.waarp.openr66.protocol.configuration.Configuration;
38  import org.waarp.vitam.common.WaarpCommon;
39  import org.waarp.vitam.common.WaarpCommon.MonitorOption;
40  import org.waarp.vitam.common.WaarpMonitor;
41  import org.waarp.vitam.common.WaarpVitamShutdownHook;
42  import org.waarp.vitam.common.WaarpVitamShutdownHook.WaarpVitamShutdownConfiguration;
43  
44  import java.io.File;
45  
46  /**
47   * IngestMonitor is the daemon taking care of IngestRequests through a
48   * directory containing JSON files
49   */
50  public class IngestMonitor extends WaarpMonitor {
51    /**
52     * Internal Logger
53     */
54    private static final WaarpLogger logger =
55        WaarpLoggerFactory.getLogger(IngestMonitor.class);
56  
57    private static File waarpConfigurationFile;
58  
59    private final IngestRequestFactory factory;
60    private final IngestManager ingestManager;
61  
62    /**
63     * Unique constructor
64     *
65     * @param elapseTime
66     * @param stopFile
67     * @param factory
68     * @param ingestManager
69     */
70    IngestMonitor(final long elapseTime, final File stopFile,
71                  final IngestRequestFactory factory,
72                  final AdminExternalClientFactory adminFactory,
73                  final IngestManager ingestManager) {
74      super(stopFile, adminFactory, elapseTime);
75      this.factory = factory;
76      this.ingestManager = ingestManager;
77      if (WaarpShutdownHook.shutdownHook == null) {
78        new WaarpVitamShutdownHook(new WaarpVitamShutdownConfiguration(this));
79        WaarpVitamShutdownHook.addShutdownHook();
80      }
81    }
82  
83    /**
84     * Will try to start the IngestMonitor according to arguments, else print
85     * the help message
86     *
87     * @param args
88     */
89    public static void main(String[] args) {
90      Options options = getOptions();
91  
92      if (args.length == 0 || WaarpCommon.checkHelp(args)) {
93        printHelp(options);
94        return;
95      }
96      final IngestMonitor ingestMonitor;
97      try {
98        ingestMonitor = getIngestMonitor(options, args);
99      } catch (ParseException e) {
100       logger.error("Error while initializing {}", IngestMonitor.class.getName(),
101                    e);
102       printHelp(options);
103       return;
104     }
105     if (!FileBasedConfiguration
106         .setSubmitClientConfigurationFromXml(Configuration.configuration,
107                                              waarpConfigurationFile
108                                                  .getAbsolutePath())) {
109       logger.error("Cannot load Waarp Configuration");
110       return;
111     }
112     ingestMonitor.invoke();
113   }
114 
115   /**
116    * Define the options associated
117    *
118    * @return the Options
119    */
120   private static Options getOptions() {
121     Options options = new Options();
122     MonitorOption.setStandardMonitorOptions(options);
123     MonitorOption.addRetryMonitorOptions(options);
124     options.addOption(IngestRequestFactory.getDirectoryOption());
125     return options;
126   }
127 
128   /**
129    * Helper to print help
130    *
131    * @param options
132    */
133   private static void printHelp(Options options) {
134     HelpFormatter formatter = new HelpFormatter();
135     formatter.printHelp(IngestMonitor.class.getSimpleName(),
136                         "Version: " + Version.fullIdentifier(), options,
137                         WaarpCommon.FOR_SIMPLE_MANDATORY_ARGUMENTS, true);
138   }
139 
140   /**
141    * Build the IngestMonitor according to arguments
142    *
143    * @param options
144    * @param args
145    *
146    * @return the new IngestMonitor
147    *
148    * @throws ParseException
149    */
150   private static IngestMonitor getIngestMonitor(Options options, String[] args)
151       throws ParseException {
152     CommandLineParser parser = new DefaultParser();
153     CommandLine cmd = parser.parse(options, args);
154     IngestRequestFactory.parseDirectoryOption(cmd);
155     MonitorOption monitorOption =
156         WaarpCommon.MonitorOption.gestMonitorOption(cmd, args);
157     waarpConfigurationFile = new File(monitorOption.getWaarpConfiguration());
158     return new IngestMonitor(monitorOption.getElapseInSecond() * 1000L,
159                              new File(monitorOption.getStopFilePath()),
160                              IngestRequestFactory.getInstance(),
161                              AdminExternalClientFactory.getInstance(),
162                              new IngestManager());
163   }
164 
165   /**
166    * Launch the IngestMonitor
167    */
168   public void invoke() {
169     try (IngestExternalClient client = factory.getClient();
170          AdminExternalClient adminExternalClient = getAdminFactory()
171              .getClient()) {
172       logger.warn("Start of {}", IngestMonitor.class.getName());
173       while (!isShutdown()) {
174         ingestManager
175             .retryAllExistingFiles(factory, client, adminExternalClient, this);
176         Thread.sleep(getElapseTime());
177       }
178       setShutdown(true);
179       logger.warn("Stop of {}", IngestMonitor.class.getName());
180     } catch (InterruptedException e) {//NOSONAR
181       logger.error("{} will stop", IngestMonitor.class.getName(), e);
182     }
183   }
184 
185 }