Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add make:producer command #13

Open
wants to merge 8 commits into
base: feat/kafka-authentication
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions commands/make_producer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { BaseCommand, args, flags } from '@adonisjs/core/ace'
import string from '@adonisjs/core/helpers/string'
import { VariableDeclarationKind } from 'ts-morph'
import { stubsRoot } from '../stubs/main.js'
import { CommandOptions } from '@adonisjs/core/types/ace'

export default class MakeProducer extends BaseCommand {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file seems to not be emitted using the previous tsc / tsup configuration, hence needing to change.

static commandName = 'make:producer'
static description = 'Make a new Kafka producer'
static options: CommandOptions = {
allowUnknownFlags: true,
}

/**
* The name of the model file.
*/
@args.string({ description: 'Kafka topic to producer data to' })
declare topic: string

/**
* Defines if we generate the factory for the model.
*/
@flags.string({
name: 'producer',
alias: 'p',
description: 'The producer group in #start/kafka.ts',
default: 'default',
})
declare producer: string

/**
* Execute command
*/
async run(): Promise<void> {
const codemods = await this.createCodemods()

const ProducerVariable = `${this.app.generators.modelName(this.parsed.flags.producer)}Producer`
const ProducerId = string.create(this.parsed.flags.producer).dashCase()

await codemods.makeUsingStub(stubsRoot, 'make/producer.stub', {
topic: this.topic,
ProducerClass: ProducerVariable,
entity: this.app.generators.createEntity(this.topic),
})

const project = await codemods.getTsMorphProject()
if (project) {
const startFile = await project.getSourceFileOrThrow(this.app.startPath('kafka.ts'))

const producerDeclaration = startFile.getVariableDeclaration(ProducerVariable)
if (!producerDeclaration) {
// adds the following to #start/kafka.ts:
// export const {{ProducerVariable}} = await Kafka.createProducer('{{ProducerId}}')
startFile.insertVariableStatement(2, {
isExported: true,
declarationKind: VariableDeclarationKind.Const,
declarations: [
{
name: ProducerVariable,
initializer: `await Kafka.createProducer('${ProducerId}')`,
},
],
})
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds the export const DefaultProducer = await Kafka.createProducer('default') or if --producer webhooks then export const WebhookProducer = await Kafka.createProducer('webhooks') to #start/kafka.ts

It's a little cludgey but I think it's what we'd want.


await startFile.save()
}
}
}
19 changes: 17 additions & 2 deletions configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export async function configure(command: ConfigureCommand) {
/**
* Publish config file
*/
await codemods.makeUsingStub(stubsRoot, 'stubs/config/kafka.stub', {})
await codemods.makeUsingStub(stubsRoot, 'stubs/start/kafka.stub', {})
await codemods.makeUsingStub(stubsRoot, 'config/kafka.stub', {})
await codemods.makeUsingStub(stubsRoot, 'start/kafka.stub', {})
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea why I needed to change these, but after adjusting tsc config, suddenly it didn't find the files correctly. Looking at other packages, and it seems I shouldn't have included the stubs/ originally.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I had the reverse when I messed with this last, it wouldn’t work without stubs/ even though none of the examples I based this off had them and I tried this in an act of desperation. I’m glad this work correctly now.


/**
* Define environment variables
Expand Down Expand Up @@ -69,7 +69,22 @@ export async function configure(command: ConfigureCommand) {
* Register provider
*/
await codemods.updateRcFile((rcFile) => {
rcFile.addCommand(`${command.name}/commands`)
rcFile.addProvider(`${command.name}/kafka_provider`)
rcFile.addPreloadFile(`#start/kafka`)
})

/**
* Install packages
*/
// Prompt when `install` or `--no-install` flags are not used
let shouldInstallPackages: boolean | undefined = command.parsedFlags.install
if (shouldInstallPackages === undefined) {
shouldInstallPackages = await command.prompt.confirm(
`Do you want to install additional packages required by "${command.name}"?`
)
}
if (shouldInstallPackages) {
await codemods.installPackages([{ name: 'kafkajs', isDevDependency: false }])
}
Copy link
Collaborator Author

@ThisIsMissEm ThisIsMissEm May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though kafka is a peerDep, this makes sure we install it. There isn't a way to specify a version here though. In theory the peerDep will have us covered, so maybe we don't need this (was inspired by lucid's configure.ts)

}
10 changes: 5 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
|
*/

export { configure } from './configure.ts'
export { Kafka } from './src/index.ts'
export { Consumer } from './src/consumer.ts'
export { Producer } from './src/producer.ts'
export { defineConfig } from './src/define_config.ts'
export { configure } from './configure.js'
export { Kafka } from './src/index.js'
export { Consumer } from './src/consumer.js'
export { Producer } from './src/producer.js'
export { defineConfig } from './src/define_config.js'
26 changes: 6 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
],
"exports": {
".": "./build/index.js",
"./commands": "./build/commands/main.js",
"./commands/*": "./build/commands/*.js",
"./env": "./build/src/env/index.js",
"./types": "./build/src/types.js",
"./kafka_provider": "./build/providers/kafka_provider.js",
"./services/kafka": "./build/services/kafka.js"
},
"scripts": {
"index:commands": "adonis-kit index build/commands",
"clean": "del-cli build",
"copy:templates": "copyfiles \"stubs/**/*.stub\" build",
"typecheck": "tsc --noEmit",
Expand All @@ -27,8 +30,8 @@
"pretest": "npm run lint",
"test": "c8 npm run quick:test",
"prebuild": "npm run lint && npm run clean",
"build": "tsup-node && tsc --emitDeclarationOnly --declaration",
"postbuild": "npm run copy:templates",
"build": "tsc",
"postbuild": "npm run copy:templates && npm run index:commands",
"release": "np",
"version": "npm run build",
"prepublishOnly": "npm run build"
Expand Down Expand Up @@ -56,7 +59,6 @@
"prettier": "^3.1.1",
"sinon": "^17.0.1",
"ts-node": "^10.9.2",
"tsup": "^8.0.2",
"typescript": "^5.3.3"
},
"dependencies": {
Expand Down Expand Up @@ -88,21 +90,5 @@
"eslintConfig": {
"extends": "@adonisjs/eslint-config/package"
},
"prettier": "@adonisjs/prettier-config",
"tsup": {
"entry": [
"./index.ts",
"./src/env/index.ts",
"./src/types.ts",
"./services/kafka.ts",
"./providers/kafka_provider.ts",
"./factories/main.ts"
],
"outDir": "./build",
"clean": true,
"format": "esm",
"dts": false,
"sourcemap": true,
"target": "esnext"
}
"prettier": "@adonisjs/prettier-config"
}
2 changes: 1 addition & 1 deletion providers/kafka_provider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApplicationService, KafkaConfig } from '@adonisjs/core/types'
import { ContainerProviderContract } from '@adonisjs/core/types/app'

