0

I'm getting this exception when executing a microservice (a part of an application I'm using from Internet).

    2022-06-03 16:13:46.770  INFO 12804 --- [   scheduling-1] i.u.d.d.s.t.e.i.TimeSeriesTrainingEngine : Python est introuvable. Ex�cutez sans argument pour proc�der � l
2022-06-03 16:13:46.778 ERROR 12804 --- [   scheduling-1] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task

java.lang.RuntimeException: Error during script calling
    at it.univaq.disim.discovery.servicemonitoring.training.engine.impl.TimeSeriesTrainingEngine.generateModel(TimeSeriesTrainingEngine.java:88) ~[classes/:na]
    at it.univaq.disim.discovery.servicemonitoring.training.scheduler.TrainingScheduler.generateModel(TrainingScheduler.java:35) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[na:na]
    at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305) ~[na:na]
    at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
    at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Python': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
 at [Source: (String)"Python est introuvable. Ex�cutez sans argument pour proc�der � l"; line: 1, column: 7]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1840) ~[jackson-core-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:722) ~[jackson-core-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._reportInvalidToken(ReaderBasedJsonParser.java:2868) ~[jackson-core-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleOddValue(ReaderBasedJsonParser.java:1914) ~[jackson-core-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:773) ~[jackson-core-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4340) ~[jackson-databind-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4189) ~[jackson-databind-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3205) ~[jackson-databind-2.10.2.jar:2.10.2]
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3188) ~[jackson-databind-2.10.2.jar:2.10.2]
    at it.univaq.disim.discovery.servicemonitoring.training.engine.impl.TimeSeriesTrainingEngine.generateModel(TimeSeriesTrainingEngine.java:56) ~[classes/:na]
    ... 13 common frames omitted

The microservice is calling a python script, and this is the class where the exception occured:

@Slf4j
@Service("time-series")
public class TimeSeriesTrainingEngine implements TrainingEngine {

    @Autowired
    private MonitoringDataRepository monitoringDataRepository;

    @Override
    public TrainingData generateModel() {
        List<MonitoringData> allMonitoringData = monitoringDataRepository.findAll();
        File inputFile = new File("temp/time-series-input.json");
        try {

            Utils.writeObjToFile(inputFile, allMonitoringData);
            log.info("inputFile = "+allMonitoringData);
            String scriptPath = new File("ml-scripts/time-series-training.py").getPath();

            /* Create the ProcessBuilder */
            ProcessBuilder pb = new ProcessBuilder("python", "-u", scriptPath, inputFile.getAbsolutePath());
            pb.redirectErrorStream(true);

            /* Start the process */
            Process proc = pb.start();

            /* Read the process's output */
            List<String> outputLines = Utils.toStrings(proc.getInputStream());
            /* Clean-up */
            proc.destroy();
            log.info(String.join("\n", outputLines));

            ObjectMapper jackson = new ObjectMapper();
            **List<String> filesName = jackson.readValue(outputLines.get(outputLines.size() - 1), new TypeReference<List<String>>() {
            });**

            // create input file
            List<String> serviceInstancesUrl = allMonitoringData.stream()
                    .map(MonitoringData::getServiceInstance)
                    .distinct()
                    .collect(Collectors.toList());

            ObjectMapper mapper = new ObjectMapper();
            List<String> inputNames = serviceInstancesUrl.stream()
                    .map(serviceInstance -> {
                        List<MonitoringData> monitoringData = monitoringDataRepository.findByServiceInstance(serviceInstance, PageRequest.of(0, 10));
                        try {
                            String monitoringDataJson = mapper.writeValueAsString(monitoringData);
                            String instanceSanitize = serviceInstance.substring(7).replaceAll(":", ".");
                            String fileName = "temp/" + instanceSanitize + ".input.json";
                            FileUtils.writeStringToFile(new File(fileName), monitoringDataJson, Charset.defaultCharset());

                            return fileName;
                        } catch (IOException e) {
                            log.error(e.getMessage(), e);
                            return null;
                        }
                    })
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());

            filesName.addAll(inputNames);
            return new TrainingData(Utils.zip(filesName));

        } catch (IOException e) {
            throw new RuntimeException("Error during script calling", e);
        } finally {
            inputFile.delete();
        }
    }
}

This is the line 56 in the class "TimeSeriesTrainingEngine":

List<String> filesName = jackson.readValue(outputLines.get(outputLines.size() - 1), new TypeReference<List<String>>() {
                });

Thanks in advance.

Abra
  • 19,142
  • 7
  • 29
  • 41
TarekTT
  • 11
  • 2
  • I think you need to add a call to method [waitFor](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Process.html#waitFor()) (of class `java.lang.Process`). I think you are `destroy`ing the process before it has terminated. – Abra Jun 03 '22 at 16:58
  • 1
    You're hitting [the exception in this question](https://stackoverflow.com/questions/65348890/python-was-not-found-run-without-arguments-to-install-from-the-microsoft-store). – Luke Woodward Jun 03 '22 at 18:21
  • @LukeWoodward exactly, the error message is `Python est introuvable. Ex�cutez sans argument pour proc�der � l` which translates from French to: `Python could not be found. Execute with no arguments to proceed with the` (google translate). Indicating that the process `python` is not correctly installed. – slindenau Jun 06 '22 at 13:35
  • indeed, this is correct, it was a problem of python that wasn't correctly installed. Many thanks for everyone – TarekTT Jun 07 '22 at 14:04

0 Answers0