-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AB#143 feat: emit the event when run state changes
- Loading branch information
1 parent
d21886b
commit b60d4ef
Showing
8 changed files
with
136 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
DATABASE_URL=postgresql://developer:Safe1!@localhost:5432/terraapprove?schema=public | ||
DATABASE_URL=postgresql://developer:Safe1!@localhost:5432/terraapprove?schema=public | ||
KAFKA_BROKERS=localhost:9092 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
DATABASE_URL=postgresql://developer:Safe1!@localhost:5433/terraapprove?schema=public | ||
DATABASE_URL=postgresql://developer:Safe1!@localhost:5433/terraapprove?schema=public | ||
KAFKA_BROKERS=localhost:9092 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {Injectable} from "@nestjs/common" | ||
import {Kafka} from "kafkajs" | ||
import {Config} from "../config/config" | ||
|
||
@Injectable() | ||
export class KafkaPublisher { | ||
private readonly kafka: Kafka | ||
|
||
constructor(readonly config: Config) { | ||
this.kafka = new Kafka({ | ||
clientId: "terraapprove-publisher", | ||
brokers: config.kafkaBrokers, | ||
retry: { | ||
retries: 2 | ||
}, | ||
connectionTimeout: 5000 | ||
}) | ||
} | ||
|
||
async publish(topic: string, message: string) { | ||
/* Possible improvements: cache the producers instead of creating a new | ||
one for each message to publish */ | ||
const producer = this.kafka.producer() | ||
await producer.connect() | ||
await producer.send({ | ||
topic, | ||
messages: [{value: message}] | ||
}) | ||
await producer.disconnect() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
RunEventPublisher, | ||
RunState | ||
} from "@libs/service/interfaces/run.interfaces" | ||
import {Injectable, Logger} from "@nestjs/common" | ||
import {TaskEither} from "fp-ts/lib/TaskEither" | ||
import {KafkaPublisher} from "./kafka-publisher" | ||
import * as TE from "fp-ts/lib/TaskEither" | ||
import {pipe} from "fp-ts/lib/function" | ||
|
||
@Injectable() | ||
export class RunKafkaEventPublisher implements RunEventPublisher { | ||
constructor(private readonly kafkaPublisher: KafkaPublisher) {} | ||
|
||
publishRunState(runState: RunState): TaskEither<never, void> { | ||
const result = pipe( | ||
runState, | ||
TE.right, | ||
TE.map(mapToEvent), | ||
TE.chainW(this.emitRunStateEvent()) | ||
) | ||
|
||
return result | ||
} | ||
|
||
private emitRunStateEvent(): (event: string) => TaskEither<never, void> { | ||
return event => | ||
TE.tryCatchK( | ||
() => this.kafkaPublisher.publish("run-state-changed", event), | ||
error => { | ||
Logger.error("Error while publishing run state event") | ||
throw error | ||
} | ||
)() | ||
} | ||
} | ||
|
||
function mapToEvent(runState: RunState): string { | ||
return JSON.stringify({ | ||
...runState, | ||
revision: runState.revision.toString() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters