MihaelIsaev/VaporCron

PostgreSQL "too many clients already" when running unit tests

Closed this issue · 1 comments

Hi, I'm experiencing the dreaded PostgreSQL Error: sorry, too many clients already error when running unit tests using Vapor 3.

I've configure a job like this:

In the boot file:

import Vapor
import VaporCron

/// Called after your application has initialized.
public func boot(_ app: Application) throws {
    _ = try? VaporCron.schedule(TokenCleanUpJob.self, on: app)
}

TokenCleanUpJob.swift:

import Vapor
import VaporCron
import FluentPostgreSQL

struct TokenCleanUpJob: VaporCronSchedulable {
    static var expression: String {
        return "*/60 * * * *" // Every 60 minutes
    }
    
    static func task(on container: VaporCronContainer) -> Future<Void> {
        return container.withPooledConnection(to: .psql) { connection in
            return Token.query(on: connection)
                .filter(\.expiry < Date())
                .delete()
                .always {
                    try? container.releasePooledConnection(connection, to: .psql)
                }
            }
    }
}

Can you tell me what I am doing wrong? Can this be resolved without having to migrate to Vapor 4?

Hey, maybe the problem is with amount of available connections either in pool, or in postgres config itself. I can't remember where to set amount of available connections for pool in Vapor3, but it is set to 1 by default as far as I remember.
If to speak about Vapor4, I'd love to help with transition Vapor3 -> Vapor4, you could write me in Discord to iMike#3049 🙂