Disconnecting prisma in jobs and dry runs
Opened this issue · 1 comments
Because jobs are invoked with workers through Bree they do not benefit from a global prisma instance. Each worker being a separate process creates a separate instance whether it's defined as a global object or not.
This leads to prisma not disconnecting correctly and is instead just being killed after each run. Prisma documentation shows how to gracefully disconnect prisma:
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
const emailService = new EmailService()
async function main() {
const allUsers = await prisma.user.findMany()
const emails = allUsers.map((x) => x.email)
await emailService.send(emails, 'Hello!')
}
main()
.then(async () => {
await prisma.$disconnect()
})
.catch(async (e) => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})
Because different jobs call the same function, specifically calcSigma
, and will do so even more in the future, the sigma instances needs to be passed as arguments to all relevant function. Importing is not possible because each function doesn't know which jobs it's being called from.
Implementing this will involve all function which call prisma. It is a good opportunity to implement "dry runs" as prisma does not provide this functionality natively. Firstly, because it necessary to testing this exact implementation. Secondly, because it's a requirement for further development and an eventual staging backend server which does not mess up the main database.
Dry flags can be implemented like they already are in calcSigma
only instead of just preventing db writes it should also print the intended behavior to console.
Todos
- implement the above example in all jobs
- pass prisma object through every function which needs it
- remove global prisma and imports
- pass dry flags through each function which writes to db
- print useful information on dry writes