-
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.
Merge pull request #38 from navikt/dev/kafka
Dev/kafka
- Loading branch information
Showing
12 changed files
with
6,262 additions
and
85 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,34 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
import createProducer from '../kafka/automatisk-reaktivert-producer'; | ||
|
||
(async function () { | ||
const prismaClient = new PrismaClient(); | ||
const kafkaProducer = await createProducer(); | ||
|
||
console.log('Starter med å hente automatiskReaktivering...'); | ||
const automatiskReaktivering = await prismaClient.automatiskReaktivering.findMany({ | ||
where: { | ||
created_at: { | ||
lt: new Date(), | ||
}, | ||
}, | ||
}); | ||
console.log(`Fant ${automatiskReaktivering.length} rader`); | ||
console.log('Dumper automatiskReaktivering på kafka...'); | ||
await kafkaProducer.send(automatiskReaktivering); | ||
console.log('Ferdig med automatiskReaktivering\n\n\n'); | ||
|
||
console.log('Starter med å hente automatiskReaktiveringSvar...'); | ||
const automatiskReaktiveringSvar = await prismaClient.automatiskReaktiveringSvar.findMany({ | ||
where: { | ||
created_at: { | ||
lt: new Date(), | ||
}, | ||
}, | ||
}); | ||
|
||
console.log(`Fant ${automatiskReaktiveringSvar.length} rader`); | ||
console.log('Dumper automatiskReaktiveringSvar på kafka...'); | ||
await kafkaProducer.send(automatiskReaktiveringSvar); | ||
console.log('Ferdig med automatiskReaktiveringSvar'); | ||
})(); |
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
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,62 @@ | ||
import { AutomatiskReaktivering, AutomatiskReaktiveringSvar } from '@prisma/client'; | ||
import { Kafka, KafkaConfig } from 'kafkajs'; | ||
import config from '../config'; | ||
import logger from '../logger'; | ||
|
||
export interface KafkaProducer { | ||
send(data: Array<AutomatiskReaktivering | AutomatiskReaktiveringSvar>): Promise<void>; | ||
} | ||
|
||
const kafkaConfig: KafkaConfig = { | ||
clientId: config.APP_NAME, | ||
brokers: [config.KAFKA_BROKERS], | ||
ssl: !config.KAFKA_CA | ||
? false | ||
: { | ||
rejectUnauthorized: false, | ||
ca: [config.KAFKA_CA], | ||
key: config.KAFKA_PRIVATE_KEY, | ||
cert: config.KAFKA_CERTIFICATE, | ||
}, | ||
}; | ||
const createProducer = async (): Promise<KafkaProducer> => { | ||
const kafka = new Kafka(kafkaConfig); | ||
const producer = kafka.producer(); | ||
|
||
await producer.connect(); | ||
|
||
return { | ||
async send(data: Array<AutomatiskReaktivering | AutomatiskReaktiveringSvar>): Promise<void> { | ||
const messages = data.map((d) => { | ||
if ('svar' in d) { | ||
return { | ||
value: JSON.stringify({ | ||
bruker_id: d.bruker_id, | ||
created_at: d.created_at, | ||
svar: d.svar, | ||
type: 'AutomatiskReaktiveringSvar', | ||
}), | ||
}; | ||
} | ||
return { | ||
value: JSON.stringify({ | ||
bruker_id: d.bruker_id, | ||
created_at: d.created_at, | ||
type: 'AutomatiskReaktivering', | ||
}), | ||
}; | ||
}); | ||
|
||
try { | ||
await producer.send({ | ||
topic: config.KAFKA_TOPIC, | ||
messages, | ||
}); | ||
} catch (e) { | ||
logger.error(`Fikk ikke sendt melding til kafka ${e}`); | ||
} | ||
}, | ||
}; | ||
}; | ||
|
||
export default createProducer; |
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