Skip to content

Commit

Permalink
Merge develop branch
Browse files Browse the repository at this point in the history
  • Loading branch information
kawasima committed Aug 30, 2016
2 parents 2e196ff + 3c61f85 commit ee3bae4
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ pom.xml
*.iml
/lib
/rrd
.project
.classpath
.settings
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.1
0.6.0
5 changes: 4 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,7 @@
:source-paths ["dev"]
:repl-options {:init-ns user}
:env {:port "45102"}}
:project/test {:dependencies [[junit "4.12"]]}})
:project/test {:dependencies [[junit "4.12"]
[org.mockito/mockito-all "1.10.19"]
[org.powermock/powermock-api-mockito "1.6.4"]
[org.powermock/powermock-module-junit4 "1.6.4"]]}})
2 changes: 1 addition & 1 deletion src/clj/job_streamer/control_bus/component/jobs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
:put! (fn [{job :edn job-id :job-id}]
(d/transact datomic (edn->datoms job job-id)))
:delete! (fn [{job-id :job-id app-id :app-id}]
(scheduler/unschedule job-id)
(scheduler/unschedule scheduler job-id)
(d/transact datomic
[[:db.fn/retractEntity job-id]
[:db/retract app-id :application/jobs job-id]]))
Expand Down
7 changes: 5 additions & 2 deletions src/clj/job_streamer/control_bus/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
:migration {:dbschema model/dbschema}})

(def environ
{:http {:port (some-> env :control_bus-port Integer.)}
:discoverer {:ws-port (some-> env :control_bus-port Integer.)}})
(let [port (some-> env :control-bus-port Integer.)]
{:http {:port port}
:discoverer {:ws-port port}
:scheduler {:host "localhost"
:port port}}))

7 changes: 4 additions & 3 deletions src/clj/job_streamer/control_bus/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:require [clojure.tools.logging :as log]
[clojure.edn :as edn]
[clojure.java.io :as io]
[datomic.api :as d])
[datomic.api :as d]
[ring.util.request :refer [content-type]])
(:import [org.jsoup Jsoup]))

(defn to-int [n default-value]
Expand Down Expand Up @@ -34,7 +35,7 @@
job-id (or job-id (d/tempid :db.part/user)) ]
(concat [{:db/id job-id
:job/name (:job/name job)
:job/restartable? (get job :job/restartable? true)
:job/restartable? (get job :job/restartable? true)
:job/edn-notation (pr-str job)
:job/steps step-refs
:job/exclusive? (get job :job/exclusive? false)}]
Expand Down Expand Up @@ -129,7 +130,7 @@
(when (#{:put :post} (get-in context [:request :request-method]))
(try
(if-let [body (body-as-string context)]
(case (get-in context [:request :content-type])
(case (or (content-type (:request context)) (get-in context [:request :content-type]))
"application/edn" [false {:edn (edn/read-string body)}]
"application/xml" [false {:edn (xml->edn body)}]
false)
Expand Down
36 changes: 25 additions & 11 deletions src/java/org/jobstreamer/batch/ShellBatchlet.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package org.jobstreamer.batch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.batch.api.AbstractBatchlet;
import javax.batch.operations.BatchRuntimeException;
import javax.batch.runtime.context.StepContext;
import javax.enterprise.inject.Any;
import javax.inject.Inject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -18,14 +10,26 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.batch.api.AbstractBatchlet;
import javax.batch.operations.BatchRuntimeException;
import javax.batch.runtime.context.StepContext;
import javax.enterprise.inject.Any;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A builtin batchlet for running a shellscript.
*
* @author kawasima
*/
public class ShellBatchlet extends AbstractBatchlet {
private static final Logger logger = LoggerFactory.getLogger(ShellBatchlet.class);
private final static Logger logger = LoggerFactory.getLogger(ShellBatchlet.class);
private Process process;

@Any
Expand All @@ -46,8 +50,16 @@ private Path createTemporaryScript(URL resourceUrl, String script) throws IOExce
return scriptFile;
}

private String executeScript(Path script) {
ProcessBuilder pb = new ProcessBuilder(script.toAbsolutePath().toString());
String executeScript(Path script) {
String args = stepContext.getProperties().getProperty("args");
String processToString = script.toAbsolutePath().toString();
List<String> processAndArgs = new ArrayList();
processAndArgs.add(processToString);

if (args != null && !args.isEmpty()) {
processAndArgs.addAll(Arrays.asList(args.split("\\s+")));
}
ProcessBuilder pb = new ProcessBuilder(processAndArgs);
pb.redirectErrorStream(true);

try {
Expand All @@ -60,6 +72,8 @@ private String executeScript(Path script) {
logger.info(line);
}
}
} catch (Throwable e) {
e.printStackTrace();
} finally {
if (process != null) {
try {
Expand Down
81 changes: 81 additions & 0 deletions test/java/org/jobstreamer/batch/ShellBatchletTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.jobstreamer.batch;

import static org.mockito.Mockito.*;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.file.Paths;
import java.util.Properties;

import javax.batch.runtime.context.StepContext;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import ch.qos.logback.classic.Logger;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ShellBatchlet.class,Logger.class})
public class ShellBatchletTest {
private static final String BAT_FILE_PATH = "test/resources/test.bat";
private static final String SHELL_FILE_PATH = "test/resources/test";
private static final String TEST_MESSAGE = "test";
private final static ShellBatchlet TARGET = new ShellBatchlet();

private StepContext sc = mock (StepContext.class);
private Properties properties = new Properties();
private Logger logger = Mockito.mock(Logger.class);
private boolean isWindows = System.getProperty("os.name").startsWith("Windows");
private String fileName;

@Before
public void setup() throws Throwable{
properties = new Properties();
Field f= TARGET.getClass().getDeclaredField("stepContext");
f.set(TARGET, sc);
setFinalStatic(ShellBatchlet.class.getDeclaredField("logger"),logger);
fileName =isWindows ? BAT_FILE_PATH : SHELL_FILE_PATH;

}

@Test
public void args0() throws Throwable{
when(sc.getProperties()).thenReturn(properties);
TARGET.executeScript(Paths.get(fileName));
verify(logger,never()).info(TEST_MESSAGE);




}

@Test
public void args1() throws Throwable{
properties.setProperty("args", TEST_MESSAGE);
when(sc.getProperties()).thenReturn(properties);
TARGET.executeScript(Paths.get(fileName));

verify(logger,times(1)).info(TEST_MESSAGE);
}

@Test
public void args2() throws Throwable{
properties.setProperty("args", TEST_MESSAGE + " " + TEST_MESSAGE);
when(sc.getProperties()).thenReturn(properties);
TARGET.executeScript(Paths.get(fileName));

verify(logger,times(2)).info(TEST_MESSAGE);
}

static void setFinalStatic(Field field, Object newValue) throws Exception {
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
}
}
2 changes: 2 additions & 0 deletions test/resources/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
echo $1
echo $2
2 changes: 2 additions & 0 deletions test/resources/test.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
echo %1
echo %2

0 comments on commit ee3bae4

Please sign in to comment.