You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@batchee.apache.org by rm...@apache.org on 2014/01/02 20:59:41 UTC

git commit: BATCHEE-11 basic cli - start and restart command

Updated Branches:
  refs/heads/master b5fe846f7 -> 9459ce388


BATCHEE-11 basic cli - start and restart command


Project: http://git-wip-us.apache.org/repos/asf/incubator-batchee/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-batchee/commit/9459ce38
Tree: http://git-wip-us.apache.org/repos/asf/incubator-batchee/tree/9459ce38
Diff: http://git-wip-us.apache.org/repos/asf/incubator-batchee/diff/9459ce38

Branch: refs/heads/master
Commit: 9459ce3886e5ecf019fb97d14af1caea41322a16
Parents: b5fe846
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Thu Jan 2 20:59:24 2014 +0100
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Thu Jan 2 20:59:24 2014 +0100

----------------------------------------------------------------------
 cli/pom.xml                                     |  64 ++++++++++++
 .../java/org/apache/batchee/cli/BatchEECLI.java |  44 ++++++++
 .../batchee/cli/command/JobOperatorCommand.java |  26 +++++
 .../org/apache/batchee/cli/command/Restart.java |  35 +++++++
 .../org/apache/batchee/cli/command/Start.java   |  35 +++++++
 .../batchee/cli/command/StartableCommand.java   | 104 +++++++++++++++++++
 cli/src/main/resources/META-INF/NOTICE.txt      |   4 +
 .../java/org/apache/batchee/cli/MainTest.java   |  59 +++++++++++
 .../apache/batchee/cli/component/Sample.java    |  35 +++++++
 .../resources/META-INF/batch-jobs/sample.xml    |  24 +++++
 pom.xml                                         |   1 +
 11 files changed, 431 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/9459ce38/cli/pom.xml
----------------------------------------------------------------------
diff --git a/cli/pom.xml b/cli/pom.xml
new file mode 100644
index 0000000..681c1f1
--- /dev/null
+++ b/cli/pom.xml
@@ -0,0 +1,64 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <artifactId>batchee-parent</artifactId>
+    <groupId>org.apache.batchee</groupId>
+    <version>0.1-incubating-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>batchee-cli</artifactId>
+  <name>BatchEE :: CLI</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.geronimo.specs</groupId>
+      <artifactId>geronimo-jbatch_1.0_spec</artifactId>
+      <scope>compile</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.geronimo.specs</groupId>
+      <artifactId>geronimo-atinject_1.0_spec</artifactId>
+      <scope>compile</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.batchee</groupId>
+      <artifactId>batchee-jbatch</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>io.airlift</groupId>
+      <artifactId>airline</artifactId>
+      <version>0.6</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.testng</groupId>
+      <artifactId>testng</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>com.github.stefanbirkner</groupId>
+      <artifactId>system-rules</artifactId>
+      <version>1.4.0</version>
+    </dependency>
+  </dependencies>
+</project>

