package br.eti.archanjo.webcron.quartz.jobs;

import br.eti.archanjo.webcron.configs.PropertiesConfig;
import br.eti.archanjo.webcron.dtos.JobsDTO;
import lombok.Getter;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;


@Getter
public class CommandLineJob implements Job {
    private final Logger logger = LoggerFactory.getLogger(CommandLineJob.class);
    private JobsDTO job;
    private PropertiesConfig.Logging loggingConfig;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        feedJob(context);
        logger.debug(String.format("%s job start to run with command: %s", getJob().getName(), getJob().getCommand()));
        try {
            runCommand();
        } catch (IOException | InterruptedException e) {
            logger.warn("CommandLineJob{execute}", e);
        }
    }

    /**
     * @param context {@link JobExecutionContext}
     */
    private void feedJob(JobExecutionContext context) {
        job = (JobsDTO) context.getMergedJobDataMap().get("data");
        loggingConfig = (PropertiesConfig.Logging) context.getMergedJobDataMap().get("loggingConfig");
    }

    private void runCommand() throws IOException, InterruptedException {
        ProcessBuilder pb = new ProcessBuilder();
        setEnvironments(pb);
        setOutputs(pb);
        pb.command(getJob().getCommand().split("\\s+"));
        Process process = pb.start();
        process.waitFor();
    }

    /**
     * @param pb {@link ProcessBuilder}
     */
    private void setEnvironments(ProcessBuilder pb) {
        if (getJob().getEnvironments() != null)
            getJob().getEnvironments().forEach(p -> pb.environment().put(p.getName(), p.getValue()));
        System.getenv().forEach((k, v) -> pb.environment().put(k, v));
        if (job.getDirectory() == null || job.getDirectory().isEmpty()) {
            pb.directory(new File(System.getProperty("user.home")));
        } else {
            pb.directory(new File(job.getDirectory()));
        }
    }

    /*
     * @param pb {@link ProcessBuilder}
     */
    private void setOutputs(ProcessBuilder pb) throws IOException {
        Path logFolder = Paths.get(getLoggingConfig().getFolder());
        if (Files.notExists(logFolder, LinkOption.NOFOLLOW_LINKS)) {
            Files.createDirectories(logFolder);
            logger.info(String.format("%s folder created", logFolder.toAbsolutePath()));
        }
        Path logPath = Paths.get(getLoggingConfig().getFolder(), String.format("%s-%s-output.log", getJob().getId(), getJob().getName()));
        pb.redirectOutput(logPath.toFile());
        pb.redirectError(logPath.toFile());
    }
}

@@ -2849,22 +2849,54 @@
 redirect
-Output
+Error(ProcessBuilder.Redirect.appendTo
 (logPath
@@ -2901,24 +2901,25 @@
 th.toFile())
+)
 ;%0A        pb
@@ -2927,21 +2927,55 @@
 redirect
-Error
+Output(ProcessBuilder.Redirect.appendTo
 (logPath
@@ -2984,16 +2984,17 @@
 oFile())
+)
 ;%0A    %7D%0A

Appending output