kafka-crab-js 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.toml +1 -1
- package/example/sample.mjs +2 -2
- package/index.d.ts +3 -1
- package/package.json +4 -4
- package/src/kafka/kafka_client.rs +9 -6
- package/src/kafka/kafka_consumer.rs +21 -6
package/Cargo.toml
CHANGED
|
@@ -26,7 +26,7 @@ rdkafka = { version = "0.33", features = [
|
|
|
26
26
|
tokio = { version = "1", features = ["full"] }
|
|
27
27
|
|
|
28
28
|
tracing = "0.1"
|
|
29
|
-
tracing-subscriber = { version = "0.3", features = ["
|
|
29
|
+
tracing-subscriber = { version = "0.3", features = ["json"] }
|
|
30
30
|
|
|
31
31
|
[build-dependencies]
|
|
32
32
|
napi-build = "2.0.1"
|
package/example/sample.mjs
CHANGED
|
@@ -7,8 +7,8 @@ import {KafkaClient, CommitMode, PartitionPosition, ConsumerResult} from '../ind
|
|
|
7
7
|
|
|
8
8
|
const kafkaClient = new KafkaClient({
|
|
9
9
|
brokers: 'localhost:29092',
|
|
10
|
-
clientId: 'my-
|
|
11
|
-
|
|
10
|
+
clientId: 'my-js-group',
|
|
11
|
+
logLevel: 'info',
|
|
12
12
|
enableAnsiLogger: true});
|
|
13
13
|
const topic = `topic-${nanoid()}`;
|
|
14
14
|
|
package/index.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export interface KafkaConfiguration {
|
|
|
14
14
|
clientId: string
|
|
15
15
|
securityProtocol?: SecurityProtocol
|
|
16
16
|
configuration?: Record<string, string>
|
|
17
|
-
|
|
17
|
+
logLevel?: string
|
|
18
18
|
}
|
|
19
19
|
export const enum CommitMode {
|
|
20
20
|
AutoCommit = 'AutoCommit',
|
|
@@ -26,6 +26,8 @@ export interface RetryStrategy {
|
|
|
26
26
|
retryTopic?: string
|
|
27
27
|
dqlTopic?: string
|
|
28
28
|
pauseConsumerDuration?: number
|
|
29
|
+
offset?: OffsetModel
|
|
30
|
+
configuration?: Record<string, string>
|
|
29
31
|
}
|
|
30
32
|
export const enum ConsumerResult {
|
|
31
33
|
Ok = 'Ok',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kafka-crab-js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"types": "index.d.ts",
|
|
6
6
|
"napi": {
|
|
@@ -55,8 +55,8 @@
|
|
|
55
55
|
},
|
|
56
56
|
"packageManager": "yarn@3.6.0",
|
|
57
57
|
"optionalDependencies": {
|
|
58
|
-
"kafka-crab-js-darwin-arm64": "0.
|
|
59
|
-
"kafka-crab-js-darwin-x64": "0.
|
|
60
|
-
"kafka-crab-js-linux-x64-gnu": "0.
|
|
58
|
+
"kafka-crab-js-darwin-arm64": "0.2.0",
|
|
59
|
+
"kafka-crab-js-darwin-x64": "0.2.0",
|
|
60
|
+
"kafka-crab-js-linux-x64-gnu": "0.2.0"
|
|
61
61
|
}
|
|
62
62
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
use std::{collections::HashMap, fmt};
|
|
1
|
+
use std::{collections::HashMap, fmt, str::FromStr};
|
|
2
2
|
|
|
3
3
|
use napi::{bindgen_prelude::*, Result};
|
|
4
4
|
use rdkafka::{
|
|
@@ -9,8 +9,7 @@ use rdkafka::{
|
|
|
9
9
|
topic_partition_list::TopicPartitionList,
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
-
use tracing::info;
|
|
13
|
-
use tracing_subscriber::EnvFilter;
|
|
12
|
+
use tracing::{info, Level};
|
|
14
13
|
|
|
15
14
|
use super::{
|
|
16
15
|
kafka_consumer::{ConsumerConfiguration, KafkaConsumer},
|
|
@@ -62,7 +61,7 @@ pub struct KafkaConfiguration {
|
|
|
62
61
|
pub client_id: String,
|
|
63
62
|
pub security_protocol: Option<SecurityProtocol>,
|
|
64
63
|
pub configuration: Option<HashMap<String, String>>,
|
|
65
|
-
pub
|
|
64
|
+
pub log_level: Option<String>,
|
|
66
65
|
}
|
|
67
66
|
|
|
68
67
|
#[derive(Clone, Debug)]
|
|
@@ -77,9 +76,13 @@ pub struct KafkaClient {
|
|
|
77
76
|
impl KafkaClient {
|
|
78
77
|
#[napi(constructor)]
|
|
79
78
|
pub fn new(kafka_configuration: KafkaConfiguration) -> Self {
|
|
79
|
+
let log_level = kafka_configuration.clone().log_level;
|
|
80
80
|
match tracing_subscriber::fmt()
|
|
81
|
-
.
|
|
82
|
-
|
|
81
|
+
.with_max_level(
|
|
82
|
+
Level::from_str(log_level.unwrap_or("error".to_owned()).as_str()).unwrap_or(Level::ERROR),
|
|
83
|
+
)
|
|
84
|
+
.json()
|
|
85
|
+
.with_ansi(false)
|
|
83
86
|
.try_init()
|
|
84
87
|
{
|
|
85
88
|
Ok(_) => {}
|
|
@@ -25,7 +25,7 @@ use super::{
|
|
|
25
25
|
kafka_admin::KafkaAdmin,
|
|
26
26
|
kafka_client::KafkaClient,
|
|
27
27
|
kafka_util::{kakfa_headers_to_hashmap_buffer, AnyhowToNapiError},
|
|
28
|
-
model::{OffsetModel, Payload},
|
|
28
|
+
model::{OffsetModel, PartitionPosition, Payload},
|
|
29
29
|
};
|
|
30
30
|
|
|
31
31
|
const RETRY_COUNTER_NAME: &str = "kafka-crab-js-retry-counter";
|
|
@@ -69,6 +69,8 @@ pub struct RetryStrategy {
|
|
|
69
69
|
pub retry_topic: Option<String>,
|
|
70
70
|
pub dql_topic: Option<String>,
|
|
71
71
|
pub pause_consumer_duration: Option<i64>,
|
|
72
|
+
pub offset: Option<OffsetModel>,
|
|
73
|
+
pub configuration: Option<HashMap<String, String>>,
|
|
72
74
|
}
|
|
73
75
|
|
|
74
76
|
#[napi(string_enum)]
|
|
@@ -184,7 +186,11 @@ impl KafkaConsumer {
|
|
|
184
186
|
};
|
|
185
187
|
let consumer_thread = ConsumerThread {
|
|
186
188
|
stream_consumer: self
|
|
187
|
-
.create_stream_consumer(
|
|
189
|
+
.create_stream_consumer(
|
|
190
|
+
&self.topic,
|
|
191
|
+
self.consumer_configuration.offset.clone(),
|
|
192
|
+
self.consumer_configuration.configuration.clone(),
|
|
193
|
+
)
|
|
188
194
|
.await
|
|
189
195
|
.map_err(|e| e.convert_to_napi())?,
|
|
190
196
|
func: func.clone(),
|
|
@@ -223,9 +229,17 @@ impl KafkaConsumer {
|
|
|
223
229
|
.pause_consumer_duration
|
|
224
230
|
.unwrap_or(DEFAULT_PAUSE_DURATION) as u64,
|
|
225
231
|
));
|
|
232
|
+
let offset_model = strategy.offset.clone().unwrap_or(OffsetModel {
|
|
233
|
+
position: Some(PartitionPosition::Stored),
|
|
234
|
+
offset: None,
|
|
235
|
+
});
|
|
226
236
|
let consumer_thread = ConsumerThread {
|
|
227
237
|
stream_consumer: self
|
|
228
|
-
.create_stream_consumer(
|
|
238
|
+
.create_stream_consumer(
|
|
239
|
+
&retry_topic,
|
|
240
|
+
Some(offset_model),
|
|
241
|
+
strategy.configuration.clone(),
|
|
242
|
+
)
|
|
229
243
|
.await
|
|
230
244
|
.map_err(|e| e.convert_to_napi())?,
|
|
231
245
|
func: func.clone(),
|
|
@@ -246,14 +260,14 @@ impl KafkaConsumer {
|
|
|
246
260
|
async fn create_stream_consumer(
|
|
247
261
|
&self,
|
|
248
262
|
topic: &str,
|
|
263
|
+
offset: Option<OffsetModel>,
|
|
264
|
+
configuration: Option<HashMap<String, String>>,
|
|
249
265
|
) -> anyhow::Result<StreamConsumer<CustomContext>> {
|
|
250
266
|
let context = CustomContext;
|
|
251
267
|
|
|
252
268
|
let ConsumerConfiguration {
|
|
253
|
-
offset,
|
|
254
269
|
create_topic,
|
|
255
270
|
group_id,
|
|
256
|
-
configuration,
|
|
257
271
|
enable_auto_commit,
|
|
258
272
|
..
|
|
259
273
|
} = self.consumer_configuration.clone();
|
|
@@ -339,7 +353,8 @@ impl ConsumerThread {
|
|
|
339
353
|
match self.handle_message_result(&message, message_result).await {
|
|
340
354
|
Ok(_) => {
|
|
341
355
|
debug!(
|
|
342
|
-
"Message consumed successfully. Partition: {}, Offset: {}",
|
|
356
|
+
"Message consumed successfully. Topic: {}, Partition: {}, Offset: {}",
|
|
357
|
+
message.topic(),
|
|
343
358
|
message.partition(),
|
|
344
359
|
message.offset()
|
|
345
360
|
);
|