diff --git a/.gitignore b/.gitignore index 7d790fc..d84c6ce 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,6 @@ pom.xml *.iml /lib /rrd +.project +.classpath +.settings \ No newline at end of file diff --git a/VERSION b/VERSION index 4b9fcbe..a918a2a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.5.1 +0.6.0 diff --git a/project.clj b/project.clj index 2d8281b..629ce65 100644 --- a/project.clj +++ b/project.clj @@ -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"]]}}) diff --git a/src/clj/job_streamer/control_bus/component/jobs.clj b/src/clj/job_streamer/control_bus/component/jobs.clj index 5838c93..0e9108f 100644 --- a/src/clj/job_streamer/control_bus/component/jobs.clj +++ b/src/clj/job_streamer/control_bus/component/jobs.clj @@ -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]])) diff --git a/src/clj/job_streamer/control_bus/config.clj b/src/clj/job_streamer/control_bus/config.clj index 9ec4184..c5e0180 100644 --- a/src/clj/job_streamer/control_bus/config.clj +++ b/src/clj/job_streamer/control_bus/config.clj @@ -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}})) diff --git a/src/clj/job_streamer/control_bus/util.clj b/src/clj/job_streamer/control_bus/util.clj index 89e6262..1a8b706 100644 --- a/src/clj/job_streamer/control_bus/util.clj +++ b/src/clj/job_streamer/control_bus/util.clj @@ -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] @@ -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)}] @@ -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) diff --git a/src/java/org/jobstreamer/batch/ShellBatchlet.java b/src/java/org/jobstreamer/batch/ShellBatchlet.java index 7f8636d..3b27aca 100644 --- a/src/java/org/jobstreamer/batch/ShellBatchlet.java +++ b/src/java/org/jobstreamer/batch/ShellBatchlet.java @@ -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; @@ -18,6 +10,18 @@ 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. @@ -25,7 +29,7 @@ * @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 @@ -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 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 { @@ -60,6 +72,8 @@ private String executeScript(Path script) { logger.info(line); } } + } catch (Throwable e) { + e.printStackTrace(); } finally { if (process != null) { try { diff --git a/test/net/unit8/job_streamer/control_bus/HolidayTest.java b/test/java/net/unit8/job_streamer/control_bus/HolidayTest.java similarity index 100% rename from test/net/unit8/job_streamer/control_bus/HolidayTest.java rename to test/java/net/unit8/job_streamer/control_bus/HolidayTest.java diff --git a/test/java/org/jobstreamer/batch/ShellBatchletTest.java b/test/java/org/jobstreamer/batch/ShellBatchletTest.java new file mode 100644 index 0000000..4477006 --- /dev/null +++ b/test/java/org/jobstreamer/batch/ShellBatchletTest.java @@ -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); + } +} diff --git a/test/resources/test b/test/resources/test new file mode 100755 index 0000000..283fd07 --- /dev/null +++ b/test/resources/test @@ -0,0 +1,2 @@ +echo $1 +echo $2 diff --git a/test/resources/test.bat b/test/resources/test.bat new file mode 100755 index 0000000..365fbb4 --- /dev/null +++ b/test/resources/test.bat @@ -0,0 +1,2 @@ +echo %1 +echo %2