http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/9459ce38/cli/src/main/java/org/apache/batchee/cli/BatchEECLI.java
----------------------------------------------------------------------
diff --git a/cli/src/main/java/org/apache/batchee/cli/BatchEECLI.java b/cli/src/main/java/org/apache/batchee/cli/BatchEECLI.java
new file mode 100644
index 0000000..a13789a
--- /dev/null
+++ b/cli/src/main/java/org/apache/batchee/cli/BatchEECLI.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.batchee.cli;
+
+import io.airlift.command.Cli;
+import io.airlift.command.Help;
+import io.airlift.command.ParseException;
+import org.apache.batchee.cli.command.Restart;
+import org.apache.batchee.cli.command.Start;
+
+public class BatchEECLI {
+    public static void main(final String[] args) {
+        final Cli.CliBuilder<Runnable> builder = Cli.<Runnable>builder("batchee")
+                .withDescription("the stupid content tracker")
+                .withDefaultCommand(Help.class)
+                .withCommands(Help.class, Start.class, Restart.class);
+
+        final Cli<Runnable> parser = builder.build();
+
+        try {
+            parser.parse(args).run();
+        } catch (final ParseException e) {
+            parser.parse("help").run();
+        }
+    }
+
+    private BatchEECLI() {
+        // no-op
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/9459ce38/cli/src/main/java/org/apache/batchee/cli/command/JobOperatorCommand.java
----------------------------------------------------------------------
diff --git a/cli/src/main/java/org/apache/batchee/cli/command/JobOperatorCommand.java b/cli/src/main/java/org/apache/batchee/cli/command/JobOperatorCommand.java
new file mode 100644
index 0000000..94198f2
--- /dev/null
+++ b/cli/src/main/java/org/apache/batchee/cli/command/JobOperatorCommand.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.batchee.cli.command;
+
+import javax.batch.operations.JobOperator;
+import javax.batch.runtime.BatchRuntime;
+
+public abstract class JobOperatorCommand implements Runnable {
+    protected JobOperator operator() {
+        return BatchRuntime.getJobOperator();
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/9459ce38/cli/src/main/java/org/apache/batchee/cli/command/Restart.java
----------------------------------------------------------------------
diff --git a/cli/src/main/java/org/apache/batchee/cli/command/Restart.java b/cli/src/main/java/org/apache/batchee/cli/command/Restart.java
new file mode 100644
index 0000000..d991b06
--- /dev/null
+++ b/cli/src/main/java/org/apache/batchee/cli/command/Restart.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.batchee.cli.command;
+
+import io.airlift.command.Command;
+import io.airlift.command.Option;
+
+import javax.batch.operations.JobOperator;
+
+@Command(name = "restart", description = "restart a batch")
+public class Restart extends StartableCommand {
+    @Option(name = "-id", description = "id of the failed batch", required = true)
+    private long id;
+
+    @Override
+    protected long doStart(final JobOperator operator) {
+        final long nid = operator.restart(id, toProperties(properties));
+        info("Batch " + nid + " restarted with id #" + nid);
+        return nid;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/9459ce38/cli/src/main/java/org/apache/batchee/cli/command/Start.java
----------------------------------------------------------------------
diff --git a/cli/src/main/java/org/apache/batchee/cli/command/Start.java b/cli/src/main/java/org/apache/batchee/cli/command/Start.java
new file mode 100644
index 0000000..84416cb
--- /dev/null
+++ b/cli/src/main/java/org/apache/batchee/cli/command/Start.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.batchee.cli.command;
+
+import io.airlift.command.Command;
+import io.airlift.command.Option;
+
+import javax.batch.operations.JobOperator;
+
+@Command(name = "start", description = "start a batch")
+public class Start extends StartableCommand {
+    @Option(name = "-name", description = "name of the batch to start", required = true)
+    private String name;
+
+    @Override
+    protected long doStart(final JobOperator operator) {
+        final long id = operator.start(name, toProperties(properties));
+        info("Batch '" + name + "' started with id #" + id);
+        return id;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/9459ce38/cli/src/main/java/org/apache/batchee/cli/command/StartableCommand.java
----------------------------------------------------------------------
diff --git a/cli/src/main/java/org/apache/batchee/cli/command/StartableCommand.java b/cli/src/main/java/org/apache/batchee/cli/command/StartableCommand.java
new file mode 100644
index 0000000..082ea45
--- /dev/null
+++ b/cli/src/main/java/org/apache/batchee/cli/command/StartableCommand.java
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.batchee.cli.command;
+
+import io.airlift.command.Arguments;
+import io.airlift.command.Command;
+import io.airlift.command.Option;
+import org.apache.batchee.util.Batches;
+
+import javax.batch.operations.JobOperator;
+import javax.batch.runtime.BatchStatus;
+import javax.batch.runtime.JobExecution;
+import javax.batch.runtime.StepExecution;
+import java.util.Collection;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.TimeUnit;
+
+public abstract class StartableCommand extends JobOperatorCommand {
+    private static final String LINE = "=========================";
+
+    @Option(name = "-wait", description = "should wait the end of the batch")
+    protected boolean wait = true;
+
+    @Arguments(description = "properties to pass to the batch")
+    protected List<String> properties;
+
+    @Override
+    public void run() {
+        final JobOperator operator = operator();
+        final long id = doStart(operator);
+        if (wait) {
+            Batches.waitForEnd(operator, id);
+            report(operator, id);
+        }
+    }
+
+    protected abstract long doStart(JobOperator operator);
+
+    private void report(final JobOperator operator, final long id) {
+        final JobExecution execution = operator.getJobExecution(id);
+
+        info("");
+        info(LINE);
+
+        info("Batch status: " + statusToString(execution.getBatchStatus()));
+        info("Exit status:  " + execution.getExitStatus());
+        if (execution.getEndTime() != null && execution.getStartTime() != null) {
+            info("Duration:     " + TimeUnit.MILLISECONDS.toSeconds(execution.getEndTime().getTime() - execution.getStartTime().getTime()) + "s");
+        }
+
+        if (BatchStatus.FAILED.equals(execution.getBatchStatus())) {
+            final Collection<StepExecution> stepExecutions = operator.getStepExecutions(id);
+            for (final StepExecution stepExecution : stepExecutions) {
+                if (BatchStatus.FAILED.equals(stepExecution.getBatchStatus())) {
+                    info("");
+                    info("Step name       : " + stepExecution.getStepName());
+                    info("Step status     : " + statusToString(stepExecution.getBatchStatus()));
+                    info("Step exit status: " + stepExecution.getExitStatus());
+                    break;
+                }
+            }
+        }
+
+        info(LINE);
+    }
+
+    private static String statusToString(final BatchStatus status) {
+        return (status != null ? status.name() : "null");
+    }
+
+    protected void info(final String text) {
+        System.out.println(text);
+    }
+
+    protected static Properties toProperties(final List<String> properties) {
+        final Properties props = new Properties();
+        if (properties != null) {
+            for (final String kv : properties) {
+                final String[] split = kv.split("=");
+                if (split.length > 1) {
+                    props.setProperty(split[0], split[1]);
+                } else {
+                    props.setProperty(split[0], "");
+                }
+            }
+        }
+        return props;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/9459ce38/cli/src/main/resources/META-INF/NOTICE.txt
----------------------------------------------------------------------
diff --git a/cli/src/main/resources/META-INF/NOTICE.txt b/cli/src/main/resources/META-INF/NOTICE.txt
new file mode 100644
index 0000000..93adb52
--- /dev/null
+++ b/cli/src/main/resources/META-INF/NOTICE.txt
@@ -0,0 +1,4 @@
+Airline
+
+website: https://github.com/airlift/airline/
+license: Apache v2

http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/9459ce38/cli/src/test/java/org/apache/batchee/cli/MainTest.java
----------------------------------------------------------------------
diff --git a/cli/src/test/java/org/apache/batchee/cli/MainTest.java b/cli/src/test/java/org/apache/batchee/cli/MainTest.java
new file mode 100644
index 0000000..136dbff
--- /dev/null
+++ b/cli/src/test/java/org/apache/batchee/cli/MainTest.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.batchee.cli;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.contrib.java.lang.system.StandardOutputStreamLog;
+
+import static org.apache.batchee.cli.BatchEECLI.main;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertThat;
+
+public class MainTest {
+    @Rule
+    public final StandardOutputStreamLog stdout = new StandardOutputStreamLog();
+
+    @Test
+    public void start() {
+        main(new String[]{ "start", "-name", "sample" });
+        assertThat(stdout.getLog(), containsString("Batch 'sample' started with id #"));
+        assertThat(stdout.getLog(), containsString("Batch status: COMPLETED"));
+    }
+
+    @Test
+    public void startAndFail() {
+        main(new String[]{ "start", "-name", "sample", "status=fail" });
+        assertThat(stdout.getLog(), containsString("Batch 'sample' started with id #"));
+        assertThat(stdout.getLog(), containsString("Batch status: FAILED"));
+    }
+
+    @Test
+    public void restart() {
+        main(new String[]{ "start", "-name", "sample", "status=fail" });
+        final String str = "Batch 'sample' started with id #";
+
+        assertThat(stdout.getLog(), containsString(str));
+        final int idx = stdout.getLog().indexOf(str);
+        final int end = stdout.getLog().indexOf(System.getProperty("line.separator"), idx);
+        final String id = stdout.getLog().substring(idx + str.length(), end);
+
+        main(new String[]{ "restart", "-id", id, "status=ok" });
+        assertThat(stdout.getLog(), containsString(str));
+        assertThat(stdout.getLog(), containsString("Batch status: COMPLETED"));
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/9459ce38/cli/src/test/java/org/apache/batchee/cli/component/Sample.java
----------------------------------------------------------------------
diff --git a/cli/src/test/java/org/apache/batchee/cli/component/Sample.java b/cli/src/test/java/org/apache/batchee/cli/component/Sample.java
new file mode 100644
index 0000000..4006713
--- /dev/null
+++ b/cli/src/test/java/org/apache/batchee/cli/component/Sample.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.batchee.cli.component;
+
+import javax.batch.api.AbstractBatchlet;
+import javax.batch.api.BatchProperty;
+import javax.inject.Inject;
+
+public class Sample extends AbstractBatchlet {
+    @Inject
+    @BatchProperty
+    private String status = "OK";
+
+    @Override
+    public String process() throws Exception {
+        if ("fail".equals(status)) {
+            throw new IllegalStateException();
+        }
+        return status;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/9459ce38/cli/src/test/resources/META-INF/batch-jobs/sample.xml
----------------------------------------------------------------------
diff --git a/cli/src/test/resources/META-INF/batch-jobs/sample.xml b/cli/src/test/resources/META-INF/batch-jobs/sample.xml
new file mode 100644
index 0000000..68e862e
--- /dev/null
+++ b/cli/src/test/resources/META-INF/batch-jobs/sample.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  See the NOTICE file distributed with this work for additional information
+  regarding copyright ownership. Licensed under the Apache License,
+  Version 2.0 (the "License"); you may not use this file except in compliance
+  with the License. You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<job id="sample" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
+  <step id="sample-step">
+    <batchlet ref="org.apache.batchee.cli.component.Sample">
+      <properties>
+        <property name="status" value="#{jobParameters['status']}" />
+      </properties>
+    </batchlet>
+  </step>
+</job>

http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/9459ce38/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 727a72a..3edf19d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -103,6 +103,7 @@
         <module>gui</module>
         <module>tools</module>
       <module>test</module>
+      <module>cli</module>
     </modules>
 
     <dependencyManagement>