Commit 24e3f5e8 authored by liaozan's avatar liaozan 🏀

feat: make the warm up logic run after init

parent c19c8bf1
package com.schbrain.archetype.initializer.listener;
import com.schbrain.archetype.initializer.maven.MavenUtils;
import com.schbrain.archetype.initializer.param.ArchetypeGenerateParam;
import com.schbrain.common.util.EnvUtils;
import lombok.SneakyThrows;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
/**
* @author liaozan
* @since 2022/3/20
*/
@Component
public class ProjectWarmUpListener implements ApplicationListener<ApplicationStartedEvent> {
private final Executor executor = Executors.newSingleThreadExecutor();
@SneakyThrows
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
if (!EnvUtils.runningOnCloudPlatform(event.getApplicationContext().getEnvironment())) {
return;
}
executor.execute(() -> {
ArchetypeGenerateParam mockParam = new ArchetypeGenerateParam();
mockParam.setGroupId("com.schbrain");
mockParam.setArtifactId("schbrain-init");
MavenUtils.generate(mockParam);
});
}
}
\ No newline at end of file
...@@ -4,7 +4,9 @@ import cn.hutool.core.io.FileUtil; ...@@ -4,7 +4,9 @@ import cn.hutool.core.io.FileUtil;
import cn.hutool.system.SystemUtil; import cn.hutool.system.SystemUtil;
import com.schbrain.archetype.initializer.config.properties.ArchetypeProperties; import com.schbrain.archetype.initializer.config.properties.ArchetypeProperties;
import com.schbrain.archetype.initializer.maven.MavenUtils; import com.schbrain.archetype.initializer.maven.MavenUtils;
import com.schbrain.archetype.initializer.param.ArchetypeGenerateParam;
import com.schbrain.common.exception.BaseException; import com.schbrain.common.exception.BaseException;
import com.schbrain.common.util.EnvUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
...@@ -13,6 +15,8 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -13,6 +15,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.File; import java.io.File;
...@@ -26,12 +30,19 @@ import java.io.File; ...@@ -26,12 +30,19 @@ import java.io.File;
@EnableConfigurationProperties(ArchetypeProperties.class) @EnableConfigurationProperties(ArchetypeProperties.class)
public class ArchetypePreparer implements ApplicationRunner { public class ArchetypePreparer implements ApplicationRunner {
@Autowired
private Environment environment;
@Autowired @Autowired
private ArchetypeProperties archetypeProperties; private ArchetypeProperties archetypeProperties;
@Autowired
private ThreadPoolTaskExecutor taskExecutor;
@Override @Override
public void run(ApplicationArguments args) { public void run(ApplicationArguments args) {
installArchetype(); taskExecutor.execute(() -> {
installArchetype();
warmUpIfRequired();
});
} }
public void installArchetype() { public void installArchetype() {
...@@ -40,6 +51,16 @@ public class ArchetypePreparer implements ApplicationRunner { ...@@ -40,6 +51,16 @@ public class ArchetypePreparer implements ApplicationRunner {
MavenUtils.install(archetypeTemplateDirectory.getAbsolutePath()); MavenUtils.install(archetypeTemplateDirectory.getAbsolutePath());
} }
private void warmUpIfRequired() {
if (!EnvUtils.runningOnCloudPlatform(environment)) {
return;
}
ArchetypeGenerateParam mockParam = new ArchetypeGenerateParam();
mockParam.setGroupId("com.schbrain");
mockParam.setArtifactId("schbrain-init");
MavenUtils.generate(mockParam);
}
private File createArchetypeDirectory() { private File createArchetypeDirectory() {
String tempDir = SystemUtil.getUserInfo().getTempDir(); String tempDir = SystemUtil.getUserInfo().getTempDir();
File archetypeTemplateDirectory = new File(tempDir, "archetype"); File archetypeTemplateDirectory = new File(tempDir, "archetype");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment