Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanlukas committed Oct 31, 2023
1 parent a9648c1 commit d1fccc3
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 54 deletions.
79 changes: 44 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

[![Compatible with: Camunda Platform 8](https://img.shields.io/badge/Compatible%20with-Camunda%20Platform%208-0072Ce)](https://github.com/camunda-community-hub/community/blob/main/extension-lifecycle.md#compatiblilty)


_This is a community project meant for playing around with Zeebe. It is not officially supported by the Zeebe Team (i.e. no gurantees). Everybody is invited to contribute!_
A Zeebe worker to evaluate scripts (i.e. script tasks). Scripts are useful for prototyping, to do (simple) calculations, or creating/modifying variables.
_This is a community project that provides a connector. It is not officially supported by Camunda. Everybody is invited to contribute!_
A connector to evaluate scripts (i.e. script tasks) that are not written in FEEL. Scripts are useful for prototyping, to do (simple) calculations, or creating/modifying variables.

## Usage

### Legacy

The legacy connector provides compatibility with the previous implementation `zeebe-script-worker`.

>The context does not offer access to `job` or `zeebeClient` anymore.
Example BPMN with service task:

```xml
Expand All @@ -21,6 +26,7 @@ Example BPMN with service task:
<zeebe:taskHeaders>
<zeebe:header key="language" value="javascript" />
<zeebe:header key="script" value="a + b" />
<zeebe:header key="resultVariable" value="result" />
</zeebe:taskHeaders>
</bpmn:extensionElements>
</bpmn:serviceTask>
Expand All @@ -30,64 +36,67 @@ Example BPMN with service task:
* required custom headers:
* `language` - the name of the script language
* `script` - the script to evaluate
* the result of the evaluation is passed as `result` variable
* `resultVariable` - the result of the evaluation is passed to this variable

### Connector

Available script languages:
The connector provides an [element template](./connector/element-templates/script-connector.json) that can be used to configure it.

### Script languages

Available script languages are by default:
* [javascript](https://www.graalvm.org/) (GraalVM JS)
* [groovy](http://groovy-lang.org/)
* [mustache](http://mustache.github.io/mustache.5.html)
* [kotlin](https://kotlinlang.org/)

To register new script languages, you can use the `ScriptEngineFactory` to register any JSR-223 compliant script engine.

If you want to provide a non-compliant implementation, you can use the [`ScriptEvaluatorExtension`](./connector/src/main/java/io/camunda/community/connector/script/spi/ScriptEvaluatorExtension.java) SPI.

To register custom file extensions, you can use the [`LanguageProviderExtension`](./connector/src/main/java/io/camunda/community/connector/script/spi/LanguageProviderExtension.java) SPI.

## Install

### Docker

The docker image for the worker is published on [GitHub Packages](https://github.com/orgs/camunda-community-hub/packages/container/package/zeebe-script-worker).
For a local setup, the repository contains a [docker-compose file](docker/docker-compose.yml). It starts a Zeebe broker and both (standalone and bundled) containers.

```
docker pull ghcr.io/camunda-community-hub/zeebe-script-worker:1.2.0
mvn clean package
cd docker
docker-compose up
```
* configure the connection to the Zeebe broker by setting `zeebe.client.broker.contactPoint` (default: `localhost:26500`)

For a local setup, the repository contains a [docker-compose file](docker/docker-compose.yml). It starts a Zeebe broker and the worker.
#### Standalone Runtime

The docker image for the connector runtime is published as GitHub package.

```
cd docker
docker-compose up
docker pull ghcr.io/camunda-community-hub/script-connector/runtime:latest
```

### Manual
Configure the connection to the Zeebe broker by setting the environment property `ZEEBE_CLIENT_BROKER_GATEWAY-ADDRESS` (default: `localhost:26500`)

1. Download the latest [worker JAR](https://github.com/zeebe-io/zeebe-script-worker/releases) _(zeebe-script-worker-%{VERSION}.jar
)_
The docker-compose file shows an example how this works.

1. Start the worker
`java -jar zeebe-script-worker-{VERSION}.jar`
#### Bundled Runtime

### Configuration
To run the connector inside the bundle, you can use the shaded jar.

The worker is a Spring Boot application that uses the [Spring Zeebe Starter](https://github.com/zeebe-io/spring-zeebe). The configuration can be changed via environment variables or an `application.yaml` file. See also the following resources:
* [Spring Zeebe Configuration](https://github.com/zeebe-io/spring-zeebe#configuring-zeebe-connection)
* [Spring Boot Configuration](https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config)
The docker-compose file shows an example how this works.

```
zeebe:
client:
worker:
defaultName: script-worker
defaultType: script
threads: 3
job.timeout: 10000
broker.contactPoint: 127.0.0.1:26500
security.plaintext: true
```
### Manual

#### Standalone Runtime

## Build from Source
1. Download the runtime jar `script-connector-runtime-{VERSION}.jar`
2. Start the connector runtime `java -jar script-connector-runtime-{VERSION}.jar`

Build with Maven
#### Bundled Runtime

`mvn clean install`
1. Download the shaded connector jar `script-connector-{VERSION}-shaded.jar`
2. Copy it to your connector runtime.

## Code of Conduct

Expand Down
4 changes: 2 additions & 2 deletions connector/element-templates/script-connector.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
}, {
"id" : "script.language",
"label" : "Script Language",
"description" : "The language the script uses",
"description" : "The language the script uses. By default, the ones available are: javascript, groovy, kotlin, mustache",
"optional" : false,
"constraints" : {
"notEmpty" : true
Expand All @@ -88,7 +88,7 @@
}, {
"id" : "script.resource",
"label" : "Script resource",
"description" : "The resource that should be executed",
"description" : "The resource that should be executed. Should be prefixed with 'classpath:' for a classpath resource, 'file:' for a file system resource. If none of these prefixes matches, it will attempt to load the provided resource as URL.",
"optional" : false,
"constraints" : {
"notEmpty" : true
Expand Down
5 changes: 2 additions & 3 deletions connector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<scope>provided</scope>
</dependency>

<!-- scriptType languages -->
<!-- script languages -->
<dependency>
<groupId>com.samskivert</groupId>
<artifactId>jmustache</artifactId>
Expand Down Expand Up @@ -85,8 +85,7 @@
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<useDependencyReducedPomInJar>true</useDependencyReducedPomInJar>
<dependencyReducedPomLocation>${project.build.directory}/dependency-reduced-pom.xml</dependencyReducedPomLocation>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public String getLanguageForScriptResource(String scriptResource) {
if (language == null) {
throw new IllegalStateException(
String.format(
"Could not determine scriptType language from file suffix '%s'", fileExtension));
"Could not determine script language from file suffix '%s'", fileExtension));
}
return language;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,19 @@ sealed interface Type {
record Embedded(
@TemplateProperty(label = "Script", description = "The script to be executed") @NotNull
String embedded,
@TemplateProperty(label = "Script Language", description = "The language the script uses")
@TemplateProperty(
label = "Script Language",
description =
"The language the script uses. By default, the ones available are: javascript, groovy, kotlin, mustache")
@NotNull
String language)
implements Type {}

record Resource(
@TemplateProperty(
label = "Script resource",
description = "The resource that should be executed")
description =
"The resource that should be executed. Should be prefixed with 'classpath:' for a classpath resource, 'file:' for a file system resource. If none of these prefixes matches, it will attempt to load the provided resource as URL.")
@NotNull
String resource)
implements Type {}
Expand Down

This file was deleted.

27 changes: 19 additions & 8 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,30 @@ services:
- "9600:9600"
networks:
- zeebe_network
zeebe-script-worker:
container_name: connector-runtime
image: camunda/connectors:8.3.0
script-connector-runtime:
container_name: script-connector-runtime
image: ghcr.io/camunda-community-hub/script-connector/runtime:latest
environment:
- ZEEBE_CLIENT_BROKER_GATEWAY-ADDRESS=zeebe:26500
- ZEEBE_CLIENT_SECURITY_PLAINTEXT=true
- CAMUNDA_CONNECTOR_POLLING_ENABLED=false
- CAMUNDA_CONNECTOR_WEBHOOK_ENABLED=false
- SPRING_MAIN_WEB-APPLICATION-TYPE=none
- OPERATE_CLIENT_ENABLED=false
depends_on:
- zeebe
networks:
- zeebe_network
script-connector-bundled:
container_name: script-connector-bundled
image: camunda/connectors-bundle:8.3.0
environment:
- ZEEBE_CLIENT_BROKER_GATEWAY-ADDRESS=zeebe:26500
- ZEEBE_CLIENT_SECURITY_PLAINTEXT=true
- CAMUNDA_CONNECTOR_POLLING_ENABLED=false
- CAMUNDA_CONNECTOR_WEBHOOK_ENABLED=false
- SPRING_MAIN_WEB-APPLICATION-TYPE=none
- OPERATE_CLIENT_ENABLED=false
depends_on:
- zeebe
networks:
- zeebe_network
volumes:
- ./../connector/target/script-connector-1.2.1-SNAPSHOT-with-script-engines.jar:/opt/app/script-connector.jar
- ./../connector/target/script-connector-1.2.1-SNAPSHOT-shaded.jar:/opt/custom/script-connector.jar

46 changes: 46 additions & 0 deletions runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
<groupId>io.camunda.connector</groupId>
<artifactId>spring-boot-starter-camunda-connectors</artifactId>
</dependency>
<dependency>
<groupId>io.camunda.spring</groupId>
<artifactId>spring-boot-starter-camunda-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down Expand Up @@ -49,4 +54,45 @@
</plugins>
</build>

<profiles>
<profile>
<id>build-image</id>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-version</id>
<goals>
<goal>build-image</goal>
</goals>
<phase>package</phase>
<configuration>
<image>
<name>ghcr.io/camunda-community-hub/script-connector/runtime:${project.version}
</name>
</image>
</configuration>
</execution>
<execution>
<id>build-latest</id>
<goals>
<goal>build-image</goal>
</goals>
<phase>package</phase>
<configuration>
<image>
<name>ghcr.io/camunda-community-hub/script-connector/runtime</name>
</image>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
7 changes: 7 additions & 0 deletions runtime/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
camunda:
connector:
polling:
enabled: false
webhook:
enabled: false

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.camunda.community.connector.script;

import static io.camunda.community.connector.script.ScriptConnector.*;
import static org.assertj.core.api.Assertions.*;

import io.camunda.zeebe.client.ZeebeClient;
import io.camunda.zeebe.client.api.response.ProcessInstanceResult;
import io.camunda.zeebe.model.bpmn.Bpmn;
import io.camunda.zeebe.model.bpmn.BpmnModelInstance;
import io.camunda.zeebe.spring.test.ZeebeSpringTest;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@ZeebeSpringTest
public class AppTest {

@Autowired ZeebeClient zeebeClient;

@Test
void shouldRun() {
// just to assert it runs
}

@Test
void shouldExecuteConnector() {
BpmnModelInstance modelInstance =
Bpmn.createExecutableProcess("process")
.startEvent()
.scriptTask(
"task",
t ->
t.zeebeJobType(SCRIPT_CONNECTOR_TYPE)
.zeebeInput("={a:a,b:a}", "context")
.zeebeInput("a+b", "script.embedded")
.zeebeInput("embedded", "script.type")
.zeebeInput("javascript", "script.language")
.zeebeTaskHeader("resultVariable", "result"))
.endEvent()
.done();
final var workflowInstanceResult =
deployAndCreateInstance(modelInstance, Collections.singletonMap("a", 3));

assertThat(workflowInstanceResult.getVariablesAsMap()).containsEntry("result", 6);
}

private ProcessInstanceResult deployAndCreateInstance(
final BpmnModelInstance workflow, Map<String, Object> variables) {
zeebeClient.newDeployResourceCommand().addProcessModel(workflow, "process.bpmn").send().join();

return zeebeClient
.newCreateInstanceCommand()
.bpmnProcessId("process")
.latestVersion()
.variables(variables)
.withResult()
.send()
.join();
}
}

0 comments on commit d1fccc3

Please sign in to comment.