neighbourhoodie / adonis-kafka-v6

MIT License
10 stars 0 forks source link

Change from "controllers" to "consumers" #6

Open ThisIsMissEm opened 4 months ago

ThisIsMissEm commented 4 months ago

Currently the patterns used in this plugin suggests sticking Kafka consumers in Adonis Controllers, which are typically for web requests, for better code separation, it'd be preferable to use a consumers directory

So, app/controllers/kafka/webhooks_controller becomes app/consumers/webhooks, as an example.

I think this would only require minimal changes to implement and give a clearer separation of concerns.

ThisIsMissEm commented 4 months ago

Additionally we can then do something like the @adonisjs/core/services/router package for registering consumers, like:

import consumers from '@neighbourhoodie/adonis-kafka/services/consumers'

const WebhookConsumer = () => import('#consumers/webhook_consumer')
const DownloadConsumer = () => import('#consumers/download_consumer')

consumers.group({ groupId: "webhook-service" })
  .subscribe({ topic: 'messages' }, [WebhooksConsumer, 'handleWebhook'])
  .subscribe({ topics: [ 'downloads' ] }, [DownloadsConsumer, 'handleDownload'])
  .subscribe({ topics: [ 'downloads-retry' ] }, [DownloadsConsumer, 'handleDownloadRetry'])

consumers.start()

We could also remove the need for consumers.start() by hooking into Service Provider's start() hook

i.e., on server start, we automatically start the consumers that are registered (kinda like how you don't need to do a router.listen(port) or something for HTTP).