Vercel production error - "prisma:info Starting a mysql pool with 5 connections."

Viewed 15

I have a simple web app that allows users to upload images to an S3 bucket, a DB entry to a mySQL DB (via Prisma, in Railway.app), that then gets distributed through AWS Cloudfront and displayed in a list below the form.

I'm using Next JS and initially had some issues getting the OSSL key to read from my local env variables but finally managed to get it running via the Node FS:

import { getSignedUrl as getSignedCloudFrontUrl } from '@aws-sdk/cloudfront-signer';
import fs from 'fs';

    export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
        const images = (await prisma.image.findMany({
            orderBy: { created: 'desc' },
        })) as ModdedImage[];
    
        for (const image of images) {
            const getFileInfo = (filePath: string) => {
                let pemKey: string = '';
                return new Promise((resolve, reject) => {
                    const reader = fs.createReadStream(filePath);
                    reader.on('error', (error) => {
                        reject('There was an error');
                    });
                    reader.on('data', (chunk) => {
                        pemKey = chunk.toString();
                        resolve(pemKey);
                    });
                });
            };
    
            const pemKey = await getFileInfo('private_key.pem');
    
            const cfUrl = `https://[MY_CF_ID].cloudfront.net/${image.id}`;
    
            const url = getSignedCloudFrontUrl({
                url: cfUrl,
                dateLessThan: '2022-12-31',
                privateKey:
                    process.env.NODE_ENV === 'production'
                        ? process.env.CF_PK!
                        : (pemKey as string),
                keyPairId: process.env.CF_KPID!,
            });
    
            image.url = url;
        }
    
        res.setHeader(
            'Cache-Control',
            'public, s-maxage=10, stale-while-revalidate=59'
        );
    
        return {
            props: {
                images: JSON.parse(JSON.stringify(images)),
            },
        };
    };

I also have an image API point that uses the same code but with prisma.image.findFirst, to get the latest uploaded image instead of reloading the page:

const newImage = (await prisma.image.findFirstOrThrow({
                orderBy: { created: 'desc' },
            })) as ModdedImage;

After committing this to Vercel, I get a runtime error in production that is seemingly related to Prisma:

[GET] /
18:00:50:89
Function Status:
None
Edge Status:
500
Duration:
1960.26 ms
Init Duration:
N/A
Memory Used:
71 MB
ID:
arn1:arn1::2dznk-1663776050199-d3afad9777a1
User Agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36
RequestId: 8c5d596c-f5e7-423c-b443-bc7570bb0bcd Error: Runtime exited with error: exit status 1
Runtime.ExitError
prisma:info Starting a mysql pool with 5 connections.
2022-09-21T16:00:52.876Z    425dbfea-bafe-4f70-92b5-f8f335eed4ec    ERROR   Error: There was an error
    at Object.getProperError (/var/task/node_modules/next/dist/lib/is-error.js:25:12)
    at NextNodeServer.renderToResponse (/var/task/node_modules/next/dist/server/base-server.js:919:39)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async NextNodeServer.pipe (/var/task/node_modules/next/dist/server/base-server.js:368:25)
    at async Object.fn (/var/task/node_modules/next/dist/server/next-server.js:727:21)
    at async Router.execute (/var/task/node_modules/next/dist/server/router.js:247:36)
    at async NextNodeServer.run (/var/task/node_modules/next/dist/server/base-server.js:346:29)
    at async NextNodeServer.handleRequest (/var/task/node_modules/next/dist/server/base-server.js:284:20)
    at async Server.module.exports (/var/task/___next_launcher.cjs:29:9) {
  page: '/'
}
RequestId: 425dbfea-bafe-4f70-92b5-f8f335eed4ec Error: Runtime exited with error: exit status 1
Runtime.ExitError
[GET] /
18:00:38:01
Function Status:
None
Edge Status:
500
Duration:
2019.04 ms
Init Duration:
493.62 ms
Memory Used:
122 MB
ID:
sfo1:sfo1::4hzrz-1663776037882-738e1f3a51a0
User Agent:
node-fetch
prisma:info Starting a mysql pool with 5 connections.
2022-09-21T16:00:41.490Z    044f8012-9805-4466-a661-8687f661257a    ERROR   Error: There was an error
    at Object.getProperError (/var/task/node_modules/next/dist/lib/is-error.js:25:12)
    at NextNodeServer.renderToResponse (/var/task/node_modules/next/dist/server/base-server.js:919:39)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async NextNodeServer.pipe (/var/task/node_modules/next/dist/server/base-server.js:368:25)
    at async Object.fn (/var/task/node_modules/next/dist/server/next-server.js:727:21)
    at async Router.execute (/var/task/node_modules/next/dist/server/router.js:247:36)
    at async NextNodeServer.run (/var/task/node_modules/next/dist/server/base-server.js:346:29)
    at async NextNodeServer.handleRequest (/var/task/node_modules/next/dist/server/base-server.js:284:20)
    at async Server.module.exports (/var/task/___next_launcher.cjs:29:9) {
  page: '/'
}
RequestId: 044f8012-9805-4466-a661-8687f661257a Error: Runtime exited with error: exit status 1
Runtime.ExitError

I have also received the error without the aforementioned line prisma:info Starting a mysql pool with 5 connections., but this looks to be where the issue is.

I also received a similar warning in development (There are already 10 instances of Prisma Client actively running), after which I restructured my PrismaClient like this:

import { PrismaClient } from '@prisma/client';

declare global {
    var prisma: PrismaClient | undefined;
}

const prisma =
    global.prisma ||
    new PrismaClient({
        log: [
            { emit: 'event', level: 'query' },
            { emit: 'event', level: 'info' },
            { emit: 'event', level: 'error' },
            { emit: 'event', level: 'warn' },
        ],
    });

if (process.env.NODE_ENV !== 'production') global.prisma = prisma;

export default prisma;

Does anyone know what causes the error and how to fix it?

0 Answers
Related