rasa:把shell脚本集成到resources目录下

dev_v1.0.1
xueqingkun 2 years ago
parent 522f02bcad
commit f081e91bdc

@ -4,4 +4,10 @@ public class RasaConstant {
public static final String TRAN_SUCCESS_MESSAGE = "Your Rasa model is trained and saved at";
public static final String RUN_SUCCESS_MESSAGE = "Rasa server is up and running";
public static final String RUN_SHELL = "run.sh";
public static final String TRAIN_SHELL = "train.sh";
public static final String KILL_SHELL = "kill.sh";
}

@ -17,4 +17,6 @@ public interface RasaCmdService {
List<String> execCmd(List<String> cmds, Predicate<String> endPredicate, long timeOut) throws InterruptedException, ExecutionException, TimeoutException;
String getShellPath(String shell);
}

@ -42,7 +42,9 @@ public class RasaModelManager {
for (RasaModelInfo rasaModelInfo : activeRasaList) {
if (!PortUtil.portIsActive(rasaModelInfo.getPort())){
try {
rasaModelInfo.getRunCmd().add(String.valueOf(rasaModelInfo.getPort()));
List<String> runCmd = rasaModelInfo.getRunCmd();
runCmd.add(String.valueOf(rasaModelInfo.getPort()));
runCmd.set(1,rasaCmdService.getShellPath(RasaConstant.RUN_SHELL));
List<String> outMessageList = rasaCmdService.execCmd(rasaModelInfo.getRunCmd(),
s -> StrUtil.isNotBlank(s) && s.contains(RasaConstant.RUN_SUCCESS_MESSAGE), 300);

@ -2,7 +2,9 @@ package com.supervision.rasa.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.supervision.exception.BusinessException;
import com.supervision.model.RasaModelInfo;
import com.supervision.rasa.config.ThreadPoolExecutorConfig;
import com.supervision.rasa.constant.RasaConstant;
@ -13,6 +15,7 @@ import com.supervision.service.RasaModeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -43,14 +46,6 @@ public class RasaCmdServiceImpl implements RasaCmdService {
@Value("${rasa.shell-env:/bin/bash}")
private String shellEnv;
@Value("${rasa.train-shell}")
private String trainShell;
@Value("${rasa.run-shell}")
private String runShell;
@Value("${rasa.kill-shell}")
private String killShell;
@Value("${rasa.shell.work:/data/vp/rasa_shell/}")
private String shellWork;
@ -73,7 +68,7 @@ public class RasaCmdServiceImpl implements RasaCmdService {
// /rasa/models 生成出来的模型的存放路径,也写在配置文件里面
String localModelsPath = replaceDuplicateSeparator(String.join(File.separator,modelsPath,argument.getModelId()));
List<String> cmds = ListUtil.toList(shellEnv, trainShell,config,localDataPath,domain,localModelsPath);
List<String> cmds = ListUtil.toList(shellEnv, getShellPath(RasaConstant.TRAIN_SHELL),config,localDataPath,domain,localModelsPath);
cmds.add(argument.getFixedModelName());
@ -87,6 +82,7 @@ public class RasaCmdServiceImpl implements RasaCmdService {
rasaModelInfo.setModelId(argument.getModelId());
rasaModelInfo.setTranStatus(trainIsSuccess(outMessage)?1:0);
rasaModelInfo.setServerStatus(-1);
cmds.set(1,null);
rasaModelInfo.setTrainCmd(cmds);
rasaModelInfo.setTrainLog(outMessageString);
rasaModeService.saveOrUpdateByModelId(rasaModelInfo);
@ -117,7 +113,7 @@ public class RasaCmdServiceImpl implements RasaCmdService {
fixedModePath = String.join(modeParentPath,fixedModelName);
}
List<String> cmds = ListUtil.toList(shellEnv, runShell,fixedModePath,endpoints,String.valueOf(port));
List<String> cmds = ListUtil.toList(shellEnv, getShellPath(RasaConstant.RUN_SHELL),fixedModePath,endpoints,String.valueOf(port));
log.info("runExec cmd : {}",StrUtil.join(" ",cmds));
@ -131,7 +127,7 @@ public class RasaCmdServiceImpl implements RasaCmdService {
if (null != dbRasaModelInfo.getPort() && runIsSuccess){
log.info("runExec server is up and dbRasaModelInfo is: {}, start kill old server ....",dbRasaModelInfo.getPort());
// 3.1 如果原本的程序已经运行且本次成功启动,杀掉原本的进程
execCmd(ListUtil.toList(runShell, killShell, String.valueOf(dbRasaModelInfo.getPort())), s -> false, 20);
execCmd(ListUtil.toList(shellEnv, getShellPath(RasaConstant.KILL_SHELL), String.valueOf(dbRasaModelInfo.getPort())), s -> false, 20);
}
if (null != dbRasaModelInfo.getPort() && !runIsSuccess){
// 3.2 如果原本的程序已经运行,但本次程序未正常启动,不对端口号进行变动
@ -142,8 +138,8 @@ public class RasaCmdServiceImpl implements RasaCmdService {
RasaModelInfo rasaModelInfo = new RasaModelInfo();
rasaModelInfo.setModelId(argument.getModelId());
rasaModelInfo.setPort(port);
rasaModelInfo.setServerStatus( runIsSuccess ? 1:0);
rasaModelInfo.setRunCmd(ListUtil.toList(shellEnv, runShell,fixedModePath,endpoints));
rasaModelInfo.setServerStatus( runIsSuccess ? 1 : 0 );
rasaModelInfo.setRunCmd(ListUtil.toList(shellEnv, null,fixedModePath,endpoints));
rasaModelInfo.setRunLog(outMessageString);
rasaModeService.saveOrUpdateByModelId(rasaModelInfo);
@ -234,4 +230,24 @@ public class RasaCmdServiceImpl implements RasaCmdService {
}
public String getShellPath(String shell){
if (StrUtil.isEmpty(shell)){
throw new BusinessException("shell 不能为空...");
}
// 获取 shell 脚本的路径
String shellPath = "shellScript/" + shell;
ClassPathResource shellScript = new ClassPathResource(shellPath);
try {
InputStream inputStream = shellScript.getInputStream();
File tempFile = File.createTempFile(shell, ".sh");
tempFile.deleteOnExit();
FileUtil.writeFromStream(inputStream, tempFile);
return tempFile.getAbsolutePath();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

@ -16,9 +16,6 @@ rasa:
models-path: /data/vp/rasa/models/
endpoints: /rasa/endpoints.yml # 启动的配置项,应该是写在配置文件里面
config: /rasa/config-local.yml # 启动rasa需要的配置文件在配置文件中配置
train-shell: /data/vp/rasa_shell/train.sh
run-shell: /data/vp/rasa_shell/run.sh
kill-shell: /data/vp/rasa_shell/kill.sh
url: 127.0.0.1:{}/webhooks/rest/webhook
wakeup:
cron: 0 */10 * * * ? #每十分钟执行一次

@ -0,0 +1,14 @@
!/usr/bin/env bash
PORT=$1
KPID=$(lsof -t -i:$PORT)
if [ -n "$KPID" ]; then
echo "PORT ===>> $PORT map PID is not empty. pid is $KPID "
echo "cmd is kill -9 $KPID "
kill -9 $KPID
else
echo "PORT ===>> $PORT map PID is empty."
fi

@ -0,0 +1,16 @@
#!/usr/bin/env bash
source /root/anaconda3/etc/profile.d/conda.sh
conda activate rasa3
MODELS_PATH=$1
ENDPOINTS=$2
PORT=$3
echo "shell cmd is rasa run -m $MODELS_PATH --enable-api --cors "*" --debug --endpoints $ENDPOINTS --port $PORT "
rasa run -m $MODELS_PATH --enable-api --cors "*" --debug --endpoints $ENDPOINTS --port $PORT
echo 'start success ...'

@ -0,0 +1,21 @@
#!/usr/bin/env bash
source /root/anaconda3/etc/profile.d/conda.sh
conda activate rasa3
CONFIG=$1
DATA=$2
DOMAIN=$3
OUT=$4
FIXED_MODEL_NAME=$5
echo "tran shell is run..."
echo "cmd is rasa train --config $CONFIG --data $DATA --domain $DOMAIN --out $OUT --fixed-model-name $FIXED_MODEL_NAME"
rasa train --config $CONFIG --data $DATA --domain $DOMAIN --out $OUT --fixed-model-name $FIXED_MODEL_NAME
echo 'train done'
Loading…
Cancel
Save