Skip to content

Commit

Permalink
First version with Worker for Camunda8.
Browse files Browse the repository at this point in the history
  • Loading branch information
pme123 committed Jan 12, 2025
1 parent ebecd62 commit 18b7332
Show file tree
Hide file tree
Showing 15 changed files with 261 additions and 52 deletions.
2 changes: 1 addition & 1 deletion 01-domain/src/main/scala/camundala/domain/exports.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import scala.deriving.Mirror
import scala.language.implicitConversions

// circe JsonInOutEncoder/ Decoder
export io.circe.{Codec as CirceCodec}
export io.circe.Codec as CirceCodec
export io.circe.{Decoder, HCursor, Json}

type InOutCodec[T] = io.circe.Codec[T]
Expand Down
12 changes: 12 additions & 0 deletions 03-worker/src/main/scala/camundala/worker/JobWorker.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package camundala.worker


import scala.concurrent.duration.*


trait JobWorker:
def topic: String
def timeout: Duration = 10.seconds



22 changes: 22 additions & 0 deletions 03-worker/src/main/scala/camundala/worker/WorkerApp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package camundala.worker

import zio.*

import scala.compiletime.uninitialized

trait WorkerApp extends ZIOAppDefault:
def workerClients: Seq[WorkerClient[?]]
var theWorkers: Set[JobWorker] = uninitialized

def workers(dWorkers: (JobWorker | Seq[JobWorker])*): Unit =
theWorkers = dWorkers
.flatMap:
case d: JobWorker => Seq(d)
case s: Seq[?] => s.collect{case d: JobWorker => d}
.toSet

override def run: ZIO[Any, Any, Any] =
for
_ <- Console.printLine("Starting WorkerApp")
_ <- ZIO.collectAllPar(workerClients.map(_.run(theWorkers)))
yield ()
10 changes: 10 additions & 0 deletions 03-worker/src/main/scala/camundala/worker/WorkerClient.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package camundala.worker

import camundala.worker.JobWorker
import zio.ZIO

trait WorkerClient[T <: JobWorker]:
def run(workers: Set[JobWorker]): ZIO[Any, Any, Any] =
runWorkers(workers.collect { case w: T => w })
protected def runWorkers(workers: Set[T]): ZIO[Any, Any, Any]

3 changes: 2 additions & 1 deletion 03-worker/src/main/scala/camundala/worker/WorkerDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import camundala.domain.*
import camundala.worker.CamundalaWorkerError.*
import scala.reflect.ClassTag

trait WorkerDsl[In <: Product: InOutCodec, Out <: Product: InOutCodec]:
trait WorkerDsl[In <: Product: InOutCodec, Out <: Product: InOutCodec]
extends JobWorker:

protected def engineContext: EngineContext

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package camundala.worker.c8zio

import camundala.worker.JobWorker
import io.camunda.zeebe.client.api.response.ActivatedJob
import io.camunda.zeebe.client.api.worker.{JobClient, JobHandler}

trait C8Worker extends JobWorker, JobHandler:
def handle(client: JobClient, job: ActivatedJob): Unit =
println(s"Handling Job: ${job}")
client.newCompleteCommand(job.getKey).send().join()

end C8Worker
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package camundala.worker.c8zio

import camundala.worker.{JobWorker, WorkerClient}
import io.camunda.zeebe.client.ZeebeClient
import io.camunda.zeebe.client.impl.oauth.OAuthCredentialsProviderBuilder
import zio.{ZIO, ZIOAppDefault}
import zio.{Console, ZIO}

import java.net.URI
import java.time.Duration

object C8WorkerClient extends ZIOAppDefault:
object C8WorkerClient extends WorkerClient[C8Worker]:

override def run: ZIO[Any, Any, Any] =
def runWorkers(workers: Set[C8Worker]): ZIO[Any, Any, Any] =
Console.printLine(s"Starting Zeebe Worker Client: ${workers}") *>
ZIO.acquireReleaseWith(zeebeClient)(_.closeClient()): client =>
for
server <- ZIO.attempt(
Expand All @@ -18,16 +19,19 @@ object C8WorkerClient extends ZIOAppDefault:
.send
.join
).forever.fork
worker <- ZIO.attempt(client
.newWorker()
.jobType("publish-tweet")
.handler(ExampleJobHandler())
.timeout(Duration.ofSeconds(10))
.open()).fork
_ <- worker.join
_ <- ZIO.collectAllPar(workers.map(w => registerWorker(w, client)))
_ <- server.join
yield ()

private def registerWorker(worker: C8Worker, client: ZeebeClient) =
Console.printLine("Registering Worker: " + worker.topic) *>
ZIO.attempt(client
.newWorker()
.jobType(worker.topic)
.handler(worker)
.timeout(worker.timeout.toMillis)
.open())

