From f75b29e0db7259bbc2278b85deb457921bd20503 Mon Sep 17 00:00:00 2001 From: Daniel Norberg Date: Tue, 21 Jun 2016 11:33:30 +0900 Subject: [PATCH] allow specifying custom notification senders * Look up sender class for `notification.type` using @Named annotation. * Move notification implementation from agent module to separate notification module. --- .../main/java/io/digdag/core/DigdagEmbed.java | 2 ++ .../io/digdag/core/agent/AgentModule.java | 6 ++---- .../DefaultNotifier.java | 19 ++++------------- .../HttpNotificationSender.java | 2 +- .../MailNotificationSender.java | 2 +- .../core/notification/NotificationModule.java | 21 +++++++++++++++++++ .../ShellNotificationSender.java | 2 +- 7 files changed, 32 insertions(+), 22 deletions(-) rename digdag-core/src/main/java/io/digdag/core/{agent => notification}/DefaultNotifier.java (80%) rename digdag-core/src/main/java/io/digdag/core/{agent => notification}/HttpNotificationSender.java (98%) rename digdag-core/src/main/java/io/digdag/core/{agent => notification}/MailNotificationSender.java (99%) create mode 100644 digdag-core/src/main/java/io/digdag/core/notification/NotificationModule.java rename digdag-core/src/main/java/io/digdag/core/{agent => notification}/ShellNotificationSender.java (98%) diff --git a/digdag-core/src/main/java/io/digdag/core/DigdagEmbed.java b/digdag-core/src/main/java/io/digdag/core/DigdagEmbed.java index 995a4f9ddb..3804de1e0b 100644 --- a/digdag-core/src/main/java/io/digdag/core/DigdagEmbed.java +++ b/digdag-core/src/main/java/io/digdag/core/DigdagEmbed.java @@ -17,6 +17,7 @@ import com.google.inject.Scopes; import com.google.inject.util.Modules; import com.google.inject.multibindings.Multibinder; +import io.digdag.core.notification.NotificationModule; import io.digdag.core.queue.QueueModule; import io.digdag.core.log.NullLogServerFactory; import io.digdag.core.log.LocalFileLogServerFactory; @@ -149,6 +150,7 @@ private List standardModules(ConfigElement systemConfig) new ConfigModule(), new WorkflowModule(), new QueueModule(), + new NotificationModule(), (binder) -> { binder.bind(ProjectArchiveLoader.class); binder.bind(ConfigElement.class).toInstance(systemConfig); diff --git a/digdag-core/src/main/java/io/digdag/core/agent/AgentModule.java b/digdag-core/src/main/java/io/digdag/core/agent/AgentModule.java index 13522f3f21..366af71261 100644 --- a/digdag-core/src/main/java/io/digdag/core/agent/AgentModule.java +++ b/digdag-core/src/main/java/io/digdag/core/agent/AgentModule.java @@ -3,6 +3,8 @@ import com.google.inject.Module; import com.google.inject.Binder; import com.google.inject.Scopes; +import com.google.inject.name.Names; +import io.digdag.spi.NotificationSender; import io.digdag.spi.Notifier; import io.digdag.spi.TemplateEngine; import io.digdag.spi.CommandLogger; @@ -17,10 +19,6 @@ public void configure(Binder binder) binder.bind(ConfigEvalEngine.class).in(Scopes.SINGLETON); binder.bind(TemplateEngine.class).to(ConfigEvalEngine.class).in(Scopes.SINGLETON); - binder.bind(HttpNotificationSender.class); - binder.bind(MailNotificationSender.class); - binder.bind(ShellNotificationSender.class); - binder.bind(Notifier.class).to(DefaultNotifier.class).in(Scopes.SINGLETON); // log binder.bind(CommandLogger.class).to(TaskContextCommandLogger.class).in(Scopes.SINGLETON); diff --git a/digdag-core/src/main/java/io/digdag/core/agent/DefaultNotifier.java b/digdag-core/src/main/java/io/digdag/core/notification/DefaultNotifier.java similarity index 80% rename from digdag-core/src/main/java/io/digdag/core/agent/DefaultNotifier.java rename to digdag-core/src/main/java/io/digdag/core/notification/DefaultNotifier.java index c77608e794..04c49ec42f 100644 --- a/digdag-core/src/main/java/io/digdag/core/agent/DefaultNotifier.java +++ b/digdag-core/src/main/java/io/digdag/core/notification/DefaultNotifier.java @@ -1,9 +1,11 @@ -package io.digdag.core.agent; +package io.digdag.core.notification; import com.google.common.base.Optional; import com.google.common.base.Throwables; import com.google.inject.Inject; import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.name.Names; import io.digdag.client.config.Config; import io.digdag.spi.Notification; import io.digdag.spi.NotificationException; @@ -19,9 +21,6 @@ public class DefaultNotifier implements Notifier { private static final String NOTIFICATION_TYPE = "notification.type"; - private static final String NOTIFICATION_TYPE_MAIL = "mail"; - private static final String NOTIFICATION_TYPE_HTTP = "http"; - private static final String NOTIFICATION_TYPE_SHELL = "shell"; private static final String NOTIFICATION_RETRIES = "notification.retries"; private static final String NOTIFICATION_MIN_RETRY_WAIT = "notification.min_retry_wait"; @@ -51,17 +50,7 @@ public DefaultNotifier(Config systemConfig, Injector injector) private NotificationSender sender(String type) { - // TODO: use key instead? - switch (type) { - case NOTIFICATION_TYPE_MAIL: - return injector.getInstance(MailNotificationSender.class); - case NOTIFICATION_TYPE_HTTP: - return injector.getInstance(HttpNotificationSender.class); - case NOTIFICATION_TYPE_SHELL: - return injector.getInstance(ShellNotificationSender.class); - default: - throw new IllegalArgumentException("Unknown notification type: " + type); - } + return injector.getInstance(Key.get(NotificationSender.class, Names.named(type))); } @Override diff --git a/digdag-core/src/main/java/io/digdag/core/agent/HttpNotificationSender.java b/digdag-core/src/main/java/io/digdag/core/notification/HttpNotificationSender.java similarity index 98% rename from digdag-core/src/main/java/io/digdag/core/agent/HttpNotificationSender.java rename to digdag-core/src/main/java/io/digdag/core/notification/HttpNotificationSender.java index c9f564a233..6ff53e4fbe 100644 --- a/digdag-core/src/main/java/io/digdag/core/agent/HttpNotificationSender.java +++ b/digdag-core/src/main/java/io/digdag/core/notification/HttpNotificationSender.java @@ -1,4 +1,4 @@ -package io.digdag.core.agent; +package io.digdag.core.notification; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.guava.GuavaModule; diff --git a/digdag-core/src/main/java/io/digdag/core/agent/MailNotificationSender.java b/digdag-core/src/main/java/io/digdag/core/notification/MailNotificationSender.java similarity index 99% rename from digdag-core/src/main/java/io/digdag/core/agent/MailNotificationSender.java rename to digdag-core/src/main/java/io/digdag/core/notification/MailNotificationSender.java index e62fedd8f8..fa8c5f3e00 100644 --- a/digdag-core/src/main/java/io/digdag/core/agent/MailNotificationSender.java +++ b/digdag-core/src/main/java/io/digdag/core/notification/MailNotificationSender.java @@ -1,4 +1,4 @@ -package io.digdag.core.agent; +package io.digdag.core.notification; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/digdag-core/src/main/java/io/digdag/core/notification/NotificationModule.java b/digdag-core/src/main/java/io/digdag/core/notification/NotificationModule.java new file mode 100644 index 0000000000..9ef6e3a557 --- /dev/null +++ b/digdag-core/src/main/java/io/digdag/core/notification/NotificationModule.java @@ -0,0 +1,21 @@ +package io.digdag.core.notification; + +import com.google.inject.Binder; +import com.google.inject.Module; +import com.google.inject.Scopes; +import com.google.inject.name.Names; +import io.digdag.spi.NotificationSender; +import io.digdag.spi.Notifier; + +public class NotificationModule + implements Module +{ + @Override + public void configure(Binder binder) + { + binder.bind(NotificationSender.class).annotatedWith(Names.named("http")).to(HttpNotificationSender.class); + binder.bind(NotificationSender.class).annotatedWith(Names.named("mail")).to(MailNotificationSender.class); + binder.bind(NotificationSender.class).annotatedWith(Names.named("shell")).to(ShellNotificationSender.class); + binder.bind(Notifier.class).to(DefaultNotifier.class).in(Scopes.SINGLETON); + } +} diff --git a/digdag-core/src/main/java/io/digdag/core/agent/ShellNotificationSender.java b/digdag-core/src/main/java/io/digdag/core/notification/ShellNotificationSender.java similarity index 98% rename from digdag-core/src/main/java/io/digdag/core/agent/ShellNotificationSender.java rename to digdag-core/src/main/java/io/digdag/core/notification/ShellNotificationSender.java index 84613ab2ec..7236056eab 100644 --- a/digdag-core/src/main/java/io/digdag/core/agent/ShellNotificationSender.java +++ b/digdag-core/src/main/java/io/digdag/core/notification/ShellNotificationSender.java @@ -1,4 +1,4 @@ -package io.digdag.core.agent; +package io.digdag.core.notification; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper;