add module 'pseudo-schedulator' for init preparation flow (#13)

This commit is contained in:
Anatoly Karlov 2021-03-30 15:23:00 +07:00 committed by GitHub
parent b68750ccf8
commit 0a7f62766d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 443 additions and 0 deletions

79
pseudo-schedulator/.gitignore vendored Normal file
View File

@ -0,0 +1,79 @@
# Created by .ignore support plugin (hsz.mobi)
.eunit
deps
*.o
*.beam
*.plt
erl_crash.dump
ebin/*.beam
rel/example_project
.concrete/DEV_MODE
.rebar
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff:
.idea/
.idea/workspace.xml
.idea/tasks.xml
.idea/dictionaries
.idea/vcs.xml
.idea/jsLibraryMappings.xml
# Sensitive or high-churn files:
.idea/dataSources.ids
.idea/dataSources.xml
.idea/dataSources.local.xml
.idea/sqlDataSources.xml
.idea/dynamic.xml
.idea/uiDesigner.xml
# Gradle:
.idea/gradle.xml
.idea/libraries
# Mongo Explorer plugin:
.idea/mongoSettings.xml
*.iws
*.ipr
*.iml
# IntelliJ
/out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
*.class
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
env.list

View File

@ -0,0 +1,16 @@
#
# Build stage
#
FROM maven:3.6.0-jdk-11-slim AS build
COPY src /home/pseudo-schedulator/src
COPY pom.xml /home/pseudo-schedulator
ENV POM_XML=/home/pseudo-schedulator/pom.xml
RUN mvn -f $POM_XML clean package
#
# Package stage
#
FROM adoptopenjdk/openjdk11:jdk-11.0.9.1_1-alpine-slim
COPY --from=build /home/pseudo-schedulator/target/pseudo-schedulator-1.0.0-SNAPSHOT.jar /usr/local/lib/pseudo-schedulator.jar
EXPOSE 8081
ENTRYPOINT ["java","-jar","/usr/local/lib/pseudo-schedulator.jar"]

View File

@ -0,0 +1,19 @@
version: '2.1'
services:
pseudo-schedulator:
image: pseudo-schedulator:1.0.0
build: .
container_name: pseudo-schedulator
hostname: pseudo-schedulator
environment:
preparation-flow.scheduler.enabled: "false"
preparation-flow.scheduler.schedule.cron: "0 0 * * * ?"
preparation-flow.ds-provider.mastercard.enabled: "true"
preparation-flow.ds-provider.mastercard.message-version: 2.1.0
preparation-flow.ds-provider.visa.enabled: "true"
preparation-flow.ds-provider.visa.message-version: 2.1.0
preparation-flow.ds-provider.mir.enabled: "true"
preparation-flow.ds-provider.mir.message-version: 2.1.0
client.three-ds-server-storage.url: http://host.docker.internal:8022/three-ds-server-storage/rest/preparation-flow
client.three-ds-server-storage.read-timeout: 5000
client.three-ds-server-storage.connect-timeout: 5000

View File

@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.8.RELEASE</version>
</parent>
<artifactId>pseudo-schedulator</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>pseudo-schedulator</name>
<description>pseudo-schedulator</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
<server.port>8082</server.port>
<exposed.ports>${server.port}</exposed.ports>
<woody.version>1.1.22</woody.version>
</properties>
<dependencies>
<!--rbkmoney-->
<dependency>
<groupId>com.rbkmoney</groupId>
<artifactId>three-ds-server-domain-lib</artifactId>
<version>1.2.3</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,24 @@
package com.example.demo;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import static java.util.Arrays.stream;
@RequiredArgsConstructor
public enum DsProvider {
VISA("visa"),
MASTERCARD("mastercard"),
MIR("mir");
@Getter
private final String id;
public static DsProvider of(String providerId) {
return stream(DsProvider.values())
.filter(p -> p.getId().equals(providerId))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Unknown providerId: " + providerId));
}
}

View File

@ -0,0 +1,82 @@
package com.example.demo;
import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;
@RequiredArgsConstructor
@Slf4j
public class PreparationFlowCronScheduler {
private final boolean isEnabledOnSchedule;
private final PreparationFlowDsProviderProperties visaProperties;
private final PreparationFlowDsProviderProperties mastercardProperties;
private final PreparationFlowDsProviderProperties mirProperties;
private final RestTemplate restTemplate;
private final String url;
@EventListener(value = ApplicationReadyEvent.class)
public void onStartup() {
try {
updatePreparationFlow();
} catch (ResourceAccessException ex) {
log.warn("3DSS Storage connection refused at start,", ex);
}
}
@Scheduled(cron = "${preparation-flow.scheduler.schedule.cron}")
public void onSchedule() {
if (isEnabledOnSchedule) {
updatePreparationFlow();
}
}
private void updatePreparationFlow() {
updateJob(
visaProperties.isEnabled(),
DsProvider.VISA.getId(),
visaProperties.getMessageVersion());
updateJob(
mastercardProperties.isEnabled(),
DsProvider.MASTERCARD.getId(),
mastercardProperties.getMessageVersion());
updateJob(
mirProperties.isEnabled(),
DsProvider.MIR.getId(),
mirProperties.getMessageVersion());
}
private void updateJob(boolean isEnabled, String dsProviderId, String messageVersion) {
if (isEnabled) {
initRBKMoneyPreparationFlow(dsProviderId, messageVersion);
}
}
private void initRBKMoneyPreparationFlow(String dsProviderId, String messageVersion) {
log.info("Init RBKMoney preparation flow, dsProviderId={}", dsProviderId);
PreparationRequest request = PreparationRequest.builder()
.providerId(dsProviderId)
.messageVersion(messageVersion)
.build();
restTemplate.postForEntity(url, request, Void.class);
}
@Data
@Builder
public static class PreparationRequest {
private String providerId;
private String messageVersion;
}
}

View File

@ -0,0 +1,11 @@
package com.example.demo;
import lombok.Data;
@Data
public class PreparationFlowDsProviderProperties {
private boolean enabled;
private String messageVersion;
}

View File

@ -0,0 +1,46 @@
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class PreparationFlowScheduleConfig {
@Bean
public PreparationFlowCronScheduler preparationFlowScheduler(
@Value("${preparation-flow.scheduler.enabled}") boolean isEnabledOnSchedule,
PreparationFlowDsProviderProperties visaPreparationFlowProperties,
PreparationFlowDsProviderProperties mastercardPreparationFlowProperties,
PreparationFlowDsProviderProperties mirPreparationFlowProperties,
RestTemplate restTemplate,
@Value("${client.three-ds-server-storage.url}") String url) {
return new PreparationFlowCronScheduler(
isEnabledOnSchedule,
visaPreparationFlowProperties,
mastercardPreparationFlowProperties,
mirPreparationFlowProperties,
restTemplate,
url);
}
@Bean
@ConfigurationProperties("preparation-flow.ds-provider.visa")
public PreparationFlowDsProviderProperties visaPreparationFlowProperties() {
return new PreparationFlowDsProviderProperties();
}
@Bean
@ConfigurationProperties("preparation-flow.ds-provider.mastercard")
public PreparationFlowDsProviderProperties mastercardPreparationFlowProperties() {
return new PreparationFlowDsProviderProperties();
}
@Bean
@ConfigurationProperties("preparation-flow.ds-provider.mir")
public PreparationFlowDsProviderProperties mirPreparationFlowProperties() {
return new PreparationFlowDsProviderProperties();
}
}

View File

@ -0,0 +1,15 @@
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class PseudoSchedulatorApplication {
public static void main(String[] args) {
SpringApplication.run(PseudoSchedulatorApplication.class, args);
}
}

View File

@ -0,0 +1,28 @@
package com.example.demo;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import java.time.Duration;
@Configuration
@Slf4j
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(
@Value("${client.three-ds-server-storage.read-timeout}") int readTimeout,
@Value("${client.three-ds-server-storage.connect-timeout}") int connectTimeout) {
return new RestTemplateBuilder()
.requestFactory(() -> new HttpComponentsClientHttpRequestFactory(HttpClients.createDefault()))
.setConnectTimeout(Duration.ofMillis(connectTimeout))
.setReadTimeout(Duration.ofMillis(readTimeout))
.build();
}
}

View File

@ -0,0 +1,48 @@
spring:
application:
name: '@name@'
description: '@description@'
output:
ansi:
enabled: always
management:
endpoint:
health:
show-details: always
security:
flag: false
metrics:
export:
statsd:
flavor: etsy
enabled: false
info:
version: '@version@'
stage: dev
server:
port: '@server.port@'
preparation-flow:
scheduler:
enabled: false
schedule:
cron: "0 0 * * * ?"
ds-provider:
mastercard:
enabled: true
message-version: "2.1.0"
visa:
enabled: false
message-version: "2.1.0"
mir:
enabled: false
message-version: "2.1.0"
client:
three-ds-server-storage:
url: http://localhost:8022/three-ds-server-storage/rest/preparation-flow
read-timeout: 5000
connect-timeout: 5000