private lazy val zeebeClient =
ZIO.attempt:
ZeebeClient.newClientBuilder()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_0m1f84q" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="5.23.0" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.21.0">
<bpmn:process id="twitter-auto-c7" name="twitter-auto-c7" isExecutable="true">
<bpmn:startEvent id="StartEvent_1">
<bpmn:outgoing>Flow_1c8rqmn</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:sequenceFlow id="Flow_1c8rqmn" sourceRef="StartEvent_1" targetRef="Activity_1p8rmh1" />
<bpmn:endEvent id="Event_1mv5kky">
<bpmn:incoming>Flow_0yzjas8</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_0yzjas8" sourceRef="Activity_1p8rmh1" targetRef="Event_1mv5kky" />
<bpmn:serviceTask id="Activity_1p8rmh1" name="Publish on Twitter" camunda:type="external" camunda:topic="publish-tweet">
<bpmn:incoming>Flow_1c8rqmn</bpmn:incoming>
<bpmn:outgoing>Flow_0yzjas8</bpmn:outgoing>
</bpmn:serviceTask>
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="twitter-auto-c7">
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
<dc:Bounds x="179" y="99" width="36" height="36" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_1mv5kky_di" bpmnElement="Event_1mv5kky">
<dc:Bounds x="432" y="99" width="36" height="36" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_1iubci5_di" bpmnElement="Activity_1p8rmh1">
<dc:Bounds x="270" y="77" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="Flow_1c8rqmn_di" bpmnElement="Flow_1c8rqmn">
<di:waypoint x="215" y="117" />
<di:waypoint x="270" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_0yzjas8_di" bpmnElement="Flow_0yzjas8">
<di:waypoint x="370" y="117" />
<di:waypoint x="432" y="117" />
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package camundala.examples.demos.newWorker

import camundala.bpmn.*

trait CompanyBpmnDsl extends BpmnDsl:

end CompanyBpmnDsl

trait CompanyBpmnProcessDsl extends BpmnProcessDsl, CompanyBpmnDsl
trait CompanyBpmnServiceTaskDsl extends BpmnServiceTaskDsl, CompanyBpmnDsl
trait CompanyBpmnCustomTaskDsl extends BpmnCustomTaskDsl, CompanyBpmnDsl
trait CompanyBpmnDecisionDsl extends BpmnDecisionDsl, CompanyBpmnDsl
trait CompanyBpmnUserTaskDsl extends BpmnUserTaskDsl, CompanyBpmnDsl
trait CompanyBpmnMessageEventDsl extends BpmnMessageEventDsl, CompanyBpmnDsl
trait CompanyBpmnSignalEventDsl extends BpmnSignalEventDsl, CompanyBpmnDsl
trait CompanyBpmnTimerEventDsl extends BpmnTimerEventDsl, CompanyBpmnDsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package camundala.examples.demos.newWorker

import camundala.camunda7.worker.C7WorkerHandler
import camundala.domain.*
import camundala.worker.*
import camundala.worker.c8zio.C8Worker

import scala.reflect.ClassTag

trait CompanyWorkerHandler extends C7WorkerHandler, C8Worker

trait CompanyValidationWorkerDsl[
In <: Product: InOutCodec
] extends CompanyWorkerHandler, ValidationWorkerDsl[In]

trait CompanyInitWorkerDsl[
In <: Product: InOutCodec,
Out <: Product: InOutCodec,
InitIn <: Product: InOutCodec,
InConfig <: Product: InOutCodec
] extends CompanyWorkerHandler, InitWorkerDsl[In, Out, InitIn, InConfig]

trait CompanyCustomWorkerDsl[
In <: Product: InOutCodec,
Out <: Product: InOutCodec
] extends CompanyWorkerHandler, CustomWorkerDsl[In, Out]


trait CompanyServiceWorkerDsl[
In <: Product: InOutCodec,
Out <: Product: InOutCodec,
ServiceIn: InOutEncoder,
ServiceOut: InOutDecoder: ClassTag
] extends CompanyWorkerHandler, ServiceWorkerDsl[In, Out, ServiceIn, ServiceOut]
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package camundala.examples.demos.newWorker

import camundala.bpmn.CustomTask
import camundala.domain.*
import camundala.examples.demos.newWorker.ExampleJob.*
import camundala.worker.CamundalaWorkerError
import camundala.worker.c8zio.C8Worker

object ExampleJobHandler extends CompanyCustomWorkerDsl[In, Out]:
lazy val customTask = example
def runWork(in: In): Either[CamundalaWorkerError.CustomError, Out] = ???
end ExampleJobHandler

object ExampleJob extends CompanyBpmnCustomTaskDsl:

val topicName = "publish-tweet"
val descr: String = "Creates and adjusts variables for the module creditcard."

case class In(
myId: Long = 123L,
myMessage: String = "hello"
)

object In:
given ApiSchema[In] = deriveApiSchema
given InOutCodec[In] = deriveInOutCodec

case class Out(
myId: Long = 123L,
myMessage: String = "hello"
)
object Out:
given ApiSchema[Out] = deriveApiSchema
given InOutCodec[Out] = deriveInOutCodec

lazy val example = customTask(
In(),
Out()
)
end ExampleJob
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package camundala.examples.demos.newWorker

import camundala.worker.{WorkerApp, WorkerClient}
import camundala.worker.c8zio.C8WorkerClient

trait CompanyWorkerApp extends WorkerApp:
lazy val workerClients: Seq[WorkerClient[?]] =
Seq(C8WorkerClient)


object TestWorkerApp extends CompanyWorkerApp:
workers(
ExampleJobHandler
)


Loading

0 comments on commit 18b7332

Please sign in to comment.