Skip to content

Commit

Permalink
buerokratt#22 missing java classes from GH merge
Browse files Browse the repository at this point in the history
  • Loading branch information
RayDNoper committed May 30, 2024
1 parent c43a156 commit eacabe7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

import ee.buerokratt.cronmanager.services.CronService;
import ee.buerokratt.cronmanager.services.JobReaderService;
import jakarta.servlet.http.HttpServletRequest;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class CronController {

Expand Down Expand Up @@ -56,9 +63,11 @@ public String runningJobs(@PathVariable(required = false) String groupName) {

@PostMapping(value = "/execute/{groupName}/{jobName}",
produces = MediaType.APPLICATION_JSON_VALUE)
public String executeJob(@PathVariable String groupName, @PathVariable String jobName) {
public String executeJob(@PathVariable String groupName,
@PathVariable String jobName,
HttpServletRequest request) {
try {
return cron.executeJob(groupName, jobName);
return cron.executeJob(groupName, jobName, request.getParameterMap());
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/ee/buerokratt/cronmanager/model/ShellExecuteJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
import org.quartz.JobExecutionException;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static ee.buerokratt.cronmanager.utils.LoggingUtils.mapDeepToString;
import static org.apache.logging.log4j.message.ParameterizedMessage.deepToString;

@Slf4j
@Getter
@Setter
Expand All @@ -20,6 +25,8 @@ public class ShellExecuteJob extends YamlJob {

private String command;

private List<String> allowedEnvs;

@Override
public String getType() {
return "exec";
Expand All @@ -34,19 +41,22 @@ public void execute(JobExecutionContext context) throws JobExecutionException {
throw new JobExecutionException("Stopped execution: current time outside defined limits: %d [ %d -> %d ]".formatted(System.currentTimeMillis(), getStartDate(), getEndDate()));
}

command = context.getJobDetail().getJobDataMap().getString("command");
JobDataMap jdm = context.getJobDetail().getJobDataMap();
command = jdm.getString("command");

try {
List<String> result = execProcess(command);
String params = jdm.containsKey("params") ? (String) jdm.get("params") : "empty";
List<String> result = execProcess(command, params);
log.info(result.stream().collect(Collectors.joining("\n")));
} catch (IOException e) {
throw new JobExecutionException("Problem running command: ", e);
}
}

private List<String> execProcess(String command) throws IOException {
private List<String> execProcess(String command, String params) throws IOException {
log.debug("Running "+command + "("+params+")");
File dir = new File("/app/");
Process process = Runtime.getRuntime().exec(command, null, dir);
Process process = Runtime.getRuntime().exec(command, params.split(","), dir);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
return reader.lines().collect(Collectors.toList());
}
Expand All @@ -55,6 +65,8 @@ private List<String> execProcess(String command) throws IOException {
public JobDataMap getJobData() {
JobDataMap map = super.getJobData();
map.put("command", command);
if (allowedEnvs != null)
map.put("allowedEnvs", String.join(",", allowedEnvs));
return map;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ee.buerokratt.cronmanager.services;

import com.fasterxml.jackson.databind.ObjectMapper;
import ee.buerokratt.cronmanager.model.ShellExecuteJob;
import ee.buerokratt.cronmanager.model.YamlJob;
import ee.buerokratt.cronmanager.utils.LoggingUtils;
import lombok.AllArgsConstructor;
Expand All @@ -14,6 +15,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.apache.logging.log4j.message.ParameterizedMessage.deepToString;

@Slf4j
@Service
@AllArgsConstructor
Expand Down Expand Up @@ -114,11 +117,29 @@ public String getRunningJobs(String groupName) throws SchedulerException {
return jobsToJson(jobs.collect(Collectors.toList()));
}

public String executeJob(String groupName, String jobName) throws SchedulerException {
public String executeJob(String groupName, String jobName,
Map<String, String[]> params) throws SchedulerException {
GroupMatcher<JobKey> matcher = groupName == null || groupName.isEmpty() ? GroupMatcher.anyGroup() : GroupMatcher.groupEquals(groupName);
log.debug("Params: "+deepToString(params));
JobKey job = scheduler.getJobKeys(matcher).stream()
.filter(key -> key.getName().equals(jobName)).findFirst().orElseGet(null);
JobDetail jobDetail = scheduler.getJobDetail(job);
JobDataMap jdm = jobDetail.getJobDataMap();

if (jobDetail.getJobClass() == ShellExecuteJob.class && jdm.containsKey("allowedEnvs")) {
List<String> allowedParams = Arrays.asList(((String)jobDetail.getJobDataMap().get("allowedEnvs")).split(","));
String env = params.entrySet().stream()
.filter(entry -> allowedParams.contains(entry.getKey()))
.map(entry-> entry.getKey() + "=" + entry.getValue()[0])
.collect(Collectors.joining(","));
jobDetail.getJobDataMap().put("params", env);
log.debug("Env: "+deepToString(env) + " stored: "+ jobDetail.getJobDataMap().get("params"));
scheduler.addJob(jobDetail, true);
}


scheduler.triggerJob(job);

return getRunningJobs(groupName);
}

Expand Down

0 comments on commit eacabe7

Please sign in to comment.