import { Kafka } from '../src/index.ts'
import { Kafka } from '../src/index.js'

export default class KafkaProvider implements ContainerProviderContract {
private app: ApplicationService
Expand Down
2 changes: 1 addition & 1 deletion services/kafka.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import app from '@adonisjs/core/services/app'
import { Kafka } from '../src/index.ts'
import { Kafka } from '../src/index.js'

let kafka: Kafka

Expand Down
131 changes: 67 additions & 64 deletions src/consumer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { Kafka, Consumer as KafkaConsumer } from 'kafkajs'
import { type EachMessagePayload } from 'kafkajs'

import { ConsumerGroupConfig, ConsumerSubscribeTopics, ConsumerSubscribeTopic } from './types.ts'
import type {
ConsumerGroupConfig,
ConsumerSubscribeTopics,
ConsumerSubscribeTopic,
ConsumerErrorHandler,
ConsumerCallback,
ConsumerCommitCallback,
} from './types.ts'

export class Consumer {
config: ConsumerGroupConfig
topics: string[]
events: any
errorHandlers: any
events: Record<string, ConsumerCallback[]>
errorHandlers: Record<string, ConsumerErrorHandler[]>
consumer: KafkaConsumer

#started: boolean = false
Expand All @@ -22,7 +29,7 @@ export class Consumer {
}

async eachMessage(payload: EachMessagePayload): Promise<void> {
const { topic, partition, message } = payload
const { topic, partition, message, heartbeat, pause } = payload

let result: any
try {
Expand All @@ -31,33 +38,49 @@ export class Consumer {
}
result = JSON.parse(message.value.toString())
} catch (error) {
this.raiseError(topic, error)
this.handleError(topic, error)
return
}

const events = this.events[topic]
const callbacks = this.events[topic]

if (!events || !events.length) {
if (!callbacks || !callbacks.length) {
return
}

const promises = events.map((callback: any) => {
return new Promise<void>((resolve) => {
callback(
result,
async (commit = true) => {
if (this.config.autoCommit) {
return resolve()
}
if (commit) {
const offset = (Number(message.offset) + 1).toString()
await this.consumer.commitOffsets([{ topic, partition, offset }])
}
const promises = callbacks.map((callback) => {
return new Promise<void>(async (resolve, reject) => {
let committed = false

const committer: ConsumerCommitCallback = async (commit = true) => {
committed = true

if (this.config.autoCommit) {
return resolve()
}

if (commit) {
const offset = (Number(message.offset) + 1).toString()
await this.consumer.commitOffsets([{ topic, partition, offset }])
}

resolve()
}

try {
await callback(result, committer, heartbeat, pause)
} catch (error) {
this.handleError(topic, error)
resolve()
}

if (!committed) {
if (this.config.autoCommit) {
resolve()
},
payload
)
} else {
reject(new Error('Expected commit() to be called as autoCommit is false'))
}
}
})
})

Expand Down Expand Up @@ -91,69 +114,49 @@ export class Consumer {
return this
}

async on({ topic, fromBeginning }: ConsumerSubscribeTopic, callback: any) {
const callbackFn = this.resolveCallback(callback)
if (!callbackFn) {
throw new Error('no callback specified or cannot find your controller method')
async on(subscription: ConsumerSubscribeTopics, callback: ConsumerCallback): Promise<void>
async on(subscription: ConsumerSubscribeTopic, callback: ConsumerCallback): Promise<void>
async on(
subscription: ConsumerSubscribeTopic & ConsumerSubscribeTopics,
callback: ConsumerCallback
) {
if (typeof callback !== 'function') {
throw new TypeError('Consumer callback is not a function')
}

if (topic instanceof RegExp) {
throw new Error('regexp topic not supported by adonis-kafka yet')
let topics = []
if (Array.isArray(subscription.topics)) {
topics = subscription.topics
} else {
topics = subscription.topic.split(',').filter((topic) => !!topic)
}

let topicArray = [topic]

if (typeof topic === 'string') {
topicArray = topic.split(',')
}

topicArray.forEach(async (item: any) => {
if (!item) {
return
}

topics.forEach(async (item) => {
const events = this.events[item] || []

events.push(callbackFn)
events.push(callback)

this.events[item] = events

this.topics.push(item)
})

await this.consumer.subscribe({
topic: item,
fromBeginning: fromBeginning,
})
await this.consumer.subscribe({
topics,
fromBeginning: subscription.fromBeginning ?? false,
})
}

raiseError(topic: string, error: Error) {
handleError(topic: string, error: Error) {
const handlers = this.errorHandlers[topic] || []
handlers.forEach((handler: any) => {
handlers.forEach((handler) => {
handler(error)
})
}

registerErrorHandler(topic: string, callback: any) {
//TODO add resolveCallback
onError(topic: string, callback: ConsumerErrorHandler) {
const handlers = this.errorHandlers[topic] || []
handlers.push(callback)
this.errorHandlers[topic] = handlers
}

resolveCallback(callback: any) {
if (Array.isArray(callback)) {
const [ControllerClass, fn] = callback
const controller = new ControllerClass()
if (typeof controller[fn] === 'function') {
return controller[fn].bind(controller)
}
}

if (typeof callback === 'function') {
return callback
}

return null
}
}
2 changes: 0 additions & 2 deletions src/define_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ export function defineConfig(config = {}): KafkaConfig {
return {
brokers: 'localhost:9092',
clientId: 'local',
connectionTimeout: 3000,
requestTimeout: 60000,
logLevel: 'info',
// Overwrite default config values if another one is provided
...config,
Expand Down
Loading
Loading