Skip to content

Commit c74a8dd

Browse files
committed
avniproject/avni-server#859 | Enable RWB module to run in multi-system integration mode
1 parent a713c3c commit c74a8dd

File tree

12 files changed

+140
-111
lines changed

12 files changed

+140
-111
lines changed

goonj/src/main/java/org/avni_integration_service/goonj/config/GoonjSpringConfig.java

-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package org.avni_integration_service.goonj.config;
22

33
import org.avni_integration_service.goonj.service.TokenService;
4-
import org.avni_integration_service.integration_data.domain.IntegrationSystem;
5-
import org.avni_integration_service.integration_data.repository.IntegrationSystemRepository;
64
import org.springframework.beans.factory.annotation.Autowired;
7-
import org.springframework.beans.factory.annotation.Value;
85
import org.springframework.boot.web.client.RestTemplateBuilder;
96
import org.springframework.context.annotation.Bean;
107
import org.springframework.http.HttpHeaders;
118
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
129
import org.springframework.stereotype.Component;
1310
import org.springframework.web.client.RestTemplate;
1411

15-
import java.util.Arrays;
1612
import java.util.Collections;
1713

1814
@Component

integrator/src/main/java/org/avni_integration_service/scheduler/IntegrationJobScheduler.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.avni_integration_service.job.AvniPowerMainJob;
1414
import org.avni_integration_service.lahi.job.AvniLahiFullErrorJob;
1515
import org.avni_integration_service.lahi.job.AvniLahiMainJob;
16+
import org.avni_integration_service.rwb.config.RwbConfig;
1617
import org.avni_integration_service.rwb.job.AvniRwbMainJob;
1718
import org.springframework.beans.factory.annotation.Autowired;
1819
import org.springframework.beans.factory.annotation.Value;
@@ -55,16 +56,14 @@ public class IntegrationJobScheduler {
5556
private String amritCron;
5657
@Value("${amrit.app.cron.full.error}")
5758
private String amritCronError;
58-
@Value("${rwb.app.cron.main}")
59-
private String rwbCron;
6059

6160
@Autowired
6261
public IntegrationJobScheduler(AvniGoonjMainJob avniGoonjMainJob, AvniGoonjFullErrorJob avniGoonjFullErrorJob,
6362
AvniPowerMainJob avniPowerMainJob, AvniPowerFullErrorJob avniPowerFullErrorJob,
6463
AvniLahiMainJob avniLahiMainJob, AvniLahiFullErrorJob avniLahiFullErrorJob,
6564
AvniAmritMainJob avniAmritMainJob, AvniAmritFullErrorJob avniAmritFullErrorJob,
66-
AvniRwbMainJob avniRwbMainJob,
67-
TaskScheduler taskScheduler, IntegrationSystemConfigRepository integrationSystemConfigRepository, IntegrationSystemRepository integrationSystemRepository) {
65+
AvniRwbMainJob avniRwbMainJob, TaskScheduler taskScheduler,
66+
IntegrationSystemConfigRepository integrationSystemConfigRepository, IntegrationSystemRepository integrationSystemRepository) {
6867
this.avniGoonjMainJob = avniGoonjMainJob;
6968
this.avniGoonjFullErrorJob = avniGoonjFullErrorJob;
7069
this.avniPowerMainJob = avniPowerMainJob;
@@ -121,7 +120,15 @@ private void schedulePower() {
121120

122121

123122
private void scheduleRwb() {
124-
if (CronExpression.isValidExpression(rwbCron)) taskScheduler.schedule(avniRwbMainJob::execute, new CronTrigger(rwbCron));
125-
// TODO: 24/12/24 rwbCronError
123+
List<IntegrationSystem> rwbSystems = integrationSystemRepository.findAllBySystemType(IntegrationSystem.IntegrationSystemType.rwb);
124+
rwbSystems.forEach(rwbSystem -> {
125+
IntegrationSystemConfigCollection integrationSystemConfigs = integrationSystemConfigRepository.getInstanceConfiguration(rwbSystem);
126+
RwbConfig rwbConfig = new RwbConfig(integrationSystemConfigs, rwbSystem);
127+
String rwbCron = integrationSystemConfigs.getMainScheduledJobCron();
128+
129+
if (CronExpression.isValidExpression(rwbCron)) {
130+
taskScheduler.schedule(() -> avniRwbMainJob.execute(rwbConfig), new CronTrigger(rwbCron));
131+
}
132+
});
126133
}
127134
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
package org.avni_integration_service.rwb.config;
22

33
import org.avni_integration_service.avni.client.AvniSession;
4-
import org.springframework.beans.factory.annotation.Value;
54
import org.springframework.stereotype.Component;
65

76
@Component
87
public class RwbAvniSessionFactory {
9-
@Value("${rwb.avni.api.url}")
10-
private String apiUrl;
118

12-
@Value("${rwb.avni.impl.username}")
13-
private String implUser;
9+
private final RwbContextProvider rwbContextProvider;
1410

15-
@Value("${rwb.avni.impl.password}")
16-
private String implPassword;
17-
18-
@Value("${rwb.avni.authentication.enabled}")
19-
private boolean authEnabled;
11+
public RwbAvniSessionFactory(RwbContextProvider rwbContextProvider) {
12+
this.rwbContextProvider = rwbContextProvider;
13+
}
2014

2115
public AvniSession createSession() {
22-
return new AvniSession(apiUrl, implUser, implPassword, authEnabled);
16+
RwbConfig rwbConfig = rwbContextProvider.get();
17+
return new AvniSession(rwbConfig.getApiUrl(), rwbConfig.getAvniImplUser(), rwbConfig.getImplPassword(), rwbConfig.getAuthEnabled());
2318
}
2419
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.avni_integration_service.rwb.config;
2+
3+
import org.avni_integration_service.integration_data.context.ContextIntegrationSystem;
4+
import org.avni_integration_service.integration_data.domain.IntegrationSystem;
5+
import org.avni_integration_service.integration_data.domain.config.IntegrationSystemConfigCollection;
6+
import org.springframework.util.StringUtils;
7+
8+
public class RwbConfig {
9+
private final IntegrationSystemConfigCollection integrationSystemConfigCollection;
10+
private final ContextIntegrationSystem integrationSystem;
11+
12+
public RwbConfig(IntegrationSystemConfigCollection integrationSystemConfigCollection, IntegrationSystem integrationSystem) {
13+
this.integrationSystemConfigCollection = integrationSystemConfigCollection;
14+
this.integrationSystem = new ContextIntegrationSystem(integrationSystem);
15+
}
16+
17+
private String getStringConfigValue(String key, String defaultValue) {
18+
String configValue = integrationSystemConfigCollection.getConfigValue(key);
19+
return StringUtils.hasLength(configValue) ? configValue : defaultValue;
20+
}
21+
22+
public String getApiUrl() {
23+
return getStringConfigValue("avni_api_url", "https://app.avniproject.org");
24+
}
25+
26+
public String getAvniImplUser() {
27+
return getStringConfigValue("avni_user", "dummy");
28+
}
29+
30+
public String getImplPassword() {
31+
return getStringConfigValue("avni_password", "dummy");
32+
}
33+
34+
public boolean getAuthEnabled() {
35+
return Boolean.parseBoolean(getStringConfigValue("avni_auth_enabled", "true"));
36+
}
37+
38+
public String getCustomQueryName() {
39+
return getStringConfigValue("custom_query", "Inactive users");
40+
}
41+
42+
public String getSinceNoOfDays() {
43+
return getStringConfigValue("since_no_of_days", "01");
44+
}
45+
46+
public String getWithinNoOfDays() {
47+
return getStringConfigValue("within_no_of_days", "03");
48+
}
49+
50+
public String getMsgTemplateId() {
51+
return getStringConfigValue("mgs_template_id", "542201");
52+
}
53+
54+
55+
public ContextIntegrationSystem getIntegrationSystem() {
56+
return integrationSystem;
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.avni_integration_service.rwb.config;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class RwbContextProvider {
7+
private static final ThreadLocal<RwbConfig> rwbConfigs = new ThreadLocal<>();
8+
9+
public void set(RwbConfig rwbConfig) {
10+
rwbConfigs.set(rwbConfig);
11+
}
12+
13+
public RwbConfig get() {
14+
RwbConfig rwbConfig = rwbConfigs.get();
15+
if (rwbConfig == null)
16+
throw new IllegalStateException("No Rwb config available. Have you called package org.avni_integration_service.rwb.config.RwbContextProvider.set.");
17+
return rwbConfig;
18+
}
19+
}

rwb/src/main/java/org/avni_integration_service/rwb/job/AvniRwbMainJob.java

+17-20
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,52 @@
33
import com.bugsnag.Bugsnag;
44
import org.apache.log4j.Logger;
55
import org.avni_integration_service.avni.client.AvniHttpClient;
6-
import org.avni_integration_service.integration_data.context.ContextIntegrationSystem;
7-
import org.avni_integration_service.integration_data.context.IntegrationContext;
8-
import org.avni_integration_service.integration_data.domain.IntegrationSystem;
9-
import org.avni_integration_service.integration_data.repository.IntegrationSystemRepository;
106
import org.avni_integration_service.rwb.config.RwbAvniSessionFactory;
7+
import org.avni_integration_service.rwb.config.RwbConfig;
8+
import org.avni_integration_service.rwb.config.RwbContextProvider;
119
import org.avni_integration_service.rwb.worker.RWBUsersNudgeWorker;
1210
import org.avni_integration_service.util.HealthCheckService;
1311
import org.springframework.stereotype.Component;
1412

13+
import static java.lang.String.format;
14+
1515
@Component
1616
public class AvniRwbMainJob {
1717

18+
private static final String EMPTY_STRING = "";
1819
private static final Logger logger = Logger.getLogger(AvniRwbMainJob.class);
1920

20-
private static final String RWB_HEALTHCHECK_SLUG = "rwb";
21-
2221
private final Bugsnag bugsnag;
23-
2422
private final HealthCheckService healthCheckService;
25-
2623
private final RwbAvniSessionFactory rwbAvniSessionFactory;
27-
2824
private final AvniHttpClient avniHttpClient;
29-
25+
private final RwbContextProvider rwbContextProvider;
3026
private final RWBUsersNudgeWorker rwbUsersNudgeWorker;
31-
private final IntegrationSystemRepository integrationSystemRepository;
3227

3328
public AvniRwbMainJob(Bugsnag bugsnag, HealthCheckService healthCheckService,
3429
RwbAvniSessionFactory rwbAvniSessionFactory, AvniHttpClient avniHttpClient,
35-
RWBUsersNudgeWorker rwbUsersNudgeWorker, IntegrationSystemRepository integrationSystemRepository) {
30+
RwbContextProvider rwbContextProvider, RWBUsersNudgeWorker rwbUsersNudgeWorker) {
3631
this.bugsnag = bugsnag;
3732
this.healthCheckService = healthCheckService;
3833
this.rwbAvniSessionFactory = rwbAvniSessionFactory;
3934
this.avniHttpClient = avniHttpClient;
4035
this.rwbUsersNudgeWorker = rwbUsersNudgeWorker;
41-
this.integrationSystemRepository = integrationSystemRepository;
36+
this.rwbContextProvider = rwbContextProvider;
4237
}
4338

44-
public void execute() {
39+
public void execute(RwbConfig rwbConfig) {
40+
String rwbIntegrationSystemName = EMPTY_STRING;
4541
try {
46-
logger.info("Rwb Main Job Started");
42+
rwbIntegrationSystemName = rwbConfig.getIntegrationSystem().getName();
43+
logger.info(format("Rwb Main Job Started: %s", rwbIntegrationSystemName));
44+
rwbContextProvider.set(rwbConfig);
4745
avniHttpClient.setAvniSession(rwbAvniSessionFactory.createSession());
48-
IntegrationContext.set(new ContextIntegrationSystem(integrationSystemRepository.findBySystemTypeAndName(IntegrationSystem.IntegrationSystemType.rwb, "rwb")));
4946
rwbUsersNudgeWorker.processUsers();
50-
healthCheckService.success(RWB_HEALTHCHECK_SLUG);
51-
logger.info("Rwb Main Job Ended");
47+
healthCheckService.success(rwbIntegrationSystemName.toLowerCase());
48+
logger.info(format("Rwb Main Job Ended: %s", rwbIntegrationSystemName));
5249
} catch (Exception e) {
53-
healthCheckService.failure(RWB_HEALTHCHECK_SLUG);
54-
logger.error("Failed", e);
50+
healthCheckService.failure(rwbIntegrationSystemName.toLowerCase());
51+
logger.error(format("Rwb Main Job Errored: %s", rwbIntegrationSystemName), e);
5552
bugsnag.notify(e);
5653
}
5754
}

rwb/src/main/java/org/avni_integration_service/rwb/job/IntegrationTask.java

-22
This file was deleted.

rwb/src/main/java/org/avni_integration_service/rwb/repository/AvniRwbUserNudgeRepository.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import org.avni_integration_service.avni.domain.*;
55
import org.avni_integration_service.avni.repository.AvniMessageRepository;
66
import org.avni_integration_service.avni.repository.AvniQueryRepository;
7+
import org.avni_integration_service.rwb.config.RwbContextProvider;
78
import org.avni_integration_service.rwb.dto.NudgeUserRequestDTO;
89
import org.joda.time.DateTime;
9-
import org.springframework.beans.factory.annotation.Value;
1010
import org.springframework.stereotype.Component;
1111

1212
@Component
@@ -15,13 +15,12 @@ public class AvniRwbUserNudgeRepository {
1515
private static final Logger logger = Logger.getLogger(AvniMessageRepository.class);
1616
private final AvniMessageRepository avniMessageRepository;
1717
private final AvniQueryRepository avniQueryRepository;
18-
private final String nudgeMessageTemplateId;
18+
private final RwbContextProvider rwbContextProvider;
1919

20-
public AvniRwbUserNudgeRepository(AvniMessageRepository avniMessageRepository, AvniQueryRepository avniQueryRepository,
21-
@Value("${rwb.avni.message.template.id}") String nudgeMessageTemplateId) {
20+
public AvniRwbUserNudgeRepository(AvniMessageRepository avniMessageRepository, AvniQueryRepository avniQueryRepository, RwbContextProvider rwbContextProvider) {
2221
this.avniMessageRepository = avniMessageRepository;
2322
this.avniQueryRepository = avniQueryRepository;
24-
this.nudgeMessageTemplateId = nudgeMessageTemplateId;
23+
this.rwbContextProvider = rwbContextProvider;
2524
}
2625

2726
public SendMessageResponse sendMessage(NudgeUserRequestDTO nudgeUserRequestDTO) {
@@ -32,7 +31,7 @@ private ManualMessageContract createMessageRequestToNudgeUser(NudgeUserRequestDT
3231
ManualMessageContract manualMessageContract = new ManualMessageContract();
3332
manualMessageContract.setReceiverId(nudgeUserRequestDTO.getUserId());
3433
manualMessageContract.setReceiverType(ReceiverType.User);
35-
manualMessageContract.setMessageTemplateId(nudgeMessageTemplateId);
34+
manualMessageContract.setMessageTemplateId(rwbContextProvider.get().getMsgTemplateId());
3635
manualMessageContract.setParameters(new String[]{
3736
nudgeUserRequestDTO.getUserName(), nudgeUserRequestDTO.getSinceNoOfDays(), nudgeUserRequestDTO.getWithinNoOfDays()});
3837
manualMessageContract.setScheduledDateTime(new DateTime()); //set current date time

rwb/src/main/java/org/avni_integration_service/rwb/service/RwbUserNudgeErrorService.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.avni_integration_service.integration_data.repository.ErrorRecordRepository;
88
import org.avni_integration_service.integration_data.repository.ErrorTypeRepository;
99
import org.avni_integration_service.integration_data.repository.IntegrationSystemRepository;
10+
import org.avni_integration_service.rwb.config.RwbContextProvider;
1011
import org.avni_integration_service.rwb.config.RwbEntityType;
1112
import org.avni_integration_service.rwb.config.RwbSendMsgErrorType;
1213
import org.springframework.stereotype.Service;
@@ -17,13 +18,15 @@ public class RwbUserNudgeErrorService {
1718
private static final String EMPTY_STRING = "";
1819

1920
private final ErrorRecordRepository errorRecordRepository;
20-
private final IntegrationSystemRepository integrationSystemRepository;
2121
private final ErrorTypeRepository errorTypeRepository;
22+
private final IntegrationSystemRepository integrationSystemRepository;
23+
private final RwbContextProvider rwbContextProvider;
2224

23-
public RwbUserNudgeErrorService(ErrorRecordRepository errorRecordRepository, IntegrationSystemRepository integrationSystemRepository, ErrorTypeRepository errorTypeRepository) {
25+
public RwbUserNudgeErrorService(ErrorRecordRepository errorRecordRepository, ErrorTypeRepository errorTypeRepository, IntegrationSystemRepository integrationSystemRepository, RwbContextProvider rwbContextProvider) {
2426
this.errorRecordRepository = errorRecordRepository;
25-
this.integrationSystemRepository = integrationSystemRepository;
2627
this.errorTypeRepository = errorTypeRepository;
28+
this.integrationSystemRepository = integrationSystemRepository;
29+
this.rwbContextProvider = rwbContextProvider;
2730
}
2831

2932
public void saveUserNudgeSuccess(String userId) {
@@ -59,7 +62,7 @@ private void saveUserNudgeError(String userId, ErrorRecord errorRecord, ErrorTyp
5962
}
6063
} else {
6164
errorRecord = new ErrorRecord();
62-
errorRecord.setIntegrationSystem(integrationSystemRepository.find());
65+
errorRecord.setIntegrationSystem(integrationSystemRepository.findEntity(rwbContextProvider.get().getIntegrationSystem().getId()));
6366
errorRecord.setEntityId(userId);
6467
errorRecord.setIntegratingEntityType(rwbEntityType);
6568
errorRecord.addErrorLog(errorType, errorMsg);
@@ -74,7 +77,7 @@ public ErrorRecord getErrorRecord(String userId) {
7477

7578
private ErrorType getErrorType(RwbSendMsgErrorType rwbSendMsgErrorType) {
7679
String name = rwbSendMsgErrorType.name();
77-
return errorTypeRepository.findByNameAndIntegrationSystem(name, integrationSystemRepository.find());
80+
return errorTypeRepository.findByNameAndIntegrationSystemId(name, rwbContextProvider.get().getIntegrationSystem().getId());
7881
}
7982
}
8083

0 commit comments

Comments
 (0)