Skip to content

Commit

Permalink
send msg to mq when namespace published
Browse files Browse the repository at this point in the history
  • Loading branch information
lepdou committed Feb 20, 2017
1 parent ff71eb0 commit 1abe61e
Show file tree
Hide file tree
Showing 6 changed files with 326 additions and 47 deletions.
Expand Up @@ -215,5 +215,8 @@ public String cloggingPort() {
return getValue("clogging.server.port");
}

public String hermesServerAddress() {
return getValue("hermes.server.address");
}

}
Expand Up @@ -2,6 +2,7 @@

import com.ctrip.framework.apollo.common.constants.ReleaseOperation;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.ctrip.framework.apollo.portal.component.emailbuilder.GrayPublishEmailBuilder;
import com.ctrip.framework.apollo.portal.component.emailbuilder.MergeEmailBuilder;
Expand All @@ -11,12 +12,18 @@
import com.ctrip.framework.apollo.portal.entity.bo.ReleaseHistoryBO;
import com.ctrip.framework.apollo.portal.service.ReleaseHistoryService;
import com.ctrip.framework.apollo.portal.spi.EmailService;
import com.ctrip.framework.apollo.portal.spi.MQService;
import com.ctrip.framework.apollo.tracer.Tracer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.PostConstruct;

@Component
public class ConfigPublishListener {

Expand All @@ -34,75 +41,107 @@ public class ConfigPublishListener {
private MergeEmailBuilder mergeEmailBuilder;
@Autowired
private PortalConfig portalConfig;
@Autowired
private MQService mqService;

private ExecutorService executorService;

@PostConstruct
public void init() {
executorService = Executors.newSingleThreadExecutor(ApolloThreadFactory.create("ConfigPublishNotify", false));
}

@EventListener
public void onConfigPublish(ConfigPublishEvent event) {
Env env = event.getConfigPublishInfo().getEnv();
if (!portalConfig.emailSupportedEnvs().contains(env)) {
return;
}
executorService.submit(new ConfigPublishNotifyTask(event.getConfigPublishInfo()));
}

ReleaseHistoryBO releaseHistory = getReleaseHistory(event);

if (releaseHistory == null) {
Tracer.logError("Will not send email, because load release history error", null);
return;
}
private class ConfigPublishNotifyTask implements Runnable {

int realOperation = releaseHistory.getOperation();
private ConfigPublishEvent.ConfigPublishInfo publishInfo;

Email email = null;
try {
email = buildEmail(env, releaseHistory, realOperation);
} catch (Throwable e) {
Tracer.logError("build email failed.", e);
ConfigPublishNotifyTask(ConfigPublishEvent.ConfigPublishInfo publishInfo) {
this.publishInfo = publishInfo;
}

if (email != null) {
emailService.send(email);
@Override
public void run() {
ReleaseHistoryBO releaseHistory = getReleaseHistory();
if (releaseHistory == null) {
Tracer.logError("Load release history failed", null);
return;
}

sendPublishEmail(releaseHistory);

sendPublishMsg(releaseHistory);
}
}

private ReleaseHistoryBO getReleaseHistory(ConfigPublishEvent event) {
ConfigPublishEvent.ConfigPublishInfo info = event.getConfigPublishInfo();
Env env = info.getEnv();
private ReleaseHistoryBO getReleaseHistory() {
Env env = publishInfo.getEnv();

int operation = info.isMergeEvent() ? ReleaseOperation.GRAY_RELEASE_MERGE_TO_MASTER :
info.isRollbackEvent() ? ReleaseOperation.ROLLBACK :
info.isNormalPublishEvent() ? ReleaseOperation.NORMAL_RELEASE :
info.isGrayPublishEvent() ? ReleaseOperation.GRAY_RELEASE : -1;
int operation = publishInfo.isMergeEvent() ? ReleaseOperation.GRAY_RELEASE_MERGE_TO_MASTER :
publishInfo.isRollbackEvent() ? ReleaseOperation.ROLLBACK :
publishInfo.isNormalPublishEvent() ? ReleaseOperation.NORMAL_RELEASE :
publishInfo.isGrayPublishEvent() ? ReleaseOperation.GRAY_RELEASE : -1;

if (operation == -1) {
return null;
}
if (operation == -1) {
return null;
}

if (info.isRollbackEvent()) {
return releaseHistoryService
.findLatestByPreviousReleaseIdAndOperation(env, info.getPreviousReleaseId(), operation);
} else {
return releaseHistoryService.findLatestByReleaseIdAndOperation(env, info.getReleaseId(), operation);
}
if (publishInfo.isRollbackEvent()) {
return releaseHistoryService
.findLatestByPreviousReleaseIdAndOperation(env, publishInfo.getPreviousReleaseId(), operation);
} else {
return releaseHistoryService.findLatestByReleaseIdAndOperation(env, publishInfo.getReleaseId(), operation);
}

}
}

private void sendPublishEmail(ReleaseHistoryBO releaseHistory) {
Env env = publishInfo.getEnv();

private Email buildEmail(Env env, ReleaseHistoryBO releaseHistory, int operation) {
switch (operation) {
case ReleaseOperation.GRAY_RELEASE: {
return grayPublishEmailBuilder.build(env, releaseHistory);
if (!portalConfig.emailSupportedEnvs().contains(env)) {
return;
}
case ReleaseOperation.NORMAL_RELEASE: {
return normalPublishEmailBuilder.build(env, releaseHistory);

int realOperation = releaseHistory.getOperation();

Email email = null;
try {
email = buildEmail(env, releaseHistory, realOperation);
} catch (Throwable e) {
Tracer.logError("build email failed.", e);
}
case ReleaseOperation.ROLLBACK: {
return rollbackEmailBuilder.build(env, releaseHistory);

if (email != null) {
emailService.send(email);
}
case ReleaseOperation.GRAY_RELEASE_MERGE_TO_MASTER: {
return mergeEmailBuilder.build(env, releaseHistory);
}

private void sendPublishMsg(ReleaseHistoryBO releaseHistory) {
mqService.sendPublishMsg(publishInfo.getEnv(), releaseHistory);
}

private Email buildEmail(Env env, ReleaseHistoryBO releaseHistory, int operation) {
switch (operation) {
case ReleaseOperation.GRAY_RELEASE: {
return grayPublishEmailBuilder.build(env, releaseHistory);
}
case ReleaseOperation.NORMAL_RELEASE: {
return normalPublishEmailBuilder.build(env, releaseHistory);
}
case ReleaseOperation.ROLLBACK: {
return rollbackEmailBuilder.build(env, releaseHistory);
}
case ReleaseOperation.GRAY_RELEASE_MERGE_TO_MASTER: {
return mergeEmailBuilder.build(env, releaseHistory);
}
default:
return null;
}
default:
return null;
}
}

}
@@ -0,0 +1,10 @@
package com.ctrip.framework.apollo.portal.spi;

import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.entity.bo.ReleaseHistoryBO;

public interface MQService {

void sendPublishMsg(Env env, ReleaseHistoryBO releaseHistory);

}
@@ -0,0 +1,36 @@
package com.ctrip.framework.apollo.portal.spi.configuration;

import com.ctrip.framework.apollo.portal.spi.ctrip.CtripMQService;
import com.ctrip.framework.apollo.portal.spi.defaultimpl.DefaultMQService;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
public class MQConfiguration {

@Configuration
@Profile("ctrip")
public static class CtripMQConfiguration {

@Bean
public CtripMQService mqService() {
return new CtripMQService();
}
}

/**
* spring.profiles.active != ctrip
*/
@Configuration
@Profile({"!ctrip"})
public static class DefaultMQConfiguration {

@Bean
public DefaultMQService mqService() {
return new DefaultMQService();
}
}

}

0 comments on commit 1abe61e

Please sign in to comment.