Skip to content
This repository was archived by the owner on Apr 8, 2024. It is now read-only.

Commit 0d0f920

Browse files
committed
Fixes #151 - Make Eager Connection Optional
1 parent c8fcc13 commit 0d0f920

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ _New shiny stuff._
2020
- `ZBClient` now contains an `activateJobs` method. This effectively exposes the entire Zeebe GRPC API, and allows you to write applications in the completely unmanaged style of the Java and Go libraries, if you have some radically different idea about application patterns.
2121
- The Grpc layer has been refactored to implement the idea of "connection characteristics". When connecting to Camunda Cloud, which uses TLS and OAuth, the library would emit errors every time. The refactor allows these connection errors to be correctly interpreted as expected behaviour of the "connection characteristics". You can also set an explicit initial connection tolerance in milliseconds for any broker connection with the environment variable `ZEEBE_INITIAL_CONNECTION_TOLERANCE`. See [this article](https://www.joshwulf.com/blog/2020/03/camunda-cloud-connection-2/), issue [#133](https://github.com/creditsenseau/zeebe-client-node-js/issues/133), and the README section "Initial Connection Tolerance" for more details.
2222
- The connection tolerance for transient drop-outs before reporting a connection error is now configurable via the environment variable `ZEEBE_CONNECTION_TOLERANCE`, as well as the previous constructor argument `connectionTolerance`.
23+
- The ZBClient eagerly connects to the broker by issuing a topology command. This allows you an onReady event to be emitted. You can disable this (for example, for testing without a broker), by either passing `eagerConnection: false` to the client constructor options, or setting the environment variable `ZEEBE_NODE_EAGER_CONNECTION` to `false`. See [#151](https://github.com/creditsenseau/zeebe-client-node-js/issues/151).
2324
- The integration tests have been refactored to allow them to run against Camunda Cloud. This required dealing with a Zeebe broker in an unknown state, so all tests now template unique process ids, unique task types, and unique message names to avoid previous test run state in the cluster interfering with subsequent test runs.
2425
- I've started documenting the internal operation of the client in BPMN diagrams. These can be found in the `design` directory.
2526
- The README now contains a section "Writing Strongly-typed Job Workers", on writing typed workers in TypeScript.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ Retry is provided by [promise-retry](https://www.npmjs.com/package/promise-retry
210210

211211
Additionally, the gRPC Client will continually reconnect when in a failed state, such as when the gateway goes away due to pod rescheduling on Kubernetes.
212212

213+
<a name = "eager-connection"></a>
214+
215+
### Eager Connection
216+
217+
The ZBClient eagerly connects to the broker by issuing a topology command in the constructor. This allows you an onReady event to be emitted. You can disable this (for example, for testing without a broker), by either passing `eagerConnection: false` to the client constructor options, or setting the environment variable `ZEEBE_NODE_EAGER_CONNECTION` to `false`.
218+
213219
<a name = "on-ready"></a>
214220

215221
### onReady(), onConnectionError(), and connected

src/lib/ConfigurationHydrator.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export class ConfigurationHydrator {
1818
...ConfigurationHydrator.decodeConnectionString(gatewayAddress),
1919
...ConfigurationHydrator.getCamundaCloudConfig(options),
2020
...ConfigurationHydrator.readTLSFromEnvironment(options),
21+
...ConfigurationHydrator.getEagerStatus(options),
2122
}
2223
return configuration
2324
}
@@ -195,4 +196,15 @@ export class ConfigurationHydrator {
195196
? maybeClusterId.split('.zeebe.camunda.io')[0]
196197
: undefined
197198
}
199+
200+
private static getEagerStatus(options: ZBClientOptions | undefined) {
201+
return {
202+
eagerConnection: !(
203+
(
204+
process.env.ZEEBE_NODE_EAGER_CONNECT || 'trueByDefault'
205+
).toLocaleLowerCase() === 'false' ||
206+
options?.eagerConnection === false
207+
),
208+
}
209+
}
198210
}

src/lib/interfaces-published-contract.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface ZBCustomLogger {
2424

2525
export interface ZBClientOptions {
2626
connectionTolerance?: MaybeTimeDuration
27+
eagerConnection?: boolean
2728
loglevel?: Loglevel
2829
stdout?: ZBCustomLogger
2930
retry?: boolean

src/zb/ZBClient.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,21 @@ export class ZBClient extends EventEmitter {
190190
// Send command to broker to eagerly fail / prove connection.
191191
// This is useful for, for example: the Node-Red client, which wants to
192192
// display the connection status.
193-
this.topology()
194-
.then(res => {
195-
this.logger.logDirect(
196-
chalk.blueBright('Zeebe cluster topology:')
197-
)
198-
this.logger.logDirect(res.brokers)
199-
})
200-
.catch(e => {
201-
// Swallow exception to avoid throwing if retries are off
202-
if (e.thisWillNeverHappenYo) {
203-
this.emit('never')
204-
}
205-
})
193+
if (!!this.options.eagerConnection) {
194+
this.topology()
195+
.then(res => {
196+
this.logger.logDirect(
197+
chalk.blueBright('Zeebe cluster topology:')
198+
)
199+
this.logger.logDirect(res.brokers)
200+
})
201+
.catch(e => {
202+
// Swallow exception to avoid throwing if retries are off
203+
if (e.thisWillNeverHappenYo) {
204+
this.emit('never')
205+
}
206+
})
207+
}
206208
}
207209

208210
public activateJobs<

0 commit comments

Comments
 (0)