Juan Hernandez has uploaded a new change for review. Change subject: engine: Add --first-run option to notifier ......................................................................
engine: Add --first-run option to notifier Add a new --first-run command line option to the notification service start script. This option is used to indicate that all existing events should be marked as processed before starting the service. Change-Id: I242bc0fbeab73d7f2d363b3e9ebf895af0131033 Signed-off-by: Juan Hernandez <juan.hernan...@redhat.com> --- M backend/manager/tools/engine-notifier/engine-notifier-resources/src/main/resources/notifier.sh M backend/manager/tools/engine-notifier/engine-notifier-service/pom.xml M backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/NotificationService.java M backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/Notifier.java M backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationConfigurator.java M backend/manager/tools/engine-notifier/engine-notifier-service/src/test/java/org/ovirt/engine/core/notifier/NotificationServiceTest.java M packaging/fedora/spec/ovirt-engine.spec.in M pom.xml 8 files changed, 153 insertions(+), 58 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/72/8772/1 diff --git a/backend/manager/tools/engine-notifier/engine-notifier-resources/src/main/resources/notifier.sh b/backend/manager/tools/engine-notifier/engine-notifier-resources/src/main/resources/notifier.sh index 60c970a..55bab62 100755 --- a/backend/manager/tools/engine-notifier/engine-notifier-resources/src/main/resources/notifier.sh +++ b/backend/manager/tools/engine-notifier/engine-notifier-resources/src/main/resources/notifier.sh @@ -9,34 +9,31 @@ # Load the prolog: . "$(dirname "$(readlink -f "$0")")"/engine-prolog.sh -usage () { - printf "engine-notifier: oVirt Event Notification Service\n" - printf "USAGE:\n" - printf "\tengine-notifier [configuration file]\n" - return 0 -} - die_no_propset() { # exit when property defined but not set then exit die "Error: $1 if defined can not be empty, please check for this in configuration file $CONF_FILE\n" 6 } -if [ "$1" == "--help" -o "$1" == "-h" ]; then - usage - exit 0 -fi - -if [ "$#" -gt 1 ]; then - usage - die "Error: wrong argument number: $#.\n" 2 -fi - -if [ "$#" -eq 1 ]; then - if [ ! -r "$1" ]; then - die "Error: configuration file does not exist or has no read permission: $1.\n" 6 +# Try to find the configuration file in the command line, it should be the first +# argument that doesn't start with a dash. Also build the list of command line +# arguments that should be passed to the Java program (excluding the configuration +# file): +CONF_FILE="" +JAVA_ARGS="" +for i in $@ +do + if [ -z "${CONF_FILE}" -a "${i:0:1}" != "-" ] + then + CONF_FILE="${i}" + else + JAVA_ARGS="${JAVA_ARGS} ${i}" fi - CONF_FILE="$1" -else +done + + +# If no configuration file is given in the command line then use the default: +if [ -z "${CONF_FILE}" ] +then CONF_FILE="${ENGINE_ETC}/notifier/notifier.conf" fi @@ -180,6 +177,7 @@ commons-codec commons-collections commons-configuration + commons-cli commons-jxpath commons-lang commons-logging diff --git a/backend/manager/tools/engine-notifier/engine-notifier-service/pom.xml b/backend/manager/tools/engine-notifier/engine-notifier-service/pom.xml index 0eb8bc7..4c9a5ec 100644 --- a/backend/manager/tools/engine-notifier/engine-notifier-service/pom.xml +++ b/backend/manager/tools/engine-notifier/engine-notifier-service/pom.xml @@ -42,6 +42,11 @@ </dependency> <dependency> + <groupId>commons-cli</groupId> + <artifactId>commons-cli</artifactId> + </dependency> + + <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <scope>provided</scope> diff --git a/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/NotificationService.java b/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/NotificationService.java index 01ed4a9..de47c06 100644 --- a/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/NotificationService.java +++ b/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/NotificationService.java @@ -45,9 +45,17 @@ private NotificationMethodFactoryMapper methodsMapper = null; private boolean shouldDeleteHistory = false; private int daysToKeepHistory; + + /** + * This flag indicates if this is the first time we are running the notification + * service and thus all the existing events should be marked as processed + * before starting. + */ + private boolean firstRun = false; - public NotificationService(NotificationConfigurator notificationConf) throws NotificationServiceException { + public NotificationService(NotificationConfigurator notificationConf, boolean firstRun) throws NotificationServiceException { this.prop = notificationConf.getProperties(); + this.firstRun = firstRun; initConfigurationProperties(); } @@ -92,7 +100,19 @@ try { log.debug("Start event notification service iteration"); startup(); + + // If this is the first run then we need to mark all the existing audit events as + // processed and also update the flag so that we will not do the same the next + // time we run this task: + if (firstRun) { + markAllEventsAsProcessed(); + firstRun = false; + } + + // Process the audit events and send the corresponding messages: processEvents(); + + // If required clean up the history of sent notifications: if (shouldDeleteHistory) { deleteObseleteHistoryData(); } @@ -254,6 +274,24 @@ } } + private void markAllEventsAsProcessed () throws SQLException { + Connection connection = null; + PreparedStatement ps = null; + try { + connection = ds.getConnection(); + ps = connection.prepareStatement("update audit_log set processed = 'true' where processed = 'false'"); + int count = ps.executeUpdate(); + log.info("Marked " + count + " audit log entries as processed during first run."); + } finally { + if (ps != null) { + ps.close(); + } + if (connection != null) { + connection.close(); + } + } + } + private void addEventNotificationHistory(event_notification_hist eventHistory) throws SQLException { diff --git a/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/Notifier.java b/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/Notifier.java index fa2c18b..1206ebb 100644 --- a/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/Notifier.java +++ b/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/Notifier.java @@ -7,6 +7,11 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.GnuParser; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.ovirt.engine.core.notifier.utils.NotificationConfigurator; @@ -18,20 +23,40 @@ /** * Main class of event notification service. Initiate the service and handles termination signals */ -@SuppressWarnings("restriction") public class Notifier { - + // The log: private static final Log log = LogFactory.getLog(Notifier.class); + + // Help text: + private static final String HELP_TEXT = + "Usage: notifier [OPTIONS]... [FILE]\n" + + "Starts the RHEV event notification service reading the\n" + + "configuration from FILE.\n" + + "\n" + + "Available options:\n" + + " -h, --help Display this help text and exit.\n" + + " -f, --first-run Mark all the existing audit events as processed\n" + + " and then run as usual.\n" + ; + + // During the first run of the notifier we don't want to send all the old + // events stored in the database: + private static boolean firstRun = false; + + // The location of the configuration file: + private static String configurationFile = "/etc/ovirt-engine/notifier/notifier.conf"; + private static ScheduledExecutorService notifyScheduler = Executors.newSingleThreadScheduledExecutor(); private static ScheduledExecutorService monitorScheduler = Executors.newSingleThreadScheduledExecutor(); private static final long DEFAULT_NOTIFICATION_INTERVAL_IN_SECONDS = 120; private static final long DEFAULT_ENGINE_MONITOR_INTERVAL_IN_SECONDS = 300; - /** - * @param args - * [0] configuration file absolute path - */ public static void main(String[] args) { + // Parse the command line options and arguments: + parseCommandLine(args); + + // Create a signal handler that will shutdown the schedulers when we + // receive the SIGHUP signal from the operating system: NotifierSignalHandler handler = new NotifierSignalHandler(); Signal.handle(new Signal("HUP"), handler); handler.addScheduledExecutorService(notifyScheduler); @@ -43,10 +68,7 @@ long engineMonitorInterval; long notificationInterval; try { - String configurationFile = null; - if (args != null && args.length == 1) { - configurationFile = args[0]; - } + // Load the configuration file: notificationConf = new NotificationConfigurator(configurationFile); // This check will be not mandatory when SMS is implemented. @@ -55,7 +77,7 @@ throw new IllegalArgumentException("Check configuration file, " + configurationFile + " MAIL_SERVER is missing"); } - notificationService = new NotificationService(notificationConf); + notificationService = new NotificationService(notificationConf, firstRun); engineMonitorService = new EngineMonitorService(notificationConf); notificationInterval = @@ -90,6 +112,45 @@ } /** + * Parses the command line parameters. + * + * @param args an array of strings containing the command line parameters + * given by the operating system + */ + private static void parseCommandLine(String[] args) { + // Parse the command line options: + Options options = new Options(); + options.addOption("h", "help", false, null); + options.addOption("f", "first-run", false, null); + CommandLineParser parser = new GnuParser(); + CommandLine line = null; + try { + line = parser.parse(options, args); + } + catch (ParseException exception) { + System.out.println(exception.getMessage()); + log.error("Error parsing command line options.", exception); + System.exit(1); + } + + // If help has been requested then show it and exit immediately: + if (line.hasOption("help")) { + System.out.println(HELP_TEXT); + System.exit(0); + } + + // Get the values of the options: + firstRun = line.hasOption("first-run"); + + // Get the name of the configuration file: + args = line.getArgs(); + if (args != null && args.length == 1) { + configurationFile = args[0]; + } + } + + + /** * Class designed to handle a proper shutdown in case of an external signal which was registered was caught by the * program. */ diff --git a/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationConfigurator.java b/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationConfigurator.java index 00b893b..8ea9427 100644 --- a/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationConfigurator.java +++ b/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationConfigurator.java @@ -13,9 +13,9 @@ * It provides simple properties querying for timer interval properties types, e.g. {@link #getTimerInterval(String)} */ public class NotificationConfigurator { - - private static final String DEFAULT_CONF_FILE_LOCATION = "/etc/engine/notifier/notifier.conf"; + // The log: private static final Log log = LogFactory.getLog(NotificationConfigurator.class); + private Map<String, String> prop = null; /** @@ -25,30 +25,14 @@ * @throws NotificationServiceException */ public NotificationConfigurator(String confFile) throws NotificationServiceException { - setConfigurationFile(confFile); - } - - /** - * Set local configuration file for the notification service override the default configuration file - * @param localConfFile - * the path of the alternate configuration file - * @throws NotificationServiceException - */ - private void setConfigurationFile(String localConfFile) throws NotificationServiceException { - String confFileLocation; - if (StringUtils.isNotEmpty(localConfFile)) { - confFileLocation = localConfFile; - log.info("Starting event notification service with configuration file: " + confFileLocation); - } else { - confFileLocation = DEFAULT_CONF_FILE_LOCATION; - log.info("Starting event notification service with default configuration file: " + confFileLocation); + if (StringUtils.isNotEmpty(confFile)) { + log.info("Starting event notification service with configuration file: " + confFile); } - File file = new File(confFileLocation); + File file = new File(confFile); if (!file.canRead()) { - throw new NotificationServiceException("Configuration file does not exist or missing permissions: " - + file.getAbsoluteFile()); + throw new NotificationServiceException("Configuration file does not exist or missing permissions: " + file.getAbsoluteFile()); } - prop = NotificationProperties.readPropertiesFile(confFileLocation); + prop = NotificationProperties.readPropertiesFile(confFile); } /** diff --git a/backend/manager/tools/engine-notifier/engine-notifier-service/src/test/java/org/ovirt/engine/core/notifier/NotificationServiceTest.java b/backend/manager/tools/engine-notifier/engine-notifier-service/src/test/java/org/ovirt/engine/core/notifier/NotificationServiceTest.java index 838f766..071251b 100644 --- a/backend/manager/tools/engine-notifier/engine-notifier-service/src/test/java/org/ovirt/engine/core/notifier/NotificationServiceTest.java +++ b/backend/manager/tools/engine-notifier/engine-notifier-service/src/test/java/org/ovirt/engine/core/notifier/NotificationServiceTest.java @@ -33,7 +33,7 @@ NotificationService notificationService = null; try { notificationService = - new NotificationService(new NotificationConfigurator("src/test/resources/conf/notifier.conf")); + new NotificationService(new NotificationConfigurator("src/test/resources/conf/notifier.conf"), false); } catch (NotificationServiceException e) { e.printStackTrace(); } diff --git a/packaging/fedora/spec/ovirt-engine.spec.in b/packaging/fedora/spec/ovirt-engine.spec.in index c2d1a59..58e2474 100644 --- a/packaging/fedora/spec/ovirt-engine.spec.in +++ b/packaging/fedora/spec/ovirt-engine.spec.in @@ -92,6 +92,7 @@ # Build time requirements: BuildRequires: apache-commons-beanutils +BuildRequires: apache-commons-cli BuildRequires: apache-commons-codec BuildRequires: apache-commons-collections BuildRequires: apache-commons-configuration @@ -214,6 +215,7 @@ Group: Virtualization/Management Requires: %{name} = %{version}-%{release} Requires: apache-commons-beanutils +Requires: apache-commons-cli Requires: apache-commons-codec Requires: apache-commons-collections Requires: apache-commons-lang @@ -444,6 +446,7 @@ apache-mina/mina-core mina-core apache-sshd/sshd-core sshd-core commons-beanutils commons-beanutils +commons-cli commons-cli commons-codec commons-codec commons-collections commons-collections commons-httpclient commons-httpclient diff --git a/pom.xml b/pom.xml index f1ee14f..e660e9b 100644 --- a/pom.xml +++ b/pom.xml @@ -25,6 +25,7 @@ <junit.version>4.8.1</junit.version> <commons-codec.version>1.4</commons-codec.version> <commons-lang.version>2.4</commons-lang.version> + <commons-cli.version>1.2</commons-cli.version> <commons-compress.version>1.3</commons-compress.version> <quartz.version>2.1.2</quartz.version> <postgres.jdbc.version>8.4-702.jdbc4</postgres.jdbc.version> @@ -175,6 +176,11 @@ <version>${commons-lang.version}</version> </dependency> <dependency> + <groupId>commons-cli</groupId> + <artifactId>commons-cli</artifactId> + <version>${commons-cli.version}</version> + </dependency> + <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>${commons-compress.version}</version> -- To view, visit http://gerrit.ovirt.org/8772 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I242bc0fbeab73d7f2d363b3e9ebf895af0131033 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Juan Hernandez <juan.hernan...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches